Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon s3 如何在使用boto3 create_presigned_post时添加元数据?_Amazon S3_Metadata_Boto3_Http Status Code 403_Pre Signed Url - Fatal编程技术网

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

Amazon s3 如何在使用boto3 create_presigned_post时添加元数据?,amazon-s3,metadata,boto3,http-status-code-403,pre-signed-url,Amazon S3,Metadata,Boto3,Http Status Code 403,Pre Signed Url,要将自定义元数据添加到我使用boto3中的create\u presigned\u post上传的文件中。我正在运行以下代码,但得到403响应。下面的代码是借用自。我做错什么了吗 def create_presigned_post(bucket_name, object_name, fields=None, conditions=None, expiration=3600): # Generate a presigned S3 POS

要将自定义元数据添加到我使用boto3中的
create\u presigned\u post
上传的文件中。我正在运行以下代码,但得到403响应。下面的代码是借用自。我做错什么了吗

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}')
,字段字典将不会自动添加到条件列表中。还必须为元素指定一个条件

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

它应该可以工作:)

谢谢!它就像一个符咒我不是从文件中得到的。我认为条件是过滤一些字段。