Skip to content

Instantly share code, notes, and snippets.

@suanmiao
Created December 13, 2023 19:39
Show Gist options
  • Save suanmiao/ed376ecd708d694d2893f6ef6b83806c to your computer and use it in GitHub Desktop.
Save suanmiao/ed376ecd708d694d2893f6ef6b83806c to your computer and use it in GitHub Desktop.
oci_download.py
import oci
import os
import sys
def download_directory(object_storage_client, namespace, bucket_name, oci_prefix, local_directory):
# Create local directory if it does not exist
if not os.path.exists(local_directory):
os.makedirs(local_directory)
# List all objects in the OCI directory
list_objects = object_storage_client.list_objects(namespace, bucket_name, prefix=oci_prefix)
total_files = len(list_objects.data.objects)
print(f"Total files to download: {total_files}")
for index, obj in enumerate(list_objects.data.objects, start=1):
# Extract the file name from the object's name
file_name = obj.name.split('/')[-1]
if file_name:
local_file_path = os.path.join(local_directory, file_name)
# Download the file
get_obj = object_storage_client.get_object(namespace, bucket_name, obj.name)
with open(local_file_path, 'wb') as f:
for chunk in get_obj.data.raw.stream(1024 * 1024, decode_content=False):
f.write(chunk)
print(f"Downloaded ({index}/{total_files}): {file_name}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python oci_download.py oci://<bucket_name>/<path> <local_directory>")
sys.exit(1)
oci_path = sys.argv[1]
local_directory = sys.argv[2]
# Parsing the OCI path
bucket_name = oci_path.split('/')[2]
oci_prefix = '/'.join(oci_path.split('/')[3:])
# Initialize OCI Client
config = oci.config.from_file()
object_storage_client = oci.object_storage.ObjectStorageClient(config)
namespace = object_storage_client.get_namespace().data
# Download directory
download_directory(object_storage_client, namespace, bucket_name, oci_prefix, local_directory)
print("Download completed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment