How to mock S3 putObject() with user metadata?

3.7k Views Asked by At

I need to mock AWS S3 when using putObject().

When calling the function I need to create the file with user metadata values. I tried to find some code examples over the web, but I found only this base code:

var AWSMock = require('mock-aws-s3');
AWSMock.config.basePath = '/tmp/buckets/' // Can configure a basePath for your local buckets
var s3 = AWSMock.S3({
    params: { Bucket: 'example' }
});
s3.putObject({Key: 'sea/animal.json', Body: '{"is dog":false,"name":"otter","stringified object?":true}'}, function(err, data) {
    s3.listObjects({Prefix: 'sea'}, function (err, data) {
        console.log(data);
    });
});

Unfortunately, it's not includes the user metadata map.

1

There are 1 best solutions below

0
On BEST ANSWER

In the parameters you're passing to the putObject() function, include a Metadata key which contains key/value pairs of the metadata you want to store with the S3 object.

Example:

s3.putObject({
    Key: 'sea/animal.json',
    Metadata: {
        MyKey: 'MyValue',
        MyKey2: 'MyValue2'
    },
    Body: '{"is dog":false,"name":"otter","stringified object?":true}'
}, function (err, data) { 
    // ...
});

See: putObject - Class: AWS.S3 — AWS SDK for JavaScript