【问题标题】:Accessing Meta Data from AWS S3 with AWS Lambda使用 AWS Lambda 从 AWS S3 访问元数据
【发布时间】: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


【解决方案1】:

元数据不在事件中,而是在头对象中。

HEAD 操作从对象中检索元数据,而不返回对象本身。如果您只对对象的元数据感兴趣,此操作很有用。要使用 HEAD,您必须对对象具有 READ 访问权限。

HEAD 请求与对象上的 GET 操作具有相同的选项。除了没有响应正文之外,响应与 GET 响应相同。

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

下面的代码是获取元数据的sn-p。

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.

【讨论】:

  • 嗯,我似乎得到了空的元数据。我已经在我的对象上设置了 Cache-Control 元数据,但 response['Metadata'] 是空的。是否有指向您引用的那些文档的链接?
【解决方案2】:

您可以从必须传递包含存储桶和密钥的对象的头对象中获取元数据:- 例如:以下是您必须使用的代码(在 NodeJs 中),以便在从 aws-sdk 生成元数据时获取与 pre-signedUrl 一起附加的元数据。

//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;```

【讨论】:

  • 你从哪里得到 reqBody?是触发事件还是触发事件后对 s3 的调用?
  • @KingAndrew 我假设reqBody 是从S3 事件消息docs.aws.amazon.com/AmazonS3/latest/userguide/… 返回的数据
  • 我认为 OP 正在使用 Python 寻求答案
猜你喜欢
  • 2016-01-11
  • 2020-11-12
  • 2017-02-08
  • 2020-09-30
  • 2017-08-27
  • 2022-01-28
  • 2019-06-04
  • 2019-01-06
  • 2017-06-06
相关资源
最近更新 更多