import boto3
from botocore.exceptions import ClientError
s3_client = boto3.client('s3', aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY')
try:
response = s3_client.generate_presigned_url('put_object',
Params={'Bucket': 'YOUR_BUCKET_NAME',
'Key': 'YOUR_OBJECT_KEY'},
ExpiresIn=3600,
HttpMethod='PUT')
except ClientError as e:
print(e)
以下是通过生成的Pre-signed URL上传文件的示例代码:
import requests
response = requests.put(PRE_SIGNED_URL, data=open('FILE_NAME', 'rb'))
其中,PRE_SIGNED_URL
为生成的Pre-signed URL,FILE_NAME
为要上传的文件名。 请注意,生成Pre-signed URL时使用的存储桶名称、对象名称和有效期必须与上传请求一致。