cloudpathlib

0.23.0 · active · verified Sun Mar 29

cloudpathlib provides pathlib-style classes for interacting with files and directories in various cloud storage services such as AWS S3, Google Cloud Storage, and Azure Blob Storage. It aims to offer a familiar filesystem interface, abstracting away cloud-specific details. The library is actively maintained, with a typical release cadence that includes bug fixes and new features. The current version is 0.23.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a CloudPath object for S3, write text to it, read text from it, check its existence, and then delete it. Users should ensure their cloud provider authentication environment variables are correctly set for automatic authentication. Similar patterns apply to GSPath and AzureBlobPath.

import os
from cloudpathlib import CloudPath

# Ensure environment variables are set for the chosen cloud provider
# e.g., for S3: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
# for GS: GOOGLE_APPLICATION_CREDENTIALS (path to JSON key file)
# for Azure: AZURE_STORAGE_CONNECTION_STRING

# Example for S3
# Replace with your actual bucket and file name
s3_file_path = CloudPath("s3://your-test-bucket/hello.txt")

try:
    # Write to the cloud file
    s3_file_path.write_text("Hello from cloudpathlib!")
    print(f"Successfully wrote to {s3_file_path}")

    # Read from the cloud file
    content = s3_file_path.read_text()
    print(f"Content read: '{content}'")

    # Check if the file exists
    if s3_file_path.exists():
        print(f"File {s3_file_path} exists.")

    # Clean up (optional)
    s3_file_path.unlink()
    print(f"File {s3_file_path} deleted.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →