我尝试通过执行以下操作将文件上传到S3:
r_response = requests.post(presigned_post["url"], json=presigned_post["fields"], files=files)
但我收到以下错误:
Bucket POST必须包含名为' key'的字段。如果已指定,请检查字段的顺序。
但我确实包含key
值。我看到的其他一个答案推荐使用{I}尝试做的OrderedDict
,但是通过下面的S3文档,我不知道它在哪里指定了所需的键,价值数据在提出请求时。
http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTForms.html http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-authentication-HTTPPOST.html
有人有什么建议吗?
Boto3按以下顺序返回包含元素值的字典:x-amz-signature
,x-amz-algorithm
,key
,x-amz-credential
,policy
和{{1我只是使用相同的字典。
x-amz-date
打印def get_signed_request(title, type, track_id, file):
S3_BUCKET = os.environ.get('S3_BUCKET')
file_name = title
file_type = type
region = 'us-east-1'
s3 = boto3.client('s3', region_name=region, config=Config(signature_version='s3v4'))
presigned_post = s3.generate_presigned_post(
Bucket = S3_BUCKET,
Key = file_name
)
files = {'file': file}
r_response = requests.post(presigned_post["url"], json=presigned_post["fields"], files=files)
的内容会显示密钥:
presigned_post
答案 0 :(得分:1)
我原本在做:
r_response = requests.post(presigned_post["url"], json=presigned_post["fields"], files=files)
我将json
更改为data
并且有效:
r_response = requests.post(presigned_post["url"], data=presigned_post["fields"], files=files)
不幸的是,我遇到了另一个错误:
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>