想要将自定义元数据添加到我上传的文件中create_presigned_post
来自boto3。我正在运行以下代码,但收到 403 响应。下面的代码借用自here https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html#generating-a-presigned-url-to-upload-a-file。难道我做错了什么?
def create_presigned_post(bucket_name, object_name,
fields=None, conditions=None, expiration=3600):
# Generate a presigned S3 POST URL
s3_client = boto3.client('s3')
try:
response = s3_client.generate_presigned_post(bucket_name,
object_name,
Fields=fields,
Conditions=conditions,
ExpiresIn=expiration)
except ClientError as e:
print(e)
return None
# The response contains the presigned URL and required fields
return response
# Generate a presigned S3 POST URL
object_name = 'test-file.txt'
response = create_presigned_post('temp', object_name, fields={'x-amz-meta-test_key': 'test_val'})
# Demonstrate how another Python program can use the presigned URL to upload a file
with open('test-file.txt', 'rb') as f:
files = {'file': (object_name, f)}
http_response = requests.post(response['url'], data=response['fields'], files=files)
# If successful, returns HTTP status code 204
print(f'File upload HTTP status code: {http_response.status_code}')
根据文件 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.generate_presigned_post,字段字典不会自动添加到条件列表中。您还必须指定元素的条件。
response = create_presigned_post(bucket_name, object_name, fields={'x-amz-meta-test_key': 'test-val'}, conditions=[{'x-amz-meta-test_key': 'test-val'}])
它应该有效:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)