wip(22w9b): add gcs upload

This commit is contained in:
Victor Westerlund 2022-03-02 03:43:06 +01:00
parent bc9ba0783f
commit 7fa595584f
6 changed files with 37 additions and 14 deletions

View file

@ -2,6 +2,6 @@ SOURCE_FOLDER=
TARGET_BUCKET=
# Cloud provider "gcs, aws, azure"
PROVIDER_NAME=
SERVICE_NAME=
# Path to service account key file
PROVIDER_KEY=
SERVICE_KEY=

View file

@ -13,7 +13,7 @@ class Backup(FileSystem):
self.db = Database()
self.cloud = StorageClient()
self.zip = self.db.get_flag("ZIP")
self.compress = self.db.get_flag("COMPRESS")
# Backup a file or folder
def backup_item(self, item: Union[list, str]) -> bool:
@ -36,7 +36,7 @@ class Backup(FileSystem):
blob = item
# Upload as zip archive
if self.zip:
if self.compress:
blob = FileSystem.zip(blob)
# Upload to cloud
@ -45,10 +45,13 @@ class Backup(FileSystem):
if self.db.set_item(item):
print("OK")
else:
print("OK, BUT: Failed to update database")
print("OK, but failed to update database")
else:
print("FAILED")
# Remove temp zip
if self.compress:
FileSystem.delete(blob)
return
# Scan TARGET_FOLDER for files and folders to back up

View file

@ -6,7 +6,7 @@ import importlib
class Storage:
def __init__(self):
self._service = None
self.service = os.getenv("PROVIDER_NAME")
self.service = os.getenv("SERVICE_NAME")
@property
def service(self):
@ -19,7 +19,7 @@ class Storage:
service = "gcs"
module = importlib.import_module("src.cloud." + service)
self._service = module.StorageClient(os.getenv("TARGET_BUCKET"))
self._service = module.StorageClient()
@staticmethod
def get_args(values):

View file

@ -1,10 +1,26 @@
import os
from google.cloud import storage
from ..fs.utils import get_file
# Client for Google Cloud Storage
class StorageClient:
def __init__(self, bucket):
client = storage.Client()
self.bucket = client.bucket(bucket)
def __init__(self):
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.getenv("SERVICE_KEY")
def upload(self, item):
blob = self.bucket.blob()
client = storage.Client()
self.bucket = client.bucket(self.get_bucket())
def get_bucket(self):
return os.getenv("TARGET_BUCKET")
def upload(self, path: str) -> bool:
name = get_file(path)
blob = self.bucket.blob(name)
try:
with open(path, "rb") as f:
blob.upload_from_file(f)
return True
except:
return False

View file

@ -10,6 +10,6 @@ CREATE TABLE manifest (
INSERT INTO flags
VALUES
("ZIP", 1),
("COMPRESS", 1),
("BUCKET_OK", 0),
("INIT", 1);

View file

@ -20,6 +20,10 @@ class FileSystem:
encoded = data.encode("utf-8")
return zlib.crc32(encoded)
@staticmethod
def delete(path: str) -> bool:
return os.remove(path)
@staticmethod
def zip(item) -> str:
dest = f"{tempfile.gettempdir()}/{str(item[1])}"