Ole
Ole

Reputation: 46890

Adding s3 object tags using a lambda function?

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

Answers (4)

smac2020
smac2020

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:

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.

Upvotes: 0

chiragsrvstv
chiragsrvstv

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

Michał Z.
Michał Z.

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

MaiKaY
MaiKaY

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

Related Questions