Reputation: 41
I need to upload a csv diretly to AWS S3 with public access.
My current code is:
import boto3
s3 = boto3.resource('s3', aws_access_key_id='xxx', aws_secret_access_key='yyy')
s3.Bucket('test1234542').upload_file('C:/Users/output1.csv', 'output1.csv')
Unfortunately the permission is private and i dont know who to change to code to upload it with public access directly.
At the moment i have to go manually to the bucket, click on the folder "Permissions",click on "Public Access" and then make a tick at "Read object".
Does someone know a python code to add the public access permission?
Best Michi
Upvotes: 2
Views: 1560
Reputation: 97
Provide the ACL argument in your put_object statement and the uploaded object will be public. Refer to this documentation.
client.put_object(
ACL='public-read',
Body='file that is to be uploaded',
Bucket='name of the bucket',
Key='obejct key'
)
Upvotes: 0
Reputation: 61
When you upload a file in AWS S3, by default its private.
In order to give public access to the file, first change the public access options of the bucket to allow the changes made to its objects.
change public access settings of the bucket
code to upload file with public access :
import boto3
s3 = boto3.resource('s3')
s3.Bucket('bucket_name').upload_file('data.csv', 'data.csv')
file_object = s3.Bucket('bucket_name').Object('data.csv')
print(file_object.Acl().put(ACL = 'public-read'))
Hope It helps !!
Upvotes: 1
Reputation: 181
You could use this to make the S3 bucket public. Make sure your EC2 instance has the correct instance-profile attached and it has the correct permissions to interact with S3( Possible s3 full access). Use the following link and let me know if it helps!
Upvotes: 0