11

Tye AWS.S3.ManagedUpload object allows you to upload an object specifying "tags", but I don't see any documentation for specifying meta data on an uploaded object. Is this possible? If so, how?

Mitch
  • 989
  • 1
  • 9
  • 25
  • Have you tried [this](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketTagging-property) – Alexander Vitanov Jul 24 '18 at 22:29
  • @AlexanderVitanov thanks for that link, but that adds tags to a bucket. I am looking for a way to add metadata to an uploaded object using AWS.S3.ManagedUpload, either when the object is uploaded or afterwards. – Mitch Jul 25 '18 at 13:42

1 Answers1

18

Try with the following:

const params = {
  Metadata: {
    'my-key': 'some-value',
  },
}
s3.upload(params, function(err, data) {
  console.log(err, data);
});

Note that the SDK will add the prefix x-amz-meta-

Harry Smith
  • 125
  • 7
Alexander Vitanov
  • 4,074
  • 2
  • 19
  • 22
  • 6
    That works thank you. However, when I use the x-amz-meta prefix as shown and look at the metadata in the AWS console, that prefix is doubled in the Key. Using your example above, the key shows as x-amz-meta-x-amz-meta-my-key. I can see in the browser network debugger that it is that way in the request headers also. Perhaps the SDK is adding that prefix for me, so I don't need to in the Metadata hash? Testing without that prefix in the hash, it still shows up in the AWS console with that prefix - once, not twice. – Mitch Jul 25 '18 at 18:45
  • 1
    You are right - no need to add it as the sdk does it for you. Nevertheless, it must be present. – Alexander Vitanov Jul 25 '18 at 21:09
  • 2
    x-amz-meta- does not need to be present in your params Metadata definition. Adding only 'my-key': 'some-value' to the definition will result in a metatdata key of x-amz-meta-my-key being present on your object in s3. – LWSChad Oct 15 '19 at 19:48
  • 3
    There's no need specifying `x-amz-meta-` prefix manually, as SDK will add it automatically. Keep in mind though that keys MUST BE LOWERCASE (as AWS will store them lowercase anyways https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#object-metadata) also ideally those should be ascii characters. If you have non-ascii chars you may get missing metadata (but keep your eye on ` x-amz-missing-meta` header in this case). – Dimitry K Dec 15 '19 at 18:01
  • 2
    While this code allows me to set a user defined meta header "Content-Type" in S3, it does not change the system defined meta header. Is there a function call in Javascript AWS version 3 that will let me change the System defined "Content-Type" meta header and set a System defined "Content-Disposition" meta header? – Davie Overman Jul 11 '21 at 16:41
  • 1
    @DavieOverman in the available params settings for S3 uploads there are ContentType and ContentDisposition that will set the "System Defined" metas: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html – Aaron McKeehan May 30 '22 at 15:52