【问题标题】:How change the image name and save it, when i uploading an image to s3 using boto3当我使用 boto3 将图像上传到 s3 时,如何更改图像名称并保存它
【发布时间】:2021-12-03 20:05:12
【问题描述】:

当我使用 boto3 将图像上传到 s3 时,如何更改图像名称并保存它。

例如)日期时间.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}'

【问题讨论】:

  • 在调用 put_object 时提供您喜欢的任何 Key 参数。您不必使用上传的文件名。
  • Key 参数的存在完全相同的目的 - 不需要重命名文件本身。

标签: javascript python amazon-web-services amazon-s3 boto3


【解决方案1】:

您可以在上传到 s3 后重命名对象。

client.copy_object(Bucket="BucketName", CopySource="BucketName/OriginalName", Key="NewName")
client.delete_object(Bucket="BucketName", Key="OriginalName")

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 2015-05-16
    • 2018-09-12
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 2019-04-29
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多