【发布时间】:2016-06-15 02:49:16
【问题描述】:
每次我将对象上传到 S3 时,我都想检索一些我添加的元数据(使用控制台 x-amz-meta-my_variable)。
我已经通过控制台设置了 lambda 以在每次将对象上传到我的存储桶时触发
我想知道是否可以使用variable = event['Records'][0]['s3']['object']['my_variable'] 之类的东西来检索这些数据,或者我是否必须使用存储桶和密钥连接回 S3,然后调用一些函数来检索它?
下面是代码:
from __future__ import print_function
import json
import urllib
import boto3
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
# variable = event['Records'][0]['s3']['object']['my_variable']
try:
response = s3.get_object(Bucket=bucket, Key=key)
# Call some function here?
print("CONTENT TYPE: " + response['ContentType'])
return response['ContentType']
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e
【问题讨论】:
-
我没有找到任何权威文档,说明发送到 Lambda 函数的 S3 事件中包含的确切内容。我的建议是记录事件,然后检查日志以查看事件中是否包含您想要的信息。
-
可以在此处找到有关 S3 事件中包含的内容的文档:docs.aws.amazon.com/AmazonS3/latest/dev/…。很遗憾没有包含元数据:(
标签: python amazon-s3 aws-lambda boto3