Skip to content

boto3

Use boto3 with EZGH Object Storage, with path-style or virtual-hosted URLs.

Updated View as Markdown

Examples here use boto3 1.42. Set the endpoint, the region and signature_version:

import boto3
from botocore.config import Config

s3 = boto3.client(
    "s3",
    endpoint_url="https://s3.us-west-1.ezghapps.com",
    region_name="us-west-1",
    aws_access_key_id="oak_…",
    aws_secret_access_key="…",
    config=Config(signature_version="s3v4"),
)

Leave the keys out to read them from AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

signature_version="s3v4" is what makes presigned URLs work: without it, generate_presigned_url signs with SigV2, which is refused.

Path-style and virtual-hosted URLs

With a custom endpoint, boto3 puts the bucket in the path. For virtual-hosted URLs, set addressing_style:

config = Config(signature_version="s3v4", s3={"addressing_style": "virtual"})
Style URL
path https://s3.us-west-1.ezghapps.com/my-bucket/report.pdf
virtual https://my-bucket.s3.us-west-1.ezghapps.com/report.pdf

Upload and download files

upload_file and download_file use multipart uploads and ranged downloads for large files.

s3.upload_file("backup.tar", "my-bucket", "backups/backup.tar")
s3.download_file("my-bucket", "backups/backup.tar", "backup.tar")

List objects

paginator = s3.get_paginator("list_objects_v2")
for page in paginator.paginate(Bucket="my-bucket", Prefix="backups/"):
    for item in page.get("Contents", []):
        print(item["Key"], item["Size"])

Handle errors

from botocore.exceptions import ClientError

try:
    s3.head_object(Bucket="my-bucket", Key="missing.txt")
except ClientError as error:
    print(error.response["Error"]["Code"])

Operations outside the supported list answer NotImplemented.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close