简体   繁体   English

使用 pandas.to_csv function 时出现权限错误

[英]PermissionError while using pandas .to_csv function

I have this code to read.txt file from s3 and convert this file to.csv using pandas:我有这个代码从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')

I have provided read write permission to IAM role but still this errors comes for the.to_csv function:我已向 IAM 角色提供了读写权限,但对于 .to_csv function 仍然出现此错误:

Anonymous access is forbidden for this operation: PermissionError

update: full error in ec2 logs is:更新: 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

I don't know why is it trying to create bucket?我不知道它为什么要尝试创建存储桶? and I have provided full access of s3 to lambda role我已经提供了 s3 对 lambda 角色的完全访问权限

Can someone please tell me what i'm missing here?有人可以告诉我我在这里缺少什么吗?

Thank you.谢谢你。

I think this is due to an incompatibility between the pandas, boto3 and s3fs libraries.我认为这是由于 pandas、boto3 和 s3fs 库之间的不兼容造成的。

Try this setup:试试这个设置:

pandas==1.0.3熊猫==1.0.3

boto3==1.13.11 boto3==1.13.11

s3fs==0.4.2 s3fs==0.4.2

I also tried with pandas version 1.1.1, it works too (I work with Python 3.7)我还尝试使用 pandas 版本 1.1.1,它也可以工作(我使用 Python 3.7)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM