简体   繁体   English

使用AWS Lambda从AWS S3访问元数据

[英]Accessing Meta Data from AWS S3 with AWS Lambda

I would like to retrieve some meta data I added (using the console x-amz-meta-my_variable) every time I upload an object to S3. 每次我将对象上传到S3时,我想检索一些我添加的元数据(使用控制台x-amz-meta-my_variable)。

I have set up lambda through the console to trigger every time an object is uploaded to my bucket 每次将对象上传到我的存储桶时,我都会通过控制台设置lambda

I am wondering if I can use something like variable = event['Records'][0]['s3']['object']['my_variable'] to retrieve this data or if I have to connect back to S3 with the bucket and key and then call some function to retrieve it? 我想知道我是否可以使用像variable = event['Records'][0]['s3']['object']['my_variable']来检索这些数据,或者我是否必须连接回S3桶和密钥然后调用一些函数来检索它?

Below is the code: 以下是代码:

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

The metadata is not in the event but in the head object. 元数据不在事件中,而是在头对象中。

The HEAD operation retrieves metadata from an object without returning the object itself. HEAD操作从对象检索元数据而不返回对象本身。 This operation is useful if you are interested only in an object's metadata. 如果您只对对象的元数据感兴趣,则此操作很有用。 To use HEAD, you must have READ access to the object. 要使用HEAD,您必须具有对该对象的READ访问权限。

A HEAD request has the same options as a GET operation on an object. HEAD请求与对象上的GET操作具有相同的选项。 The response is identical to the GET response except that there is no response body. 响应与GET响应相同,只是没有响应主体。

s3.head_object(Bucket=bucket, Key=key) s3.head_object(Bucket = bucket,Key = key)

Below code is a snippet to get the metadata. 下面的代码是获取元数据的代码段。

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

Output: 输出:

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

You can get the meta-data from the head object where you have to pass an object which contains bucket and key:- Eg : Below is a code(in NodeJs) that you have to use in order to get the meta-data which was attached with the pre-signedUrl while generating it from the aws-sdk. 您可以从头对象获取元数据,您必须传递包含存储桶和密钥的对象: - 例如:下面是您必须使用的代码(在NodeJ中)才能获取元数据附加了pre-signedUrl,同时从aws-sdk生成它。

//for generating pre-signed url with meta data
exports.getSignedUrl = async (myKey, metadata) => {
  const signedUrlExpireSeconds = 20000;
  const params = {
    Bucket: BUCKET,
    Key: myKey,
    Expires: signedUrlExpireSeconds,
    /* ACL: 'bucket-owner-full-control', ContentType:'image/jpeg', */
    ContentType: 'image/jpeg',
    ACL: 'public-read',
    Metadata: metadata,
  };
  const url = await s3.getSignedUrl('putObject', params);
  return url;
};
//for obtainig the meta data for the bucket and key
    const s3Object = reqBody.Records[0].s3;
    const bucketName = s3Object.bucket.name;
    const objectKey = s3Object.object.key;

    const params = {
      Bucket: bucketName,
      Key: objectKey,
    };
    const data = await s3.headObject(params).promise();
    const metadata = (!data) ? null : data.Metadata;```

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

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