amazon-s3 - 如何在使用 boto3 create_presigned_post 时添加元数据?

标签 amazon-s3 metadata boto3 http-status-code-403 pre-signed-url

想要将自定义元数据添加到我使用来自 boto3 的 create_presigned_post 上传的文件。我正在运行以下代码,但收到 403 响应。下面的代码是从here借来的.我做错了什么吗?

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}')

最佳答案

As per document , 字段字典不会自动添加到条件列表中。您还必须为元素指定条件。

response = create_presigned_post(bucket_name, object_name, fields={'x-amz-meta-test_key': 'test-val'}, conditions=[{'x-amz-meta-test_key': 'test-val'}])

它应该可以工作:)

关于amazon-s3 - 如何在使用 boto3 create_presigned_post 时添加元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62961099/

相关文章:

c# - 如何以编程方式区分 epub 固定布局和可重排布局

python - 在 DynamoDB python boto3 中执行 update_item 时出错

python - S3 对象和 ObjectSummary 之间有什么区别?

python - 如何将标签添加到 boto (Amazon S3) 中的 key ?

amazon-web-services - 如何按修改日期列出 Amazon S3 存储桶内容?

amazon-web-services - Golang上传Http请求FormFile到Amazon S3

java - JPEGMetadata 无法解析为类型

java - 如何在 Android 中读取/写入自定义 PNG 元数据?

node.js - 如何在 Multer-s3 中使用动态存储桶名称进行文件上传

apache-spark - 使用2.8.0 hadoop aws jar时无法使用spark从s3读取,使用hadoop aws 2.7.3时无法将delta表写入s3