Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading a file to AWS S3 with ACL set to public_read

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.

like image 689
bnzelener Avatar asked Feb 18 '15 18:02

bnzelener


1 Answers

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

like image 147
kevinw Avatar answered Nov 19 '22 08:11

kevinw