Skip to content

Cannot use UPath on S3 with pandas: PermissionError/Access Denied #241

Open
@ba1dr

Description

@ba1dr
import pandas as pd
from upath import UPath

AWS_KEY = "AKIAxxxxxxx"
AWS_SECRET = "xxxxxxxxxxxxxxx"

bucket = 'upathtest'
fkey = f"folder1/folder2/test1.xlsx"
s3base = UPath(f"s3://{bucket}", key=AWS_KEY, secret=AWS_SECRET)
s3path = s3base / fkey

print(list(s3base.iterdir()))      # THIS WORKS!
with s3path.open('w') as ff:
    ff.write("test1,test2")        # THIS WORKS EITHER!

df = pd.DataFrame()
df.to_excel(s3path)           # !! This fails
Traceback

Traceback (most recent call last):
  File "/mypath/venv/lib/python3.11/site-packages/s3fs/core.py", line 113, in _error_wrapper
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/aiobotocore/client.py", line 411, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden

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

Traceback (most recent call last):
  File "/mypath/try_fss.py", line 57, in <module>
    main()
  File "/mypath/try_fss.py", line 53, in main
    test03()
  File "/mypath/try_fss.py", line 47, in test03
    pd.read_csv(s3path)
  File "/mypath/venv/lib/python3.11/site-packages/pandas/io/parsers/readers.py", line 1026, in read_csv
    return _read(filepath_or_buffer, kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/pandas/io/parsers/readers.py", line 620, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/pandas/io/parsers/readers.py", line 1620, in __init__
    self._engine = self._make_engine(f, self.engine)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/pandas/io/parsers/readers.py", line 1880, in _make_engine
    self.handles = get_handle(
                   ^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/pandas/io/common.py", line 728, in get_handle
    ioargs = _get_filepath_or_buffer(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/pandas/io/common.py", line 443, in _get_filepath_or_buffer
    ).open()
      ^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/fsspec/core.py", line 147, in open
    return self.__enter__()
           ^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/fsspec/core.py", line 105, in __enter__
    f = self.fs.open(self.path, mode=mode)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/fsspec/spec.py", line 1303, in open
    f = self._open(
        ^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/s3fs/core.py", line 689, in _open
    return S3File(
           ^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/s3fs/core.py", line 2183, in __init__
    super().__init__(
  File "/mypath/venv/lib/python3.11/site-packages/fsspec/spec.py", line 1742, in __init__
    self.size = self.details["size"]
                ^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/fsspec/spec.py", line 1755, in details
    self._details = self.fs.info(self.path)
                    ^^^^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/fsspec/asyn.py", line 118, in wrapper
    return sync(self.loop, func, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/fsspec/asyn.py", line 103, in sync
    raise return_result
  File "/mypath/venv/lib/python3.11/site-packages/fsspec/asyn.py", line 56, in _runner
    result[0] = await coro
                ^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/s3fs/core.py", line 1375, in _info
    out = await self._call_s3(
          ^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/s3fs/core.py", line 366, in _call_s3
    return await _error_wrapper(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/mypath/venv/lib/python3.11/site-packages/s3fs/core.py", line 145, in _error_wrapper
    raise err
PermissionError: Forbidden

I tried to use client_kwargs - this does not work either.

aioboto_client_kwargs = {
    'aws_access_key_id': AWS_KEY,
    'aws_secret_access_key': AWS_SECRET,
}
s3base = UPath(f"s3://{bucket}", client_kwargs=aioboto_client_kwargs)
...
# same error

AWS user has AmazonS3FullAccess policy attached.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions