Boto3无法从名称中包含时间戳的文件中读取元数据



我正在尝试使用boto3从s3桶读取附加的元数据内容

key = "myfile_20401__2021-03-04_16:33:12.597"
aws_s3_client=boto3.client("s3")
def get_s3_metadata(key):
bucket="my_bucket_name"
s3_client=aws_s3_client()
s3_response = s3_client.get_object(Bucket=bucket, Key=key)
metadata = s3_response.get("Metadata")

get_s3_metadata(key)

但是上面的代码返回以下错误

[ERROR] ClientError: An error occurred (AccessDenied) when calling the GetObject operation: Access Denied
File "/var/runtime/botocore/client.py", line 386, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 705, in _make_api_call
raise error_class(parsed_response, operation_name)
经分析,这不是由于访问问题引起的,而是由于文件名引起的。因此,当我从文件中删除时间戳时,它可以工作。

所以有没有办法我们可以解决这个问题,而不改变文件名(包括时间戳)?如果有人能帮上忙,我很感激。

S3对象键包含字符(冒号),这些字符在S3中使用是不安全的。幸运的是,这很容易解决,因为使用冒号只需要对它们进行urlencode:

from urllib.parse import quote
import boto3

def get_s3_metadata(key):
bucket="my_bucket_name"
s3_client=boto3.client("s3")
s3_response = s3_client.get_object(Bucket=bucket, Key=quote(key))
metadata = s3_response.get("Metadata")

key = "myfile_20401__2021-03-04_16:33:12.597"
get_s3_metadata(key)

最新更新