Skip to content

Use an S3 SDK

Connect AWS SDKs and mc-compatible clients to the gateway.

This tutorial connects the AWS CLI and the Python SDK (boto3) to a Silo PostgreSQL gateway, generates a presigned download URL, and configures bucket CORS for browser requests. Complete the Quick start first so the attachments bucket, app-s3 user and local hello.txt file exist.

Set the endpoint and working directory in your terminal:

export SILO_ENDPOINT='https://s3.example.net:9000'
export SILO_TUTORIAL_DIR="$HOME/.local/share/silo-tutorial/files"
export AWS_CA_BUNDLE='/path/to/s3-ca.crt'

AWS CLI and Python SDK

Configure an AWS CLI profile (silo-app) with the app-s3 credentials, region us-east-1 and path-style addressing:

aws configure --profile silo-app
aws configure set s3.addressing_style path --profile silo-app
aws --profile silo-app --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api list-objects-v2 --bucket attachments
aws --profile silo-app --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3 cp "$SILO_TUTORIAL_DIR/hello.txt" s3://attachments/hello.txt

Create a Python 3.10+ virtual environment and install boto3:

python3 -m venv "$HOME/.local/share/silo-tutorial/venv"
"$HOME/.local/share/silo-tutorial/venv/bin/pip" install 'boto3==1.43.108' 'botocore==1.43.108'

Save the following script as ~/.local/share/silo-tutorial/sdk.py:

import os
import boto3
from botocore.config import Config

session = boto3.Session(profile_name="silo-app", region_name="us-east-1")
s3 = session.client(
    "s3",
    endpoint_url=os.environ["SILO_ENDPOINT"],
    verify=os.environ["AWS_CA_BUNDLE"],
    config=Config(signature_version="s3v4", s3={"addressing_style": "path"}),
)
s3.put_object(Bucket="attachments", Key="sdk.txt", Body=b"hello from Python\n")
response = s3.get_object(Bucket="attachments", Key="sdk.txt")
try:
    print(response["Body"].read().decode("utf-8"))
finally:
    response["Body"].close()

Run "$HOME/.local/share/silo-tutorial/venv/bin/python" "$HOME/.local/share/silo-tutorial/sdk.py". It prints hello from Python.

Keep default SDK checksums and TLS verification enabled. When uploading larger files with SDK multipart helpers, the completed object must fit within the store’s maximum object size (256 MiB by default, up to 1 GiB if configured at initialization).

Presigned URLs and bucket CORS

Generate a presigned GET URL valid for 15 minutes (900 seconds) using the s3 client from above:

url = s3.generate_presigned_url(
    "get_object", Params={"Bucket": "attachments", "Key": "sdk.txt"}, ExpiresIn=900
)
print(url)

A presigned URL carries the signing principal’s permissions and cannot grant access that the signer lacks. If signed by temporary STS credentials, the URL stops working when the session expires even if ExpiresIn is longer.

To allow browser uploads or downloads from another origin, configure a bucket CORS rule with an administrative profile (silo-admin):

aws configure --profile silo-admin
aws configure set s3.addressing_style path --profile silo-admin
aws --profile silo-admin --endpoint-url "$SILO_ENDPOINT" --region us-east-1 s3api put-bucket-cors \
  --bucket attachments \
  --cors-configuration '{"CORSRules":[{"AllowedOrigins":["https://app.example.net"],"AllowedMethods":["GET","PUT","HEAD"],"AllowedHeaders":["*"],"ExposeHeaders":["ETag"],"MaxAgeSeconds":600}]}'
curl --cacert "$AWS_CA_BUNDLE" -i -X OPTIONS "$SILO_ENDPOINT/attachments/cors-demo.txt" \
  -H 'Origin: https://app.example.net' \
  -H 'Access-Control-Request-Method: PUT' \
  -H 'Access-Control-Request-Headers: content-type'

A matching preflight request returns the allowed origin and methods in the response headers; a rejected preflight returns HTTP 403 (AccessForbidden). CORS headers do not bypass SigV4 signing or IAM authorization.

Scan to open this page
https://mimus.pgsty.com/docs/s3/tutorial/sdk/