3
我想每次将对象上传到S3时检索一些我添加的元数据(使用控制台x-amz-meta-my_variable)。使用AWS Lambda从AWS S3访问元数据
我已经建立了拉姆达通过控制台来触发每一个对象被上传到我的桶
我想知道如果我可以使用类似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事件的任何权威的文档。我的建议是记录事件,然后检查日志,看看你想要的信息是否包含在事件中。 – garnaat
有关S3事件包含的文档可以在这里找到:https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html。这是一个耻辱,元数据不包括:( – tramwaj29