wip(22w11a): add aws support

This commit is contained in:
Victor Westerlund 2022-03-15 14:51:34 +01:00
parent e58bb45001
commit cdeeb47278
3 changed files with 41 additions and 1 deletions

View file

@ -3,7 +3,7 @@ SOURCE_FOLDER=""
# (Required) Name of the remote bucket or container # (Required) Name of the remote bucket or container
TARGET_BUCKET="" TARGET_BUCKET=""
# (Required) Cloud provider (gcs, s3, azure) # (Required) Cloud provider (gcloud, aws, azure)
SERVICE_NAME="" SERVICE_NAME=""
# (Required) Cloud provider access string or path to key file # (Required) Cloud provider access string or path to key file
SERVICE_KEY="" SERVICE_KEY=""

40
src/cloud/aws.py Normal file
View file

@ -0,0 +1,40 @@
import os
import boto3
from botocore.exceptions import ClientError
from ..fs.utils import get_file
class StorageClient:
def __init__(self):
self.set_access_key()
self.client = boto3.client("s3")
self._error = None
@property
def error(self):
return self._error
@error.setter
def error(self, state):
self._error = state
# Get IAM user access key and ID
def set_access_key(self):
key = os.getenv("SERVICE_KEY").split(";")
if len(key) != 2:
self.error = "Invalid AWS service key"
return False
os.environ["aws_access_key_id"] = key[0]
os.environ["aws_secret_access_key"] = key[1]
def upload(self, path: str) -> bool:
name = get_file(path)
try:
resp = self.client.upload_file(path, os.getenv("TARGET_BUCKET"), name)
except ClientError as e:
self.error = e
return False
return True