Skip to content

Commit

Permalink
upload: allow setting S3 endpoint by an environment variable
Browse files Browse the repository at this point in the history
To assist with testing, make it possible to override the default
S3 endpoint URL by setting EXODUS_GW_S3_ENDPOINT_URL. It can be
used to (e.g.) test against localstack.

Note that, at some point, we expect to support multiple target
environments and have some method of managing their configuration;
most likely this env var will be dropped at that time.
  • Loading branch information
rohanpm committed Jul 24, 2020
1 parent 5cce74a commit 7d570e1
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions exodus_gw/s3/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from typing import Optional
import textwrap
import logging
import os

import aioboto3
from fastapi import Request, Response, Path, Query, HTTPException
Expand All @@ -53,6 +54,15 @@
# - requests should be authenticated


def s3_client():
# Certain aspects of the boto client can be tweaked by environment variables
# for development.
# This is expected to be replaced with a proper configuration system at some point.
return aioboto3.client(
"s3", endpoint_url=os.environ.get("EXODUS_GW_S3_ENDPOINT_URL") or None
)


@app.post(
"/upload/{bucket}/{key}",
tags=["upload"],
Expand Down Expand Up @@ -157,7 +167,7 @@ async def object_put(bucket: str, key: str, request: Request):
# Single-part upload handler: entire object is written via one PUT.
reader = RequestReader.get_reader(request)

async with aioboto3.client("s3") as s3:
async with s3_client() as s3:
response = await s3.put_object(
Bucket=bucket,
Key=key,
Expand All @@ -178,7 +188,7 @@ async def complete_multipart_upload(

LOG.debug("completing mpu for parts %s", parts)

async with aioboto3.client("s3") as s3:
async with s3_client() as s3:
response = await s3.complete_multipart_upload(
Bucket=bucket,
Key=key,
Expand All @@ -197,7 +207,7 @@ async def complete_multipart_upload(


async def create_multipart_upload(bucket: str, key: str):
async with aioboto3.client("s3") as s3:
async with s3_client() as s3:
response = await s3.create_multipart_upload(Bucket=bucket, Key=key)

return xml_response(
Expand All @@ -213,7 +223,7 @@ async def multipart_put(
):
reader = RequestReader.get_reader(request)

async with aioboto3.client("s3") as s3:
async with s3_client() as s3:
response = await s3.upload_part(
Body=reader,
Bucket=bucket,
Expand Down Expand Up @@ -248,7 +258,7 @@ async def abort_multipart_upload(
"""
LOG.debug("Abort %s", uploadId)

async with aioboto3.client("s3") as s3:
async with s3_client() as s3:
await s3.abort_multipart_upload(
Bucket=bucket, Key=key, UploadId=uploadId
)
Expand Down

0 comments on commit 7d570e1

Please sign in to comment.