4

The documentation describes how to tag an s3 object via the console. How do we do it programmatically with a lambda function?

1
  • A new AWS article that shows you how to perform this use case will be released soon. This use case is implemented using the Java Lambda runtime API. It automatically generates tags and applies them to objects in a given S3 bucket. I will post the link here soon.
    – smac2020
    Mar 31, 2021 at 15:02

4 Answers 4

9

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"
   }
   */
 });
4

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
});
1

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

0

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:

enter image description here

After you execute the Lambda function, it creates tags and applies them to the image.

enter image description here

For more information, click Creating an Amazon Web Services Lambda function that tags digital assets located in Amazon S3 buckets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.