简体   繁体   中英

AWS boto3 how to get the metadata from the key?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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