Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I have set up lambda through the console to trigger every time an object is uploaded to my bucket

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?

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
like image 647
Y Anderson Avatar asked Mar 02 '16 10:03

Y Anderson


1 Answers

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.

//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;```
like image 63
Nishant Dwivedi Avatar answered Sep 28 '22 04:09

Nishant Dwivedi