通过S3_附加_Kwarg将Pandas数据帧加载到S3

2024-04-26 09:05:24 发布

您现在位置:Python中文网/ 问答频道 /正文

请原谅我在这方面的无知/缺乏知识

我想上传一个数据帧到S3,但我需要传递“ACL”:“bucket-owner-full-control”

import pandas as pd
import s3fs

fs = s3fs.S3FileSystem(anon=False, s3_additional_kwargs={'ACL': 'bucket-owner-full-control'})
df = pd.DataFrame()
df['test'] = [1,2,3]
df.head()

df.to_parquet('s3://path/to/file/df.parquet', compression='gzip')

我通过将其加载到Pyarrow表和类似的加载方式来解决此问题:

import pyarrow.parquet as pq

table = pa.Table.from_pandas(df)

pq.write_to_dataset(table=table, 
                    root_path='s3://path/to/file/',
                    filesystem=fs) 

但这让人感觉很不舒服,在第一个例子中,我觉得一定有办法通过ACL


Tags: topathimportpandasdfs3bucketas
2条回答

你可以这样做:

pd.to_parquet('name.parquet',storage_options={"key":xxxxx,"secret":gcp_secret_access_key,'xxxxx':{'ACL': 'bucket-owner-full-control'}})

对于熊猫1.2.0,这里提到了storage_options

如果你被熊猫困住了<;1.2.0(在我的例子中是1.1.3),这个技巧确实有帮助:

storage_options = dict(anon=False, s3_additional_kwargs=dict(ACL="bucket-owner-full-control"))

import s3fs
fs = s3fs.S3FileSystem(**storage_options)
df.to_parquet('s3://foo/bar.parquet', filesystem=fs)

相关问题 更多 >