Downloaded GZ files is showing 0 byte

428 Views Asked by At

I am using OCI Python SDK and when i am trying to download an object (from an OCI bucket) which is GZ format it is getting downloaded but the file size is zero byte. Attaching the code Any help is much appriciable.

import os
import oci
import io
import sys

reporting_namespace = 'xygabcdef'

prefix_file = "abc/xyz"

# Update these values
destination_path = 'downloaded_reports'

# Make a directory to receive reports
if not os.path.exists(destination_path):
    os.mkdir(destination_path)

# Get the list of reports
config = oci.config.from_file(oci.config.DEFAULT_LOCATION, oci.config.DEFAULT_PROFILE)
reporting_bucket = sys.argv[1]
object_storage = oci.object_storage.ObjectStorageClient(config)
report_bucket_objects = object_storage.list_objects(reporting_namespace, reporting_bucket, prefix=prefix_file)

#def download_audit():
for o in report_bucket_objects.data.objects:
    print('Found file ' + o.name)
    object_details = object_storage.get_object(reporting_namespace, reporting_bucket, o.name)
    print (object_details)
    filename = o.name.rsplit('/', 1)[-1]
    with open(destination_path + '/' + filename, 'wb') as f:
        for chunk in object_details.data.raw.stream(1024 * 1024, decode_content=False):
            f.write(chunk)

2

There are 2 best solutions below

2
On

Please see the example here. Does this work for you? Namely:

get_obj = object_storage.get_object(namespace, bucket_name, example_file_object_name)
with open('example_file_retrieved', 'wb') as f:
    for chunk in get_obj.data.raw.stream(1024 * 1024, decode_content=False):
        f.write(chunk)

In your example destintation_path seems to be undefined, and seems to have a typo (destintation -> destination). Could this be the problem?

Lastly, what does object_details report the file size / content-length as? It could be that the file size of the object in Object Storage is itself 0 bytes.

0
On

the .content from the .data of get_object should give you the file data (binary or text/josn/...), so here is a modified version of your code:

import os
import sys

import oci

reporting_namespace = 'xygabcdef'
prefix_file = "abc/xyz"

# Update these values
destination_path = 'downloaded_reports'

# Get the list of reports
config = oci.config.from_file(oci.config.DEFAULT_LOCATION, oci.config.DEFAULT_PROFILE)
reporting_bucket = sys.argv[1]
object_storage = oci.object_storage.ObjectStorageClient(config)
objects = object_storage.list_objects(reporting_namespace, reporting_bucket, prefix=prefix_file).data

# def download_audit():
for obj in objects:
    print('Found file ' + obj.name)
    object_response = object_storage.get_object(reporting_namespace, reporting_bucket, obj.name).data
    print(object_response)

    file_path = os.path.join(destination_path, obj.name)
    # Make sure parent dirs up to the file level are created
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    with open(file_path, 'wb') as file:
        file.write(object_response.content)