3

We are trying to use aws S3 for storing files. We created a simple REST API in JAVA to upload and retrieve a file.

Clients requesting to update files use our REST API's which provide a presigned url to either PUT/GET the file. We are using AWS SDK for java to generate the pre signed urls.

We need to add some custom metadata to the files when they are being updated on S3. As we dont control the upload to S3 itself, is there a way we can add this information while we are generating the pre signed url? It wont be good to have the clients to provide this information as a part of their request headers.

luk2302
  • 55,258
  • 23
  • 97
  • 137
bh1210
  • 51
  • 1
  • 5

1 Answers1

9

We stumbled upon the same issue today and were trying to use

// does not work
request.putCustomRequestHeader(Headers.S3_USER_METADATA_PREFIX + "foo", "bar");

which unfortunately does not really work, it adds the metadata but the caller of the presigned url has to still provide the metadata using request headers which is something the client should not have to do.

Finally we found that using GeneratePresignedUrlRequest#addRequestParameter does the job marvellously:

GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest("bucket", "yourFile.ending");
request.addRequestParameter(Headers.S3_USER_METADATA_PREFIX + "foo", "bar"); 
// import com.amazonaws.services.s3.Headers; needed

The presigned url then looks something like

https://bucket.s3.region.amazonaws.com/yourFile.ending?x-amz-meta-foo=bar&X-Amz-Security-Token=...

The metadata can be clearly seen in the url, using Postman to PUT to that file using upload-file in the body creates the file with the correct metadata in the bucket and it is not possible for the client to change the meta-data because that would make the signature no longer match the request.

The only not-so-pretty part about this is having to specify the internal aws header prefix for user metadata.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • Your code is working properly for me. But it's set the blank value against metadata of specific file. Upload request consist all valid data but it's not going to save in s3 bucket. Please help me to solve this issue. – Radadiya Nikunj Aug 14 '18 at 13:48
  • 1
    Thanks.. Got the solution. AWS looking for all metadata keys in lower-case. If any Keys consist upper case character then it will store metadata with blank value. – Radadiya Nikunj Aug 14 '18 at 14:40
  • Worked great for me! – Alberto Coletta Apr 17 '20 at 17:17