To generate a Presigned URL for a file upload to S3 + add tagging information

1

Hello AWS Support, Can we know does AWS SDK supports adding tagging information for uploading file via presigned URL, if so can you point out some documentation.

Anand
asked 7 months ago1570 views
2 Answers
0
Accepted Answer

Hello Anand,

Yes, you can use AWS SDK to generate a pre-signed URL to upload an object to a specified S3 bucket with metadata. What I'm not sure, and I haven't been able to test, is that you can set tags directly; but something I've used in the past is custom object metadata. I suggest you try first with the "x-amz-tagging" and if it doesn't let you set it, try with user-defined object metadata "x-amz-meta-yourcustomkey: yourcustomvalue". Please check the following documentation as a reference:

Please let me know if that works for your use-case; I'm eager to learn the solution you finally apply, even though, unfortunately, I don't have the time right now to help you troubleshoot as this is not the official AWS support and just a community resource.

If the answer is helpful, please click "Accept Answer" and upvote it.

profile picture
EXPERT
answered 7 months ago
profile picture
EXPERT
reviewed a month ago
  • I tried with x-amz-tagging but ran into this error " The request signature we calculated does not match the signature you provided. Check your key and signing method." I tried with new keys too but no luck

  • You might need to generate the pre-signed URL in the correct region (you should call the endpoint in the same region where your bucket lives) or have some unsupported characters in the key name. Can you try that and see if that fixes the issue?

  • Some more details. Here is what I tried:

        const s3Client = new S3Client({ region: AWS_REGION });
        const putObjectCommand = new PutObjectCommand({ Bucket: bucket, Key: s3_key, Tagging: 'tag=myTag' });
        return await getSignedUrl(s3Client, putObjectCommand);
    

    Result:

    h ttps://telemetry-upload.s3.us-east-2.amazonaws.com/filename?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAXGUCOVV4FKISXDJR%2F20231027%2Fus-east-2%2Fs3%2Faws4_request&X-Amz-Date=20231027T164533Z&X-Amz-Expires=900&X-Amz-Signature=699be84c8ccb32d35c7446a6b3f188cb4a63e60dd707f463bbedfc025c2afe2f&X-Amz-SignedHeaders=host&x-amz-tagging=tag%3DmyTag&x-id=PutObject

    await fetch(url, {
                        method: 'PUT',
                        body: reader.result,
                        headers: {
                            'Content-Type': 'application/octet-stream',
                            'x-amz-tagging': 'tag=mytag',
                        },
                    });
    

    Verified that the header on the http request: X-Amz-Tagging: tag=mytag is there.

    Result is the server does not return a response, connection is terminated. But when I remove the 'x-amz-tagging': 'tag=mytag' from my request the file is uploaded with a 200 response. No tag exists on the newly uploaded file.

0

I found a solution that worked for me.

    const s3Client = new S3Client({ region: AWS_REGION });
    const putObjectCommand = new PutObjectCommand({ Bucket: bucket, Key: s3_key, Tagging: 'tag=myTag'});
    const unhoistableHeaders : Set<string> =  new Set(['x-amz-tagging']);
    return await getSignedUrl(s3Client, putObjectCommand,{ unhoistableHeaders: unhoistableHeaders });

It appears that you need to add the x-amz-tagging header to the list of unhoistable headers (https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/requestpresigningarguments.html).

ChrisB
answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions