简体   繁体   English

使用lambda更新amazon s3对象元数据而不执行对象复制?

[英]Update amazon s3 object meta data with lambda without performing a object copy?

Is it possible to add or update an s3 objects metadata with a lambda function without making a copy of the object? 是否可以使用lambda函数添加或更新s3对象元数据而无需复制对象? This 2 year old post says that we do need to make a copy , but its 2 years old, so maybe things have changed? 这个2岁的帖子说我们确实需要复制一份 ,但是2岁了,所以也许情况发生了变化?

In one sense, nothing has changed, fundamentally, because objects are immutable, and metadata is part of the object. 从某种意义上说,从根本上说,没有任何改变,因为对象是不可变的,元数据是对象的一部分。 To "change" an immutable object requires a copy and replace. 要“更改”不可变对象,需要复制和替换。

However , S3 objects now support tagging, which is a different class of "metadata," attached to -- rather than part of -- the object. 但是 ,S3对象现在支持标记,标记是一个不同的“元数据”类,附加到 - 而不是 - 对象的一部分。

S3 Object Tagging – You can associate multiple key-value pairs (tags) with each of your S3 objects, with ability to change them at any time. S3对象标记 - 您可以将多个键值对(标记)与每个S3对象相关联,并可以随时更改它们。 The tags can be used to manage and control access, set up S3 Lifecycle policies, customize the S3 Analytics, and filter the CloudWatch metrics. 这些标签可用于管理和控制访问,设置S3生命周期策略,自定义S3 Analytics以及过滤CloudWatch指标。 You can think of the bucket as a data lake, and use tags to create a taxonomy of the objects within the lake. 您可以将桶视为数据湖,并使用标签创建湖中对象的分类。 This is more flexible than using the bucket and a prefix, and allows you to make semantic-style changes without renaming, moving, or copying objects. 这比使用存储桶和前缀更灵活,并允许您在不重命名,移动或复制对象的情况下进行语义样式更改。

— S3 Storage Managment Update, 2017-03-20 - S3存储管理更新,2017-03-20

See also Difference Between Object Tags and Object Metadata? 另请参阅对象标签和对象元数据之间的区别?

use s3.headObject method and setup corresponding metadata via object.Metadata 使用s3.headObject方法和设置通过相应的元数据object.Metadata

I create lambda and subscribe it to event when new object is created: 我创建lambda并在创建新对象时将其订阅到事件:

console.log('Loading function');

const aws = require('aws-sdk');

const s3 = new aws.S3({ apiVersion: '2006-03-01' });


exports.handler = async (event, context, callback) => {
    console.log('Received event:', JSON.stringify(event, null, 2));

    const bucket = event.Records[0].s3.bucket.name;
    const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
    var params = {
        Bucket: bucket,
        Key: key,
        CopySource: encodeURIComponent( bucket+"/"+key ),
        Metadata: {
            mode: '33188',
        },
        MetadataDirective: 'REPLACE',
    };
    try {
        var data =  await s3.copyObject(params).promise();
        console.log('UPDATE SUCCESS', data);
    } catch (err) {
        console.log(err);
        const message = `Error updating object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.`;
        console.log(message);
        throw new Error(message);
    }
};

在此输入图像描述

WARNING Do not forget this: 警告不要忘记这一点:

在此输入图像描述

Otherwise you will fall into infinite loop 否则你将陷入无限循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM