Oracle Cloud Infrastructure Filesystem (ocifs)

1.3.4 · active · verified Thu Apr 16

ocifs is a Pythonic filesystem interface to Oracle Cloud Infrastructure (OCI) Object Storage. It builds on top of the `oci` Python SDK and integrates with the `fsspec` (filesystem_spec) ecosystem, allowing applications to interact with OCI Object Storage as if it were a local filesystem. It provides common file-system style operations like `cp`, `mv`, `ls`, `du`, and `open()` for reading and writing data. The current version is 1.3.4, and it is actively maintained by Oracle with a regular release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `OCIFileSystem` and perform basic operations like listing objects, writing, and reading a binary file from Oracle Cloud Object Storage. It assumes OCI credentials are configured via a default `~/.oci/config` file or available through Resource/Instance Principals. Remember to replace placeholder bucket and namespace names.

import ocifs
import os

# Initialize OCIFileSystem.
# It will attempt to use default OCI config (~/.oci/config) or Resource Principal/Instance Principal.
# For explicit config, pass a path or an oci.config object.
# config_path = os.environ.get('OCI_CONFIG_PATH', '~/.oci/config')
# fs = ocifs.OCIFileSystem(config=config_path, profile=os.environ.get('OCI_PROFILE', 'DEFAULT'))

fs = ocifs.OCIFileSystem()

# Replace with your bucket and namespace
bucket_name = 'your_bucket_name'
namespace_name = 'your_namespace_name'

# List contents of a bucket
try:
    print(f"Listing contents of oci://{bucket_name}@{namespace_name}")
    items = fs.ls(f'oci://{bucket_name}@{namespace_name}')
    for item in items:
        print(item)
except Exception as e:
    print(f"Error listing bucket contents: {e}")

# Example: Write and read a file
file_path = f'oci://{bucket_name}@{namespace_name}/test_ocifs.txt'
data_to_write = b'Hello, ocifs!'

try:
    with fs.open(file_path, 'wb') as f:
        f.write(data_to_write)
    print(f"Successfully wrote to {file_path}")

    with fs.open(file_path, 'rb') as f:
        read_data = f.read()
    print(f"Successfully read from {file_path}: {read_data}")
    assert read_data == data_to_write

    fs.rm(file_path)
    print(f"Successfully deleted {file_path}")
except Exception as e:
    print(f"Error during file operations: {e}")

view raw JSON →