简体   繁体   English

AWS boto3 如何从密钥中获取元数据?

[英]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我正在尝试获取在 s3 存储桶中上传的文件的元数据值

#i have to specifically use the boto3.resource('s3') for other api call in the project. #i 必须专门为项目中的其他 api 调用使用 boto3.resource('s3')。

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 .正在返回resource ,而不是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 .因为head_object()不是可以对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.它将提供元数据的字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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