使用python和boto在s3中创建一个文件

Use*_*erX 19 amazon-s3 boto amazon-web-services

我有下面的链接,当我尝试访问它时,它出现一个xml文件说"Acess denied".

我需要去aws管理控制台并将此part-0000文件公开,以便我可以访问它.

你知道我如何使用boto与python授予权限,这样我就可以访问这个链接而无需转到aws managmet控制台并使文件公开?

downloadLink = 'https://s3.amazonaws.com/myFolder/uploadedfiles/2015423/part-00000'
Run Code Online (Sandbox Code Playgroud)

gar*_*aat 29

这应该给你一个想法:

import boto.s3
conn = boto.s3.connect_to_region('us-east-1')  # or region of choice
bucket = conn.get_bucket('myFolder')
key = bucket.lookup('uploadedfiles/2015423/part-00000')
key.set_acl('public-read')
Run Code Online (Sandbox Code Playgroud)

在这种情况下,public-read是S3支持的一个固定ACL策略,允许任何人读取该文件.

  • 是的,该代码片段适用于boto.不是boto3. (5认同)

Man*_*hra 11

from boto3.s3.transfer import S3Transfer
import boto3

# ...
# have all the variables populated which are required below

client = boto3.client('s3', aws_access_key_id=access_key,
                      aws_secret_access_key=secret_key)
transfer = S3Transfer(client)
transfer.upload_file(filepath, bucket_name, folder_name+"/"+filename)
response = client.put_object_acl(ACL='public-read', Bucket=bucket_name, Key="%s/%s" % (folder_name, filename))
Run Code Online (Sandbox Code Playgroud)