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

Make a file in s3 public using python and boto

我在下面有一些东西 link,当我尝试访问它时,它出现了一个 xml 文件,上面写着 "Acess denied".

我需要转到 aws 管理控制台并制作此 part-0000 文件 public 以便我可以访问它。

你知道我如何使用带有 python 的 boto 来授予权限,这样我就可以访问这个 link 而无需转到 aws managmet 控制台并制作文件 public 吗?

downloadLink = 'https://s3.amazonaws.com/myFolder/uploadedfiles/2015423/part-00000'

这应该会给你一个想法:

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')

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

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))

这似乎适用于 boto 2.42.0 和 Python 3

s3 = boto.connect_s3()
b = s3.get_bucket('brianray')
k = Key(b)
k.key = new_file_name
k.set_contents_from_filename(new_file_name)
k.set_acl('public-read')
k.generate_url(expires_in=0, query_auth=False)

Boto3 设置访问控制列表。好question/answers 这里。

bucket.Acl().put(ACL='public-read')
obj.Acl().put(ACL='public-read')

使用obj.Acl().put...在移动或操作物品时非常有用。如果 scripting/procedural.

特别有用

通过 https://boto3.readthedocs.io/en/latest/guide/migrations3.html#access-controls.