Skip to main content

FastAPI Provider AWS

An AWS S3 provider package for FastAPI projects that provides a clean, class-based interface for interacting with Amazon S3.

Features

  • 📦 File Upload

    • Upload bytes or files to S3
    • Automatic content type handling
    • Returns S3 URLs for uploaded objects
  • 🔗 Presigned URLs

    • Generate presigned GET URLs for secure file access
    • Configurable expiration times
  • 📥 File Download

    • Download objects from S3 as bytes
    • Parse S3 URLs (s3://, https:// formats)
  • 🛡️ Error Handling

    • Comprehensive error handling
    • Clear validation messages

Installation

Using UV:

uv add fastapi-provider-aws

Or using pip:

pip install fastapi-provider-aws

Quick Start

Basic Usage

import os
from fastapi import FastAPI
from fastapi_provider_aws import S3Provider

app = FastAPI()

# Initialize AWS S3 provider (reused across routes/services)
s3_provider = S3Provider(
    bucket=os.getenv("S3_BUCKET_NAME", "my-bucket"),
    region=os.getenv("AWS_REGION", "us-east-1"),
    aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
    aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
)

@app.post("/upload")
async def upload_file():
    # Upload bytes
    s3_url = s3_provider.upload_bytes(
        key="path/to/file.txt",
        content=b"Hello, World!",
        content_type="text/plain"
    )
    return {"s3_url": s3_url}

Direct Initialization

from fastapi_provider_aws import S3Provider

# Initialize with direct values
s3_provider = S3Provider(
    bucket="my-bucket-name",
    region="us-west-2",
    aws_access_key_id="your-access-key-id",
    aws_secret_access_key="your-secret-access-key",
)

Uploading Files

# Upload from file path
s3_url = s3_provider.upload_file(
    file_path="/path/to/local/file.pdf",
    key="documents/file.pdf"
)

# Upload bytes
s3_url = s3_provider.upload_bytes(
    key="data/content.txt",
    content=b"File content here",
    content_type="text/plain"
)

Generating Presigned URLs

# Generate a presigned URL (expires in 1 hour by default)
presigned_url = s3_provider.presign_get_url_from_s3_url(
    s3_url="s3://bucket-name/path/to/file.pdf",
    expiration=3600  # seconds
)

# Or from an HTTPS S3 URL
presigned_url = s3_provider.presign_get_url_from_s3_url(
    s3_url="https://bucket-name.s3.amazonaws.com/path/to/file.pdf",
    expiration=7200  # 2 hours
)

Downloading Files

# Download object as bytes
content = s3_provider.get_object_bytes(
    s3_url="s3://bucket-name/path/to/file.pdf"
)

# Extract filename from S3 URL
filename = s3_provider.get_filename_from_s3_url(
    s3_url="s3://bucket-name/path/to/file.pdf",
    default="document.pdf"
)

Using with Dependency Injection

import os
from fastapi import Depends
from fastapi_provider_aws import S3Provider

def get_s3_provider() -> S3Provider:
    return S3Provider(
        bucket=os.getenv("S3_BUCKET_NAME", "my-bucket"),
        region=os.getenv("AWS_REGION", "us-east-1"),
        aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
        aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
    )

@app.post("/upload")
async def upload_file(
    s3: S3Provider = Depends(get_s3_provider)
):
    s3_url = s3.upload_bytes(
        key="path/to/file.txt",
        content=b"Hello, World!"
    )
    return {"s3_url": s3_url}

Using in Application Config

import os
# In your api/config.py or similar
from fastapi_provider_aws import S3Provider

# Initialize AWS S3 provider (reused across routes/services)
s3_provider = S3Provider(
    bucket=os.getenv("S3_BUCKET_NAME", "my-bucket"),
    region=os.getenv("AWS_REGION", "us-east-1"),
    aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
    aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
)

# Then import and use in your routes
from api.config import s3_provider

@app.post("/upload")
async def upload_file():
    s3_url = s3_provider.upload_bytes(
        key="path/to/file.txt",
        content=b"Hello, World!"
    )
    return {"s3_url": s3_url}

API Reference

S3Provider

__init__

Initialize the S3 provider.

Parameters:

  • bucket (str, required): S3 bucket name
  • region (str, required): AWS region name
  • aws_access_key_id (str, required): AWS access key ID
  • aws_secret_access_key (str, required): AWS secret access key
  • server_side_encryption (str | None, optional): SSE algorithm for puts (default "AES256"). Pass None to omit the header.

upload_bytes

Upload bytes to S3 and return an s3:// URL.

Parameters:

  • key (str, required): S3 object key (path)
  • content (bytes, required): Content to upload
  • content_type (str, optional): MIME type of the content

Returns: str - S3 URL (s3://bucket/key)

upload_file

Upload a local file to S3 and return an s3:// URL.

Parameters:

  • file_path (str, required): Path to local file
  • key (str, required): S3 object key (path)

Returns: str - S3 URL (s3://bucket/key)

presign_get_url_from_s3_url

Generate a presigned GET URL for a stored S3 URL.

Parameters:

  • s3_url (str, required): S3 URL (supports s3:// and https:// formats)
  • expiration (int, optional): URL expiration time in seconds (default: 3600)

Returns: str - Presigned HTTPS URL

get_object_bytes

Download an object from S3 and return its content as bytes.

Parameters:

  • s3_url (str, required): S3 URL to download

Returns: bytes - File content

get_filename_from_s3_url

Extract the filename from an S3 URL.

Parameters:

  • s3_url (str, required): S3 URL to extract filename from
  • default (str, optional): Default filename if extraction fails

Returns: str - Filename

URL Format Support

The provider supports multiple S3 URL formats:

  • s3://bucket-name/path/to/file
  • https://bucket-name.s3.amazonaws.com/path/to/file
  • http://bucket-name.s3.amazonaws.com/path/to/file

Error Handling

The provider includes error handling for:

  • Missing AWS credentials
  • Invalid S3 URLs
  • Network errors
  • S3 API errors

Development

Setup

  1. Clone the repository
  2. Install dependencies:
uv sync
  1. Set up environment variables (optional, for use with os.getenv()):
export AWS_ACCESS_KEY_ID=your-access-key-id
export AWS_SECRET_ACCESS_KEY=your-secret-access-key
export S3_BUCKET_NAME=your-bucket-name
export AWS_REGION=us-east-1

License

MIT License - see LICENSE file for details

Metadata

Release files for fastapi-provider-aws 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-provider-aws 0.1.1
File Size Uploaded
fastapi_provider_aws-0.1.1.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-provider-aws 0.1.1
File Interpreter ABI Platform
fastapi_provider_aws-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / fastapi_provider_aws-0.1.1.tar.gz

Download URL fastapi_provider_aws-0.1.1.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3c7db1281886623f2c81ac0ffca23b4e342c1d4bec219990c712d0b758c49cb1
BLAKE2b-256 checksum
How to use checksums
8cf02a73cdeb84c5688b54ed8c9a7578b0d7213eda80400dfa0ca9250b11ba56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release files / fastapi_provider_aws-0.1.1-py3-none-any.whl

Download URL fastapi_provider_aws-0.1.1-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
14646f3740931bfb54fe7fb95d6170afb0980883605ae9bd298c7a1d39f65379
BLAKE2b-256 checksum
How to use checksums
4b1175f1e477d78cfaa58085b4549bb45077a82b0237e641e00c8aa1d892770c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 17, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page