66lotte
66lotte

Reputation: 187

Upload object to Oracle Storage using put_object in Python

I'm trying to upload an object to Oracle Storage with oci-cli library in Python. When I try using command-line:

oci os object put -ns grddddaaaZZ -bn dev.bucket --name processed/2020-11 --file /path/to/my/file/image.tif

I actually get a response like:

Upload ID: 4f...78f0fdc5
Split file into 2 parts for upload.
Uploading object  [------------------------------------]    0%
...

but when I try using the framework:

try:
    namespace = 'grddddaaaZZ'
    bucket = 'dev.bucket'
    object_path = 'processed/2020-11/image.tif'

    with open('/path/to/my/file/image.tif', "rb") as image:
       publish_payload = image.read()

    response = object_storage.put_object(namespace, bucket, object_path, publish_payload)

except (InvalidConfig, BaseConnectTimeout, ConfigFileNotFound, ServiceError) as error:
    logging.error(">>>>>>>> Something went wrong when try to list bucket {} objects. Error {}".
                          format(bucket, error))

the upload does not complete:

...
    response = object_storage.put_object(namespace, bucket, object_path, publish_payload)
  File ".../.venv/lib/python3.8/site-packages/oci/object_storage/object_storage_client.py", line 4113, in put_object
    return self.base_client.call_api(
  File ".../.venv/lib/python3.8/site-packages/oci/base_client.py", line 272, in call_api
    response = self.request(request)
  File ".../.venv/lib/python3.8/site-packages/oci/base_client.py", line 378, in request
    raise exceptions.RequestException(e)
oci.exceptions.RequestException: ('Connection aborted.', timeout('The write operation timed out'))

I thought that it could be the size of file (which is around 208Mb), but in put_object documentation says 5Gb limit. So, I do not think it could be the issue. My last chance would be to use os.system(), but it would not be what I truly want.

Some clue in what could be missing in this second option?

Upvotes: 3

Views: 3726

Answers (2)

linuxamrit
linuxamrit

Reputation: 1

    with open('tomcat_access_log_20221118-231901.log.zip', 'rb') as filePtr:
...     upload_resp = object_storage_client.put_object(nameSpace,bucket_name='my-Test-Bucket',object_name=file_to_upload,put_object_body=filePtr)

Note : file_to_upload = 'empty_folder_for_testing/tomcat-admin-server/tomcat_access_log_20221118-231901.log.zip'

The above code getting stuck for very log till end getting timeout. But actually i can see file uploaded properly. But this command getting stuck for long enough till timeout ... Any idea ?

Upvotes: 0

char
char

Reputation: 2147

You could try uploading some other data first, to see if it's the payload:

namespace = 'grddddaaaZZ'
bucket_name = 'dev.bucket'
object_name = 'processed/2020-11/test.txt'
test_data = b"Hello, World!"

obj = object_storage.put_object(
    namespace,
    bucket_name,
    object_name,
    my_data)

or you try it without reading the file contents and just passing the file object:

namespace = 'grddddaaaZZ'
bucket = 'dev.bucket'
object_path = 'processed/2020-11/image.tif'

with open('/path/to/my/file/image.tif', 'rb') as f:
    obj = object_storage.put_object(namespace, bucket, object_path, f)

Upvotes: 2

Related Questions