mirror of
https://codeberg.org/vlw/cloud-backup.git
synced 2025-09-14 01:53:42 +02:00
wip(22w11a): add aws support
This commit is contained in:
parent
e58bb45001
commit
cdeeb47278
3 changed files with 41 additions and 1 deletions
|
@ -3,7 +3,7 @@ SOURCE_FOLDER=""
|
|||
# (Required) Name of the remote bucket or container
|
||||
TARGET_BUCKET=""
|
||||
|
||||
# (Required) Cloud provider (gcs, s3, azure)
|
||||
# (Required) Cloud provider (gcloud, aws, azure)
|
||||
SERVICE_NAME=""
|
||||
# (Required) Cloud provider access string or path to key file
|
||||
SERVICE_KEY=""
|
||||
|
|
40
src/cloud/aws.py
Normal file
40
src/cloud/aws.py
Normal 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
|
Loading…
Add table
Reference in a new issue