Amazon S3 Filesystem for PyFilesystem2

1.1.1 · maintenance · verified Wed Apr 15

fs-s3fs is a PyFilesystem2 adapter for Amazon S3, allowing you to interact with S3 buckets using the standard PyFilesystem2 API. It's built on top of boto3 and provides a consistent way to manage files and directories on S3. The current version is 1.1.1, with the last release in 2019, indicating a slow maintenance cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an `S3FS` object, connect to an S3 bucket, create/read/list/delete files and directories. Ensure AWS credentials are set via environment variables or passed directly, and replace `your-unique-s3-bucket-name` with an actual S3 bucket you have access to.

import os
from s3fs import S3FS

# For local testing, ensure AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set
# in your environment variables. You can also pass them directly to the constructor.
# AWS_REGION_NAME can also be specified if needed.

access_key = os.environ.get('AWS_ACCESS_KEY_ID', 'YOUR_ACCESS_KEY')
secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY', 'YOUR_SECRET_KEY')
bucket_name = 'your-unique-s3-bucket-name' # IMPORTANT: Replace with an existing S3 bucket name

if access_key == 'YOUR_ACCESS_KEY' or secret_key == 'YOUR_SECRET_KEY':
    print("WARNING: Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.")
    print("         This quickstart will likely fail without valid AWS credentials.")

try:
    # Initialize S3FS. If access_key/secret_key are None, boto3 will look for env vars or IAM roles.
    fs_s3 = S3FS(bucket_name=bucket_name,
                 access_key=access_key if access_key != 'YOUR_ACCESS_KEY' else None,
                 secret_key=secret_key if secret_key != 'YOUR_SECRET_KEY' else None)

    print(f"Successfully connected to S3 bucket: {bucket_name}")

    # Example 1: List contents of the bucket root
    print(f"\nContents of '{bucket_name}' (root):")
    for info in fs_s3.listdir('/', detail=True):
        print(f"- {info['name']} (type: {info['type']}) {'(directory)' if info['is_dir'] else ''}")

    # Example 2: Create a directory
    new_dir = 'test_s3fs_dir'
    fs_s3.makedir(new_dir, recreate=True)
    print(f"\nDirectory '{new_dir}' created.")

    # Example 3: Create a file inside the new directory
    file_path = f"{new_dir}/hello.txt"
    with fs_s3.open(file_path, 'w') as f:
        f.write("Hello from fs-s3fs on S3!")
    print(f"File '{file_path}' created with content.")

    # Example 4: Read the file
    with fs_s3.open(file_path, 'r') as f:
        content = f.read()
    print(f"Content of '{file_path}': '{content}'")

    # Example 5: Remove the file (clean up)
    fs_s3.remove(file_path)
    print(f"File '{file_path}' removed.")

    # Example 6: Remove the directory
    fs_s3.removedir(new_dir)
    print(f"Directory '{new_dir}' removed.")

except Exception as e:
    print(f"\nAn error occurred: {e}")
finally:
    if 'fs_s3' in locals():
        fs_s3.close() # Important to close the filesystem
        print("\nS3FS connection closed.")

view raw JSON →