a. 获取accesskey
进入IAM - 选择用户 - 选择安全证书-创建访问密钥
b. 服务端创建获取签名接口
# key格式:img/test.png ,其中img为文件夹
key = request.GET.get('key',None)
bucket_name = 你的bucket名称
generated_signed_url = create_presigned_url(bucket_name ,key)
def create_presigned_url(bucket_name,bucket_key,expiration=3600):
s3_client = boto3.client('s3',
aws_access_key_id='xxx',
aws_secret_access_key='xxx',
config=Config(signature_version='s3v4'),
region_name='us-east-1')
url = s3_client.generate_presigned_url(
ClientMethod = 'put_object',
Params = {
'Bucket':bucket_name,
'Key':bucket_key,
'ACL':'public-read'
}
)
return url
c. 用put直接请求b返回的url,带上以下header
x-amz-acl: public-read
以binary方式上传文件,注意文件名要与bucket_key同名(不算文件夹)
网友评论