使用 Content-Type 参数来指定预签名请求中要上传的文件的MIME类型。以下是使用Python的示例代码:
import boto3
from botocore.client import Config
# 建立 S3 客户端连接
s3 = boto3.client('s3', config=Config(signature_version='s3v4'))
# 指定上传对象的存储桶名称和上传文件名
bucket_name = 'your-bucket-name'
filename = 'your-file-name'
# 设置文件的 Content-Type
content_type = 'image/jpeg' # 更改成你的文件的 MIME type
# 生成预签名 URL
presigned_url = s3.generate_presigned_url(
ClientMethod='put_object',
Params={
'Bucket': bucket_name,
'Key': filename,
'ContentType': content_type # 指定文件的 MIME type
},
ExpiresIn=3600 # URL 有效时间为 1 小时
)
print(presigned_url)
此代码将生成一个有效期为1小时的预签名 URL,并在请求中指定了上传文件的 MIME 类型。