ruby-on-railsamazon-web-servicesamazon-s3pre-signed-urlaws-sdk-ruby

How to upload an object using presigned URL along with tags in s3 aws-sdk-ruby v3


I am trying to upload an object using a presigned URL. But I want to upload the the object along with tags. What is the proper way to do it?

Approach 1:

I tried the following ruby code:

signer = Aws::S3::Presigner.new
signer.presigned_url(:put_object, bucket: bucket_name, key: url, tagging: 'taggingName=tagValue')

But this is only uploading the object, but not adding tags.

Approach 2:

I tried to whitelist 'x-amz-tagging' header and tried to upload the file along with the header from client side.

Ruby code:

signer = Aws::S3::Presigner.new
url = signer.presigned_url(:put_object, bucket: bucket_name, key: public_url, whitelist_headers: ['x-amz-tagging'])

Client Side code:

return $http({
      method: 'PUT',
      url: presigned_url,
      ignoreLoadingBar: true,
      data: file,
      headers: {
        'Content-Type': file.type,
        'x-amz-tagging': 'taggingName=tagValue'
      }
    })

But this is throwing me an error while uploading saying "x-amz-tagging" is not signed.

Am using aws-sdk-ruby v3 (Ror)


Solution

  • As per the documentation

    The tag-set for the object. The tag-set must be encoded as URL Query parameters. (For example, "Key1=Value1")

    And the error is seems to be saying the same thing

    "x-amz-tagging" is not signed.

    So when you create the URL you need to provide what kind of tags with corresponding values.

        PUT /example-object HTTP/1.1
        Host: example-bucket.s3.<Region>.amazonaws.com   
        Accept: */*   
        Authorization:authorization string   
        Date: Thu, 22 Sep 2016 21:58:13 GMT   
        x-amz-tagging: tag1=value1&tag2=value2
    
        [... bytes of object data]   
         
    

    Example 6th on the same documentation page.

    Whatever tags you have provided while creating the signed url, you extract the same from url and pass them to x-amz-tagging

    something like

         const tag = signS3URL.tag;
         ...
         const options = {
            url: signedUrl,
            path: fileUrl,
            method: "PUT",
            headers: { "Content-Type": file.mimeType, "X-Amz-Tagging": tag }
        };
      
    

    I am not too good with JS though.

    Last but not least check your cloudtrail logs for errors for tagging the object.