Y A*_*son 5 python amazon-s3 boto3 aws-lambda
每次将对象上传到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
Run Code Online (Sandbox Code Playgroud)
元数据不是在事件中,而是在head对象中。
HEAD操作从对象检索元数据,而不返回对象本身。如果您仅对对象的元数据感兴趣,则此操作很有用。要使用HEAD,您必须具有对该对象的READ访问权限。
HEAD请求具有与对对象的GET操作相同的选项。除了没有响应主体外,该响应与GET响应相同。
s3.head_object(桶=水桶,钥匙=钥匙)
下面的代码是获取元数据的代码段。
from __future__ import print_function
import boto3, logging
s3 = boto3.client('s3')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
for record in event['Records']
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
response = s3.head_object(Bucket=bucket, Key=key)
logger.info('Response: {}'.format(response))
print("Author : " + response['Metadata']['author'])
print("Description : " + response['Metadata']['description'])
Run Code Online (Sandbox Code Playgroud)
输出:
[INFO] 2016-05-18T01:30:47.900Z 241f0cfc-1c98-12e6-b9a7-cf406f32a0dc Response: {u'AcceptRanges': 'bytes', u'ContentType': 'binary/octet-stream', 'ResponseMetadata': {'HTTPStatusCode': 200, 'HostId': 'K8JMVbEt5xA+qXuXOedb1y5nxuv6scMXnNH/rHVtxcg=', 'RequestId': 'D05BE92E55E0'}, u'LastModified': datetime.datetime(2016, 5, 17, 22, 54, 37, tzinfo=tzutc()), u'ContentLength': 94320, u'ETag': '"0e4d457d912bce9ff81952"', u'Metadata': {'author': 'Satyajit Ray', 'description':'He was an Indian filmmaker, widely regarded as one of the greatest filmmakers of the 20th century.'}}
Author : Satyajit Ray
Description : He was an Indian filmmaker, widely regarded as one of the greatest filmmakers of the 20th century.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12609 次 |
| 最近记录: |