Uploading a file to AWS S3 with ACL set to public_read
Asked Answered
S

1

12

In my Rails app I save customer RMA shipping labels to an S3 bucket on creation. I just updated to V2 of the aws-sdk gem, and now my code for setting the ACL doesn't work.

Code that worked in V1.X:

  # Saves label to S3 bucket
  s3 = AWS::S3.new
  obj = s3.buckets[ENV['S3_BUCKET_NAME']].objects["#{shippinglabel_filename}"]
  obj.write(open(label.label('pdf').postage_label.label_pdf_url, 'rb'), :acl => :public_read)

.write seems to have been deprecated, so I'm using .put now. Everything is working, except when I try to set the ACL.

New code for V2.0:

  # Saves label to S3 bucket
  s3 = Aws::S3::Resource.new
  obj = s3.bucket(ENV['S3_BUCKET_NAME']).object("#{shippinglabel_filename}")
  obj.put(Base64.decode64(label_base64), { :acl => :public_read })

I get an Aws::S3::Errors::InvalidArgument error, pointed at the ACL.

Silverside answered 18/2, 2015 at 18:58 Comment(3)
What's the full error message?Larrylars
Aws::S3::Errors::InvalidArgument is the bulk of the message. The console just points towards my Model at the obj.put line. Can post a screenshot if that would be helpful.Silverside
I was wondering if it was some hint about the error, such as the format of the argument or that not being allowed or such. No worries.Larrylars
P
22

This code works for me:

photo_obj = bucket.object object_name
photo_obj.upload_file path, {acl: 'public-read'}

so you need to use the string 'public-read' for the acl. I found this by seeing an example in object.rb

Phanerogam answered 26/2, 2015 at 6:31 Comment(4)
You rock - thanks! I can't believe it was just a hyphen instead of an underscore. I actually switched back to the old API because of this - but now I know how to upgrade. Thanks!Silverside
Ack! Same here -- I had an underscore, too. Double-checked the official docs three times without catching my error!Blanca
yep, the hyphen works, thank you. I'm using Python/boto3 and had the same problem, now mostly solved. still problem with mime-type, but I'll figure it out.Roofing
Why isn't this the accepted answer? Was stuck for hours until i stumbled across this answer.Fabyola

© 2022 - 2024 — McMap. All rights reserved.