Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 presigned URL with metadata

I am trying to create presigned-url using boto3 below

s3 = boto3.client(
    's3', 
    aws_access_key_id=settings.AWS_ACCESS_KEY, 
    aws_secret_access_key=settings.AWS_ACCESS_SECRET, 
    region_name=settings.AWS_SES_REGION_NAME,
    config=Config(signature_version='s3v4')
)
metadata = {
    'test':'testing'
}
presigned_url = s3.generate_presigned_url(
ClientMethod='put_object', 
Params={
    'Bucket': settings.AWS_S3_BUCKET_NAME,
    'Key': str(new_file.uuid),
    'ContentDisposition': 'inline',
    'Metadata': metadata
})

So, after the URL is generated and I try to upload it to S3 using Ajax it gives 403 forbidden. If I remove Metadata and ContentDisposition while creating URL it gets uploaded successfully.

Boto3 version: 1.9.33

Below is the doc that I referring to: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.generate_presigned_url

like image 956
Dhruvil Amin Avatar asked Dec 06 '18 10:12

Dhruvil Amin


2 Answers

Yes I got it working, Basically after the signed URL is generated I need to send all the metadata and Content-Dispostion in header along with the signed URL. For eg: My metadata dictionary is {'test':'test'} then I need to send this metadata in header i.e. x-amz-meta-test along with its value and content-dispostion to AWS

like image 163
Dhruvil Amin Avatar answered Oct 27 '22 23:10

Dhruvil Amin


I was using createPresignedPost and for me I got this working by adding the metadata I wanted to the Fields param like so :-

const params = {
    Expires: 60,
    Bucket: process.env.fileStorageName,
    Conditions: [['content-length-range', 1, 1000000000]], // 1GB
    Fields: {
        'Content-Type': 'application/pdf',
        key: strippedName,
        'x-amz-meta-pdf-type': pdfType,
        'x-amz-meta-pdf-id': pdfId,
    },
};

As long as you pass the data you want, in the file's metadata, to the lambda that you're using to create the preSignedPost response then the above will work. Hopefully will help someone else...

like image 27
Joe Keene Avatar answered Oct 27 '22 23:10

Joe Keene