The code below work locally and uploads files from a directory to S3. It's using Boto3 with 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
My problem is what when I run the same code on my production server (Ubuntu) I get the following error, why?
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.
Again locally on my Mac this code works, it is only on my Ubuntu server I get this error.
The errors says:
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.
This typically occurs when the Amazon S3 bucket you are using is in a different Region to where the Amazon S3 client was created.
For example, the bucket is in us-west-2
but the client was created for ap-southeast-2
.
You can specify the region via a credentials file, or by passing a region_name
when creating the client object. A default region can also be defined in boto3.setup_default_session()
.
来源:https://stackoverflow.com/questions/32246149/permanentredirect-when-calling-the-putobject-operation