I encountered an error while trying to upload a file to the Storage using boto3 on CloudFlare R2.
Until a few days ago, there were no issues with this code, but suddenly, an issue arose, and it's difficult to identify the cause.
I have been running the code in various environments such as Python 3.7, 3.9, and 3.11, and I have been using the appropriate version of boto3 for each of these environments.
The code is as follows:
class CloudApi:
def init( self, endpoint_url, access_key_id, secret_access_key, bucket_name ):
self.endpoint_url = endpoint_url
self.access_key_id = access_key_id
self.secret_access_key = secret_access_key
self.bucket_name = bucket_name
def boto3_client(self):
s3 = boto3.client(
service_name="s3",
endpoint_url=self.endpoint_url,
aws_access_key_id=self.access_key_id,
aws_secret_access_key=self.secret_access_key,
)
return s3
def upload_file(self, path_local, path_cloud):
s3 = self.boto3_client()
s3.upload_file(path_local, self.bucket_name, path_cloud)
r2 = CloudApi( endpoint_url="", access_key_id="", secret_access_key="", bucket_name="")
r2.upload_file(path_local='', path_cloud='')
The error message is as follows:
Traceback (most recent call last):
File "~/cloud_utils.py", line 182, in <module>
test.upload_file(path_file_local='~/test.json',
File "~/cloud_utils.py", line 121, in upload_file
create_multipart_upload_response = s3.create_multipart_upload(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "~/venv/lib/python3.11/site-packages/botocore/client.py", line 530, in _api_call
return self._make_api_call(operation_name, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "~/venv/lib/python3.11/site-packages/botocore/client.py", line 964, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (Unauthorized) when calling the CreateMultipartUpload operation: Unauthorized
What should I do to resolve this issue? Unlike AWS, I'm not sure about the policies or IAM aspects that need to be adjusted on CloudFlare.