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

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

我正在尝试将 base64 编码的图像上传到 S3 存储桶。登录后我的存储桶列表正确显示。我正在尝试使用存储桶上传#put_object

我没有看到关于无效参数是什么的任何提示。

我的 JSON body 看起来像.. ““产品”: { "product_photo": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAT8AAADQCAYAAABxw2ZIAAABQGlDQ1BJQ0MgUHJvZmlsZQAAKJFjYGASSCwoyGFhYGDIzSspCnJ3UoiIjFJgf8LAxsDMIAqEVonJxQWOAQE+QCUMMBoVfLvGwAiiL+uCzH ...."

如果我使用 Carrierwave 文件存储,图像可以很好地保存。

如何仅使用 aws-sdk 上传我的 base64 图像?

#Aws::S3::Bucket:0x00007fd60a2a8928 那是新桶 正在上传 在 654 毫秒内完成 500 个内部服务器错误(ActiveRecord:17.3 毫秒)

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

app/controllers/products_controller.rb:139:在“上传”中

  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

编辑:根据@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

启用 wire trace 以查看发送到 S3 的内容以及响应。

根据 documentation:acl 允许的选项是 private, public-read, public-read-write, authenticated-read, aws-exec-read, bucket-owner-read, bucket-owner-full-control

因此,您必须将 public_read 更改为 public-read

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