使用 Boto3 将文件上传到带有前缀的 S3 存储桶
Uploading a file to a S3 bucket with a prefix using Boto3
我正在尝试将文件上传到 S3 存储桶,但我无权访问存储桶的根级别,我需要将其上传到某个前缀。以下代码:
import boto3
s3 = boto3.resource('s3')
open('/tmp/hello.txt', 'w+').write('Hello, world!')
s3_client.upload_file('/tmp/hello.txt', bucket_name, prefix+'hello-remote.txt')
给我一个错误:
An error occurred (AccessDenied) when calling the PutObject operation: Access Denied: ClientError Traceback (most recent call last): File "/var/task/tracker.py", line 1009, in testHandler s3_client.upload_file('/tmp/hello.txt', bucket_name, prefix+'hello-remote.txt') File "/var/runtime/boto3/s3/inject.py", line 71, in upload_file extra_args=ExtraArgs, callback=Callback) File "/var/runtime/boto3/s3/transfer.py", line 641, in upload_file self._put_object(filename, bucket, key, callback, extra_args) File "/var/runtime/boto3/s3/transfer.py", line 651, in _put_object **extra_args) File "/var/runtime/botocore/client.py", line 228, in _api_call return self._make_api_call(operation_name, kwargs) File "/var/runtime/botocore/client.py", line 492, in _make_api_call raise ClientError(parsed_response, operation_name) ClientError: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
bucket_name
的格式为 abcd
,而 prefix
的格式为 a/b/c/d/
。我不确定错误是由于斜杠错误还是您可以通过某种方式在其他地方指定前缀,或者我没有写权限(尽管我应该有)。
此代码执行无任何错误:
for object in output_bucket.objects.filter(Prefix=prefix):
print(object.key)
虽然没有输出,因为桶是空的。
我假设您已完成所有这些设置:
- AWS 访问密钥 ID 和密钥设置(通常存储在
~/.aws/credentials
- 您可以访问 S3 并且知道您的存储桶名称和前缀(子目录)
根据 Boto3 S3 upload_file
documentation,您应该像这样上传:
upload_file(Filename, Bucket, Key, ExtraArgs=None, Callback=None, Config=None)
import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')
这里要注意的重点是s3.meta.client
。别忘了——它对我有用!
希望对您有所帮助。
原来我需要 SSE:
transfer = S3Transfer(s3_client)
transfer.upload_file('/tmp/hello.txt', bucket_name, prefix+'hello-remote.txt', extra_args={'ServerSideEncryption': "AES256"})
import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file( 'csv1.csv', "bucketname", "prefixna/csv1.csv")
以下是 John Adjei 的替代答案。这也取自Boto3 S3 upload_file documentation。因为客户端是 low-level(低抽象/更接近机器代码)它可以提高性能 - 特别是如果你处理大数据。
import boto3
s3 = boto3.client('s3')
with open("FILE_NAME", "rb") as f:
s3.upload_fileobj(f, "BUCKET_NAME", "OBJECT_NAME")
这是我的回答:
import boto3
s3_client = boto3.client(service_name='s3', region_name='ap-southeast-1',
aws_access_key_id='AWS_ACCESS_KEY_ID',
aws_secret_access_key='AWS_SECRET_ACCESS_KEY')
dest_bucket = 'data-lake'
dest_prefix = 'datamart/my_file_name/'
file_name = 'my_file_name'+ '.parquet'
s3.meta.client.delete_object(Bucket=dest_bucket,Key=dest_prefix + file_name)
我正在尝试将文件上传到 S3 存储桶,但我无权访问存储桶的根级别,我需要将其上传到某个前缀。以下代码:
import boto3
s3 = boto3.resource('s3')
open('/tmp/hello.txt', 'w+').write('Hello, world!')
s3_client.upload_file('/tmp/hello.txt', bucket_name, prefix+'hello-remote.txt')
给我一个错误:
An error occurred (AccessDenied) when calling the PutObject operation: Access Denied: ClientError Traceback (most recent call last): File "/var/task/tracker.py", line 1009, in testHandler s3_client.upload_file('/tmp/hello.txt', bucket_name, prefix+'hello-remote.txt') File "/var/runtime/boto3/s3/inject.py", line 71, in upload_file extra_args=ExtraArgs, callback=Callback) File "/var/runtime/boto3/s3/transfer.py", line 641, in upload_file self._put_object(filename, bucket, key, callback, extra_args) File "/var/runtime/boto3/s3/transfer.py", line 651, in _put_object **extra_args) File "/var/runtime/botocore/client.py", line 228, in _api_call return self._make_api_call(operation_name, kwargs) File "/var/runtime/botocore/client.py", line 492, in _make_api_call raise ClientError(parsed_response, operation_name) ClientError: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
bucket_name
的格式为 abcd
,而 prefix
的格式为 a/b/c/d/
。我不确定错误是由于斜杠错误还是您可以通过某种方式在其他地方指定前缀,或者我没有写权限(尽管我应该有)。
此代码执行无任何错误:
for object in output_bucket.objects.filter(Prefix=prefix):
print(object.key)
虽然没有输出,因为桶是空的。
我假设您已完成所有这些设置:
- AWS 访问密钥 ID 和密钥设置(通常存储在
~/.aws/credentials
- 您可以访问 S3 并且知道您的存储桶名称和前缀(子目录)
根据 Boto3 S3 upload_file
documentation,您应该像这样上传:
upload_file(Filename, Bucket, Key, ExtraArgs=None, Callback=None, Config=None)
import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')
这里要注意的重点是s3.meta.client
。别忘了——它对我有用!
希望对您有所帮助。
原来我需要 SSE:
transfer = S3Transfer(s3_client)
transfer.upload_file('/tmp/hello.txt', bucket_name, prefix+'hello-remote.txt', extra_args={'ServerSideEncryption': "AES256"})
import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file( 'csv1.csv', "bucketname", "prefixna/csv1.csv")
以下是 John Adjei 的替代答案。这也取自Boto3 S3 upload_file documentation。因为客户端是 low-level(低抽象/更接近机器代码)它可以提高性能 - 特别是如果你处理大数据。
import boto3
s3 = boto3.client('s3')
with open("FILE_NAME", "rb") as f:
s3.upload_fileobj(f, "BUCKET_NAME", "OBJECT_NAME")
这是我的回答:
import boto3
s3_client = boto3.client(service_name='s3', region_name='ap-southeast-1',
aws_access_key_id='AWS_ACCESS_KEY_ID',
aws_secret_access_key='AWS_SECRET_ACCESS_KEY')
dest_bucket = 'data-lake'
dest_prefix = 'datamart/my_file_name/'
file_name = 'my_file_name'+ '.parquet'
s3.meta.client.delete_object(Bucket=dest_bucket,Key=dest_prefix + file_name)