Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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:

Anonymous access is forbidden for this operation: PermissionError

update: full error in ec2 logs is:

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

Can someone please tell me what i'm missing here?

Thank you.

like image 298
dar Avatar asked Jun 08 '26 17:06

dar


1 Answers

I think this is due to an incompatibility between the pandas, boto3 and s3fs libraries.

Try this setup:

pandas==1.0.3

boto3==1.13.11

s3fs==0.4.2

I also tried with pandas version 1.1.1, it works too (I work with Python 3.7)

like image 66
David Avatar answered Jun 11 '26 06:06

David