通过 CLI 为 PUT 使用 CURL 的 S3 签名 URL?

0

【以下的问题经过翻译处理】 文档说 CLI 命令“aws s3 presign”返回“GET”的 URL。网络上的许多帖子声称他们已经能够使用带有这些 URL 的 curl 上传 (PUT)。我一直无法复制这个。文档有时会过时。任何人都可以使用由 CLI 生成的 签名 URL 来确认 PUTNOT 可能的吗。或者,有人可以提供一个规范的、最近的、能够做到这一点的例子。

profile picture
专家
已提问 5 个月前56 查看次数
1 回答
0

【以下的回答经过翻译处理】 根据 CLI 的文档,生成的预签名 URL 仅可用于“GET”。

当我需要 POST 时,我保存了这个URL;如果您需要 PUT,您应该能够修改它以使用 generate_presigned_url() 而不是 generate_presigned_post()。请注意,这将 boto3 库与 Python 结合使用,因此您需要安排让您的 Python 安装可以访问该库(例如,pip install boto3)。输出包括为 curl 和 httpie 格式化的值,我希望它们有用。

获取预签名 POST URL 的 Python 脚本:

import boto3 client = boto3.client("s3")

get presigned info

note other params can be passed here, including expiration

see https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.generate_presigned_post

info = client.generate_presigned_post(Bucket="some-bucket", Key="filename.txt")

print the "fields" out in the right format to feed to httpie

print(" ".join(f"{k}='{v}'" for (k, v) in info["fields"].items()))

for curl, use this

print(" ".join(f"-F {k}='{v}'" for (k, v) in info["fields"].items()))

confirm the URL

print(info["url"])

下面是使用返回值的示例。您可能需要删除注释行才能让您的 shell 有效。

与 httpie 一起使用:

http --form -v \

this the the URL from Python output

https://some-bucket.s3.amazonaws.com/ \

these are the copy/pasted fields from Python output

key='filename.txt' AWSAccessKeyId='AKIA...EWS3' policy='eyJl...dfQ==' signature='RcU...XLw=' \

this is the file to upload

file@local-filename.txt

与curl一起使用:

curl -X POST \

the copy/pasted fields

-F key='filename.txt' -F AWSAccessKeyId='AKIA...EWS3' -F policy='eyJle...dfQ==' -F signature='RcU9...XLw=' \

the file to upload

-F file=@local-filename.txt \

the URL

https://some-bucket.s3.amazonaws.com/

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则