上传到S3,获取boto.exception.S3ResponseError:S3ResponseError:400 Bad Request

时间:2018-05-30 14:28:46

标签: python-3.x amazon-web-services amazon-s3 boto3

考虑这段代码

    file = request.files["wg_puid"]
    filename = hash + ".wargamerpl2"
    zipname = hash + ".zip"
    file.save(os.path.join(UPLOAD_FOLDER, filename))
    os.chdir(UPLOAD_FOLDER)
    zipfile.ZipFile(zipname, mode='w').write(os.path.join(UPLOAD_FOLDER, filename))
    if os.path.exists(filename):
        os.remove(filename)
    else:
        return apology("Something went wrong", 400)


    # Uploads replay to S3
    # TODO fix error
    key = boto.s3.key.Key(bucket, zipname)
    with open(zipname, "rb") as f:
        key.send_file(f)

我尝试将.zip上传到服务器的文件,然后将该文件发送到我的S3存储桶。我目前收到错误:

boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?><Error><Code>BadRequest</Code><Message>An error occurred when parsing the HTTP request.</Message><RequestId>3184ACA27BE403B1</RequestId><HostId>...</HostId></Error>

我的代码在这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

我需要使用set_contents_from_filename,而不是send_file

这是我的代码,已修复并正常工作。

        # Upload percentage
    def percent_cb(complete, total):
        sys.stdout.write('.')
        sys.stdout.flush()

    # Upload .zip to S3 bucket
    k = Key(bucket)
    k.key = zipname
    k.set_contents_from_filename(zipname, cb=percent_cb, num_cb=10)