2016-03-02 428 views
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 
+2

我還沒有找到什麼被包括在發送給lambda函數S3事件的任何權威的文檔。我的建議是記錄事件,然後檢查日誌,看看你想要的信息是否包含在事件中。 – garnaat

+0

有關S3事件包含的文檔可以在這裏找到:https://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html。這是一個恥辱,元數據不包括:( – tramwaj29

回答

1

元數據是未在事件,但在頭對象。

HEAD操作從對象中檢索元數據而不返回對象本身。如果您只對對象的元數據感興趣,此操作很有用。要使用HEAD,您必須具有對該對象的讀取權限。

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']) 

輸出:

[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.