Skip to main content

Using the S3 Proxy

Once the S3 proxy is configured and running, switching your code to point to the proxy instead of S3 should involve only minimal changes. Documented below is an overview of those changes with code samples.

Bucket addressing

When using AWS S3, the bucket name and region are typically part of the URL as part of Amazon's virtual bucket addressing. With that, an S3 bucket named "antimatter" in the "us-west-2" region would be addressable at https://antimatter.s3.us-west-2.amazonaws.com

Path style bucket addressing (e.g. https://s3.us-west-2.amazonaws.com/antimatter) is possible, but Amazon has deprecated support for this.

AWS client library setup

import boto3

# This assumes that the proxy can be accessed at "http://s3proxy:9234" and a bucket
# named "test_bucket" exists in region "us-west-2"
PROXY_ENDPOINT = "http://s3proxy:9234"
BUCKET = "test_bucket"
REGION = "us-west-2"
TEST_FILE = "test_file"
TEST_TEXT = b"Antimatter test"

# Create client, pointing at the proxy
# Note: to configure an access key and secret, pass the "aws_access_key_id" and
# "aws_secret_access_key" args.
client = boto3.client("s3", region_name=REGION, endpoint_url=PROXY_ENDPOINT)

# Test client connection and functionality by creating a file in the bucket, then
# downloading the file and comparing the contents
client.put_object(Bucket=BUCKET, Key=TEST_FILE, Body=TEST_TEXT)
response_text = client.get_object(Bucket=BUCKET, Key=TEST_FILE).get("Body").read()
assert response_text == TEST_TEXT

# Check that the file was encrypted on S3 by downloading it with a regular client
client_plaintext = boto3.client("s3", region_name=REGION)
response_plaintext = client_plaintext.get_object(Bucket=BUCKET, Key=TEST_FILE).get("Body").read()
assert response_plaintext != TEST_TEXT

Generating presigned URLs

Generating a presigned URL can be performed by building it client side, or by calling the correct endpoint in the S3 Proxy (for presigned GETs and PUTs; support for POSTs is not yet implemented).

For more about presigned URLs on S3, see https://docs.aws.amazon.com/AmazonS3/latest/userguide/ShareObjectPreSignedURL.html

Code examples

The code examples assume the following:

  • the S3 Proxy can be reached at http://s3proxy:9234
  • the presigned endpoints on S3 Proxy can be reached at http://s3proxy:9235
  • the bucket 'test_bucket' exists
  • the file 'test_file' can be accessed and/or created
  • for Python examples, an S3 client named 'client_plaintext' that points to AWS directly has been created, as in the code examples above
  • for NodeJS examples, an S3 client named 'clientPlaintext' that points to AWS directly has been created, as in the code examples above

Generating a presigned GET request:

curl --request GET \
--url http://s3proxy:9234/am-gen-presign/get/test_bucket/test_file

Generating a presigned PUT request:

curl --request GET \
--url http://s3proxy:9234/am-gen-presign/put/test_bucket/test_file

Generating a presigned POST request:

# Not supported; this must be generated client side