简体   繁体   English

将文件上传到S3并触发Lambda的最佳实践

[英]best practice to upload file to S3 and trigger a lambda

I would like to implement the following scenario using AWS S3 and Lambda. 我想使用AWS S3和Lambda实施以下方案。

User takes a photo from mobile, add description, location and more attributes as a separate metadata. 用户从移动设备拍摄照片,添加描述,位置和更多属性作为单独的元数据。 Upload the image file to S3 with the metadata as the file inner metadata. 使用元数据作为文件内部元数据将图像文件上传到S3。 According to that link I can do it by adding a key value pair to ObjectMetadata with "x-amz-meta-" as a key. 根据链接,我可以通过使用“ x-amz-meta-”作为键将一个键值对添加到ObjectMetadata来实现。

I was managed to invoke the lambda every creation of a file in my S3 bucket but in the S3Event from the Lambda's invocation method I'm not getting the metadata that I putted in the ObjectMetadata. 我设法在S3存储桶中每次创建文件时都调用lambda,但是在Lambda的调用方法的S3Event中,我没有得到放置在ObjectMetadata中的元数据。

Code sample for the upload process: 上传过程的代码示例:

PutObjectRequest por = new PutObjectRequest(BUCKET_NAME, KEY_NAME, file);
ObjectMetadata metadata = new ObjectMetadata();
metadata.addUserMetadata("x-amz-meta-xxxxx", "test123");
por.setMetadata(metadata);
s3client.putObject(por);

I hope that I provided enough information 我希望我提供了足够的信息

AWS recommends to store files on S3, files' metadata on the DynamoDB. AWS建议将文件存储在S3上,将文件的元数据存储在DynamoDB上。 With this approach easy and cost effective to perform a search by the metadata. 通过这种方法,可以轻松且经济高效地执行元数据搜索。 Please keep in mind, that according to S3 pricing model you pay for each API call. 请记住,根据S3定价模型,您需要为每个API调用付费。

You can add metadata during the upload. 您可以在上传期间添加元数据。 I used the upload_file but putObject should work as well: 我使用了upload_fileputObject应该也可以正常工作:

s3client.putobject(por, ExtraArgs={"Metadata":metadata})

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

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