Home > Enterprise >  AWS boto3 how to get the metadata from the key?
AWS boto3 how to get the metadata from the key?

Time:10-12

I am trying to get the metadata value of the file uploaded in the s3 bucket

#i have to specifically use the boto3.resource('s3') for other api call in the project.

i have below data available under the metadata field

#metadata


Key=Content-Type
Value= application/json

below are the code

bucket= 'mybucket'
key='L1/input/file.json'
s3_resource = boto3.resource('s3')
object = s3_resource.Object(bucket,key)
metadata = object.metadata

but iam getting below error

[ERROR] ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden

can anyone help me on this.

CodePudding user response:

Be careful of your syntax. This line:

s3_client=boto3.resource('s3')

is returning a resource, not a client.

Therefore, this line is failing:

obj = s3_client.head_object(bucket,key)

because head_object() is not an operation that can be performed on a resource.

Instead, use:

s3_resource = boto3.resource('s3')
object = s3_resource.Object('bucket_name','key')
metadata = object.metadata

It will provide a Dictionary of the metadata.

  • Related