Make a file in s3 public using python and boto

▼魔方 西西 提交于 2019-12-21 06:59:07

问题


I have thins link below, and when I try to acess it it appears an xml file saying "Acess denied".

And I need to go to aws managment console and make this part-0000 file public so I can acess it.

Do you know how can I give permissions using boto with python so I can acess this link without needed to go to aws managmet console and make the file public?

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

回答1:


This should give you an idea:

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

In this case, public-read is one of the canned ACL policies supported by S3 and would allow anyone to read the file.




回答2:


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



回答3:


This seems to work with boto 2.42.0 and 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)



回答4:


Boto3 setting ACL. Good question/answers here.

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

Use of obj.Acl().put... is very helpful when moving or manipulating items. Especially helpful if scripting/procedural.

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



来源:https://stackoverflow.com/questions/29422549/make-a-file-in-s3-public-using-python-and-boto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!