当我使用boto3上传图像到s3时,如何更改图像名称并保存它
) datetime.jpg交货
请求帮助……
@app.route('/fileupload', methods=['POST'])
def file_upload():
file = request.files['file']
s3 = boto3.client('s3',
aws_access_key_id="mykey",
aws_secret_access_key="myaccesskey"
)
s3.put_object(
ACL="public-read",
Bucket="burket",
Body=file,
Key=file.filename,
ContentType=file.content_type)
print(url)
return jsonify({'result': 'success'})
function save() {
let form_data = new FormData($('#upload-file')[0]);
$.ajax({
type: 'POST',
url: '/fileupload',
data: form_data,
processData: false,
contentType: false,
success: function (data) {
alert("success!");
},
});
}
我认为我们可以使用这个代码。但它不能被使用。
extension = file.filename.split('.')[-1]
today = datetime.now()
mytime = today.strftime('%Y-%m-%d-%H-%M-%S')
filename = f'file-{mytime}'
上传到s3后可以重命名对象
client.copy_object(Bucket="BucketName", CopySource="BucketName/OriginalName", Key="NewName")
client.delete_object(Bucket="BucketName", Key="OriginalName")