使用pandas .to_csv函数时发生PermissionError

f87krz0w  于 2023-03-21  发布在  其他
关注(0)|答案(2)|浏览(149)

我有这样的代码从s3读取.txt文件,并使用pandas将此文件转换为.csv:

file = pd.read_csv(f's3://{bucket_name}/{bucket_key}', sep=':', error_bad_lines=False)
file.to_csv(f's3://{bucket_name}/file_name.csv')

我已经向IAM角色提供了读写权限,但.to_csv函数仍然出现此错误:

Anonymous access is forbidden for this operation: PermissionError

**update:**ec2日志中的完整错误为:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/s3fs/core.py", line 446, in _mkdir
    await self.s3.create_bucket(**params)
  File "/usr/local/lib/python3.6/dist-packages/aiobotocore/client.py", line 134, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the CreateBucket operation: Anonymous access is forbidden for this operation

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

Traceback (most recent call last):
  File "convert_file_instance.py", line 92, in <module>
    main()
  File "convert_file_instance.py", line 36, in main
    raise e
  File "convert_file_instance.py", line 30, in main
    file.to_csv(f's3://{bucket_name}/file_name.csv')
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 3165, in to_csv
    decimal=decimal,
  File "/usr/local/lib/python3.6/dist-packages/pandas/io/formats/csvs.py", line 67, in __init__
    path_or_buf, encoding=encoding, compression=compression, mode=mode
  File "/usr/local/lib/python3.6/dist-packages/pandas/io/common.py", line 233, in get_filepath_or_buffer
    filepath_or_buffer, mode=mode or "rb", **(storage_options or {})
  File "/usr/local/lib/python3.6/dist-packages/fsspec/core.py", line 399, in open
    **kwargs
  File "/usr/local/lib/python3.6/dist-packages/fsspec/core.py", line 254, in open_files
    [fs.makedirs(parent, exist_ok=True) for parent in parents]
  File "/usr/local/lib/python3.6/dist-packages/fsspec/core.py", line 254, in <listcomp>
    [fs.makedirs(parent, exist_ok=True) for parent in parents]
  File "/usr/local/lib/python3.6/dist-packages/s3fs/core.py", line 460, in makedirs
    self.mkdir(path, create_parents=True)
  File "/usr/local/lib/python3.6/dist-packages/fsspec/asyn.py", line 100, in wrapper
    return maybe_sync(func, self, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/fsspec/asyn.py", line 80, in maybe_sync
    return sync(loop, func, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/fsspec/asyn.py", line 51, in sync
    raise exc.with_traceback(tb)
  File "/usr/local/lib/python3.6/dist-packages/fsspec/asyn.py", line 35, in f
    result[0] = await future
  File "/usr/local/lib/python3.6/dist-packages/s3fs/core.py", line 450, in _mkdir
    raise translate_boto_error(e) from e
PermissionError: Anonymous access is forbidden for this operation

我不知道它为什么要创建bucket?我已经为lambda角色提供了s3的完全访问权限
谁能告诉我我错过了什么?
谢谢你。

mcvgt66p

mcvgt66p1#

我认为这是由于pandas、boto3和s3fs库之间的不兼容。
试试这个设置:
Pandas==1.0.3
boto3==1.13.11
s3fs==0.4.2
我也尝试了pandas版本1.1.1,它也可以工作(我使用Python 3.7)

m0rkklqb

m0rkklqb2#

我在新版本的pandas中遇到了类似的问题,这个版本组合解决了这个问题:
pandas==1.5.2
s3fs==2022.11.0

相关问题