quantumpotato
quantumpotato

Reputation: 9767

Aws::S3::Errors::InvalidArgument (): on bucket.put_object

I'm trying to upload base64 encoded image to S3 bucket. My bucket list shows correctly after signing in. I'm trying to upload using the bucket#put_object

I don't see any hint as to what the invalid argument is.

My JSON body looks like.. ""product": { "product_photo": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAT8AAADQCAYAAABxw2ZIAAABQGlDQ1BJQ0MgUHJvZmlsZQAAKJFjYGASSCwoyGFhYGDIzSspCnJ3UoiIjFJgf8LAxsDMIAqEVonJxQWOAQE+QCUMMBoVfLvGwAiiL+uCzH ...."

and if I use the Carrierwave file storage, image saves fine.

How do I upload my base64 image using just the aws-sdk?

#Aws::S3::Bucket:0x00007fd60a2a8928 THat was the new bucket UPLOADING NOW Completed 500 Internal Server Error in 654ms (ActiveRecord: 17.3ms)

Aws::S3::Errors::InvalidArgument ():

app/controllers/products_controller.rb:139:in `supload'

  def supload(params)
    s3 = Aws::S3::Client.new(access_key_id: ENV['AWS_ACCESS_KEY_ID'].strip, secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'].strip) #call strip on the keys
    resp = s3.list_buckets
    puts resp
    
    bucket = resp[:buckets][0]

    s3Resource = Aws::S3::Resource.new

    # reference an existing bucket by name
    bucket = s3Resource.bucket('htm-product-photo')
    puts bucket
    puts "That was the new bucket"
   
    data = Base64.decode64(params[:product_photo].to_s)

    type = "image/png" #params[:contentType].to_s
    extension = ".png" #params[:extension].to_s
    name = ('a'..'z').to_a.shuffle[0..7].join # + ".#{extension}"
    puts "UPLOADING NOW"
    obj = bucket.put_object({body: data, key: name, content_type:type,acl:"public_read"})
    puts obj.errors
    puts "Tried to upload"
    # https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Bucket.html#put_object-instance_method
    url = obj.public_url().to_s
    url
  end

EDIT: With wire trace enabled per @D. SM

    #<Aws::S3::Bucket:0x00007ffa87738708>
THat was the new bucket
UPLOADING NOW
opening connection to htm-product-photo.s3.amazonaws.com:443...
opened
starting SSL for htm-product-photo.s3.amazonaws.com:443...
SSL established, protocol: TLSv1.2, cipher: ECDHE-RSA-AES128-GCM-SHA256
<- "PUT /ekjlaqbo HTTP/1.1\r\nContent-Type: image/png\r\nAccept-Encoding: \r\nUser-Agent: aws-sdk-ruby3/3.104.3 ruby/2.6.2 x86_64-darwin18 aws-sdk-s3/1.76.0\r\nX-Amz-Acl: public_read\r\nExpect: 100-continue\r\nContent-Md5: ThpRJ9rI1gkEr5zO+D4zOA==\r\nHost: htm-product-photo.s3.amazonaws.com\r\nX-Amz-Date: 20200816T221225Z\r\nX-Amz-Content-Sha256: f39e6d48f796085a4c601e08ce91be80a06fdffd22b0b3426b9eab95098fc003\r\nAuthorization: AWS4-HMAC-SHA256 Credential=AKIAUSPGIPMTP6S6J47A/20200816/us-east-1/s3/aws4_request, SignedHeaders=content-md5;content-type;host;user-agent;x-amz-acl;x-amz-content-sha256;x-amz-date, Signature=d30f1a30c6065f8d808cc238c0650c2c11799e55c2e22003571e43c90e021936\r\nContent-Length: 87850\r\nAccept: */*\r\n\r\n"
-> "HTTP/1.1 400 Bad Request\r\n"
-> "x-amz-request-id: B4DF8652D31666ED\r\n"
-> "x-amz-id-2: 32VYR2WsieoAOP2mOb27faxyhO1koISnl8ZerKLpIRQVIJE9dtZeQ3isluBzV042F1kBV7WgIEQ=\r\n"
-> "Content-Type: application/xml\r\n"
-> "Transfer-Encoding: chunked\r\n"
-> "Date: Sun, 16 Aug 2020 22:12:26 GMT\r\n"
-> "Connection: close\r\n"
-> "Server: AmazonS3\r\n"
-> "\r\n"
-> "139\r\n"
reading 313 bytes...
-> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>InvalidArgument</Code><Message></Message><ArgumentName>x-amz-acl</ArgumentName><ArgumentValue>public_read</ArgumentValue><RequestId>B4DF8652D31666ED</RequestId><HostId>32VYR2WsieoAOP2mOb27faxyhO1koISnl8ZerKLpIRQVIJE9dtZeQ3isluBzV042F1kBV7WgIEQ=</HostId></Error>"
read 313 bytes

Upvotes: 0

Views: 2382

Answers (2)

dinesh ygv
dinesh ygv

Reputation: 1890

The allowed options for :acl according to documentation are private, public-read, public-read-write, authenticated-read, aws-exec-read, bucket-owner-read, bucket-owner-full-control.

So, you have to change public_read to public-read

obj = bucket.put_object({body: data, key: name, content_type:type,acl:"public-read"})

Upvotes: 3

D. SM
D. SM

Reputation: 14520

Enable wire trace to see what is being sent to S3 and what the responses are.

Upvotes: 1

Related Questions