Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a file in s3 public using python and boto

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'
like image 976
UserX Avatar asked Apr 02 '15 21:04

UserX


People also ask

How do I make my S3 bucket from private to public?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Bucket name list, choose the name of the bucket that you want. Choose Permissions. Choose Edit to change the public access settings for the bucket.

How do I create a S3 bucket with boto3?

How to create S3 bucket using Boto3? To create the Amazon S3 Bucket using the Boto3 library, you need to either create_bucket client or create_bucket resource. Note: Every Amazon S3 Bucket must have a unique name. Moreover, this name must be unique across all AWS accounts and customers.


2 Answers

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.

like image 126
garnaat Avatar answered Oct 20 '22 13:10

garnaat


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))
like image 14
Manish Mehra Avatar answered Oct 20 '22 14:10

Manish Mehra