import oci def get_client(): # # Authentication is done using Instance Principals on VMs and Resouce Principal on OCI Container Instances # The function first tries Resource Principal and fails back to Instance Principal in case of error # try: signer = oci.auth.signers.get_resource_principals_signer() except: signer = signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner() # Create secret client and retrieve content client = oci.object_storage.ObjectStorageClient( {}, signer=signer ) # the first empyty bracket is an empty config return client def list_bucket(client, namespace, bucket, prefix): objects = client.list_objects(namespace, bucket, prefix=prefix) # see https://docs.oracle.com/en-us/iaas/tools/python/2.135.0/api/request_and_response.html#oci.response.Response for all attrs return objects.data def upload_file(client, source_filename, namespace, bucket, prefix, target_filename): with open(source_filename, "rb") as in_file: client.put_object( namespace, bucket, f"{prefix.rstrip('/')}/{target_filename}", in_file ) def clean_folder(client, namespace, bucket, prefix): objects = client.list_objects(namespace, bucket, prefix=prefix) for o in objects.data.objects: print(f"Deleting {prefix.rstrip('/')}/{o.name}") client.delete_object(namespace, bucket, f"{o.name}") def delete_file(client, file, namespace, bucket, prefix): client.delete_object(namespace, bucket, f"{prefix.rstrip('/')}/{file}") def download_file(client, namespace, bucket, prefix, source_filename, target_filename): # Retrieve the file, streaming it into another file in 1 MiB chunks get_obj = client.get_object( namespace, bucket, f"{prefix.rstrip('/')}/{source_filename}" ) with open(target_filename, "wb") as f: for chunk in get_obj.data.raw.stream(1024 * 1024, decode_content=False): f.write(chunk)