调用 PutObject 操作时的 PermanentRedirect

Pro*_*eus 2 python amazon-s3 boto amazon-web-services python-3.x

下面的代码在本地运行并将文件从目录上传到 S3。它使用 Boto3 和 Python 3。

s3 = boto3.resource('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_ACCESS_KEY_SECRET)
bucket = s3.Bucket(bucket_name)
uploadFileNames = []
for (sourceDir, dirname, filenames) in os.walk(sourceDir):
    for filename in filenames:
        bucket.put_object(Key=filename, Body=open("{}{}".format(sourceDir, filename), "rb"))
    break
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我在生产服务器(Ubuntu)上运行相同的代码时,出现以下错误,为什么?

    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.4/site-packages/botocore/client.py", line 335, in _make_api_call
    raise ClientError(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (PermanentRedirect) when calling the PutObject operation: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
Run Code Online (Sandbox Code Playgroud)

再次在我的 Mac 本地,此代码可以工作,但仅在我的 Ubuntu 服务器上我收到此错误。

Joh*_*ein 5

错误说:

调用 PutObject 操作时发生错误 ( PermanentRedirect ):您尝试访问的存储桶必须使用指定的端点进行寻址。请将所有未来请求发送到此端点

当您使用的 Amazon S3 存储桶与创建 Amazon S3 客户端的区域位于不同区域时,通常会发生这种情况。

例如,存储桶位于us-west-2,但客户端是为 创建的ap-southeast-2

您可以通过凭据文件region_name或在创建客户端对象时传递 a 来指定区域。也可以在 中定义默认区域boto3.setup_default_session()