Skip to content

Writing to a new file in an existing bucket throws "unspecified location constraint" error as it incorrectly tries to create new bucket #1404

@DanielTsiang

Description

@DanielTsiang

Writing to a new file in an existing bucket throws "unspecified location constraint" error as it incorrectly tries to create new bucket

Problem

Using the current latest versions of fsspec and s3fs i.e. 2023.10.0, when I tried to write to a new file inside a "subdirectory" in S3 that didn't exist yet via:

with fsspec.open("S3://existing-bucket-name/existing-key/new-key/new-file", "w") as file:
    df.to_csv(file, index=False)

I got this error, even though the S3 bucket already exists:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/s3fs/core.py", line 113, in _error_wrapper
    return await func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/aiobotocore/client.py", line 383, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  ...
    with fsspec.open("S3://existing-bucket-name/existing-key/new-key/new-file", "w") as file:
  File "/usr/local/lib/python3.10/site-packages/fsspec/core.py", line 452, in open
    out = open_files(
  File "/usr/local/lib/python3.10/site-packages/fsspec/core.py", line 293, in open_files
    [fs.makedirs(parent, exist_ok=True) for parent in parents]
  File "/usr/local/lib/python3.10/site-packages/fsspec/core.py", line 293, in <listcomp>
    [fs.makedirs(parent, exist_ok=True) for parent in parents]
  File "/usr/local/lib/python3.10/site-packages/fsspec/asyn.py", line 118, in wrapper
    return sync(self.loop, func, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/fsspec/asyn.py", line 103, in sync
    raise return_result
  File "/usr/local/lib/python3.10/site-packages/fsspec/asyn.py", line 56, in _runner
    result[0] = await coro
  File "/usr/local/lib/python3.10/site-packages/s3fs/core.py", line 914, in _makedirs
    await self._mkdir(path, create_parents=True)
  File "/usr/local/lib/python3.10/site-packages/s3fs/core.py", line 899, in _mkdir
    await self._call_s3("create_bucket", **params)
  File "/usr/local/lib/python3.10/site-packages/s3fs/core.py", line 348, in _call_s3
    return await _error_wrapper(
  File "/usr/local/lib/python3.10/site-packages/s3fs/core.py", line 140, in _error_wrapper
    raise err
PermissionError: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.

This happens because of the parameter auto_mkdir=True in the open_files() function here.

Workaround

I managed to resolve this error by changing the command to the following, i.e. adding auto_mkdir=False as a kwarg:

with fsspec.open("S3://existing-bucket-name/existing-key/new-key/new-file", "w", auto_mkdir=False) as file:
    df.to_csv(file, index=False)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions