使用lambda函数添加s3对象标签?

时间:2018-04-11 14:06:54

标签: amazon-web-services amazon-s3 aws-lambda aws-sdk

该文档介绍了如何通过控制台tag an s3 object。我们如何使用lambda函数以编程方式执行此操作?

4 个答案:

答案 0 :(得分:4)

如果您在Lambda中使用JavaScript,则可以使用:s3.putObjectTagging

文档代码段

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

答案 1 :(得分:3)

您也可以使用ResourceGroupsTaggingAPI

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

答案 2 :(得分:0)

如果您使用AWS开发工具包(SDK)进行节点与S3的交互,只需将Tagging字段添加到要放在S3存储桶中的对象中即可。

// 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

答案 3 :(得分:0)

您可以创建一个能够自动标记 Amazon S3 存储桶中所有资产的 Lambda 函数。例如,假设您有这样的资产:

enter image description here

执行 Lambda 函数后,它会创建标签并将其应用于图像。

enter image description here

有关详细信息,请单击 Creating an Amazon Web Services Lambda function that tags digital assets located in Amazon S3 buckets