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= TARGET_BUCKET=
# Cloud provider "gcs, aws, azure" # Cloud provider "gcs, aws, azure"
PROVIDER_NAME= SERVICE_NAME=
# Path to service account key file # Path to service account key file
PROVIDER_KEY= SERVICE_KEY=

View file

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

View file

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

View file

@ -1,10 +1,26 @@
import os
from google.cloud import storage from google.cloud import storage
from ..fs.utils import get_file
# Client for Google Cloud Storage # Client for Google Cloud Storage
class StorageClient: class StorageClient:
def __init__(self, bucket): def __init__(self):
client = storage.Client() os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.getenv("SERVICE_KEY")
self.bucket = client.bucket(bucket)
def upload(self, item): client = storage.Client()
blob = self.bucket.blob() 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 INSERT INTO flags
VALUES VALUES
("ZIP", 1), ("COMPRESS", 1),
("BUCKET_OK", 0), ("BUCKET_OK", 0),
("INIT", 1); ("INIT", 1);

View file

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