How change the image name and save it, when i uploading an image to s3 using boto3.
ex) datetime.jpg
pleas help....
@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!");
},
});
}
I think we can use this code. But it couldn't be used.
extension = file.filename.split('.')[-1]
today = datetime.now()
mytime = today.strftime('%Y-%m-%d-%H-%M-%S')
filename = f'file-{mytime}'
You can rename objects after uploading into s3.
client.copy_object(Bucket="BucketName", CopySource="BucketName/OriginalName", Key="NewName")
client.delete_object(Bucket="BucketName", Key="OriginalName")