Reputation: 46890
The documentation describes how to tag an s3 object via the console. How do we do it programmatically with a lambda function?
Upvotes: 4
Views: 6526
Reputation: 10704
You can create a Lambda function that is able to automatically tag all assets in an Amazon S3 bucket. For example, assume you have an asset like this:
After you execute the Lambda function, it creates tags and applies them to the image.
For more information, click Creating an Amazon Web Services Lambda function that tags digital assets located in Amazon S3 buckets.
Upvotes: 0
Reputation: 211
If you're using AWS SDK for node to interact with S3, it could be done by simply adding the Tagging
field to your object that would be put in the S3 bucket.
// Create an object to be sent to S3
var params = {
Body: <Binary String>,
Bucket: "examplebucket",
Key: "HappyFace.jpg",
Tagging: "key1=value1&key2=value2"
};
// Put the params object to s3
s3.putObject(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
console.log(data); // successful response
}
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html
Upvotes: 1
Reputation: 1392
You can also use ResourceGroupsTaggingAPI for that:
const resourcegroupstaggingapi = new AWS.ResourceGroupsTaggingAPI();
resourcegroupstaggingapi.tagResources({
ResourceARNList: [
'arn:aws:s3<...>',
'arn:aws:s3<...>'
],
Tags: {
'SomeTagKey': 'Some tag value',
'AnotherTagKey': 'Another tag value'
}
}, (err, result) => {
// callback
});
Upvotes: 4
Reputation: 4482
If you are using JavaScript in your Lambda you are good to use: s3.putObjectTagging
Documentation Snippet
/* The following example adds tags to an existing object. */
var params = {
Bucket: "examplebucket",
Key: "HappyFace.jpg",
Tagging: {
TagSet: [
{
Key: "Key3",
Value: "Value3"
},
{
Key: "Key4",
Value: "Value4"
}
]
}
};
s3.putObjectTagging(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
/*
data = {
VersionId: "null"
}
*/
});
Upvotes: 9