我正在尝试将文件上传到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)
虽然没有输出,因为存储桶为空。
原来我需要SSE:
transfer = S3Transfer(s3_client)
transfer.upload_file('/tmp/hello.txt', bucket_name, prefix+'hello-remote.txt', extra_args={'ServerSideEncryption': "AES256"})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句