From c81c9e72aa642238bc9cff1fe799df3edf7fe4f6 Mon Sep 17 00:00:00 2001 From: Jo Walsh Date: Tue, 20 Aug 2024 16:14:39 +0100 Subject: [PATCH] replace the JSON credentials with .env / dotenv --- src/os_api/api.py | 16 ++++++++-------- src/os_api/credentials.json | 5 ----- 2 files changed, 8 insertions(+), 13 deletions(-) delete mode 100644 src/os_api/credentials.json diff --git a/src/os_api/api.py b/src/os_api/api.py index 8fc13a1..d5b38ae 100644 --- a/src/os_api/api.py +++ b/src/os_api/api.py @@ -3,15 +3,15 @@ """ import asyncio -import json import logging -from pathlib import Path +import os from time import perf_counter import aioboto3 import boto3 import uvicorn from botocore.exceptions import NoCredentialsError, PartialCredentialsError +from dotenv import load_dotenv from fastapi import FastAPI, File, Form, Query, UploadFile from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse, RedirectResponse @@ -51,13 +51,13 @@ ) -# Load AWS credentials and S3 bucket name from config file -with Path(__file__).with_name("credentials.json").open("r", encoding="utf-8") as config_file: - aws_credentials = json.load(config_file) +# Load AWS credentials and S3 bucket name from .env file +# Rather than depend on the presence of credentials.json in the package +load_dotenv() -AWS_ACCESS_KEY_ID = aws_credentials["AWS_ACCESS_KEY_ID"] -AWS_SECRET_ACCESS_KEY = aws_credentials["AWS_SECRET_ACCESS_KEY"] -AWS_URL_ENDPOINT = aws_credentials["AWS_URL_ENDPOINT"] +AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID", "") +AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY", "") +AWS_URL_ENDPOINT = os.environ.get("AWS_URL_ENDPOINT", "") CONCURRENCY_LIMIT = 200 # Adjust this value based on your server capabilities diff --git a/src/os_api/credentials.json b/src/os_api/credentials.json deleted file mode 100644 index 883ad25..0000000 --- a/src/os_api/credentials.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "AWS_ACCESS_KEY_ID": "", - "AWS_SECRET_ACCESS_KEY": "", - "AWS_URL_ENDPOINT": "" -} \ No newline at end of file