-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp_storage.py
97 lines (72 loc) · 2.66 KB
/
gcp_storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import json
import os
from libcloud.storage.types import Provider
from libcloud.storage.providers import get_driver
from libcloud.storage.drivers import google_storage
from libcloud.storage.drivers.google_storage import GoogleStorageDriver
from cloud_storage import CloudStorage
from libcloud.storage.base import Object
class GCPCloudStorage(CloudStorage):
def __init__(self, key, secret, owner=None, date_str=None):
super().__init__(self)
"""Initializes a GCPCloudStorage object
https://libcloud.readthedocs.io/en/latest/storage/drivers/google_storage.html
Args:
key: service account email
secret: service account private key
owner: owner metadata (default: {None})
date_str: date created metadata (default: {None})
"""
self.key = key
self.secret = None
self.container_name = 'smcs-123'
if isJson(secret):
self.secret = secret['private_key']
else:
self.secret = secret
self.driver = GoogleStorageDriver(key=self.key, secret=self.secret)
self.container = driver.get_container(container_name='smcs-123')
self.metaData = {'meta_data': {}}
setMetaData(owner, date_str)
pass
def setMetaData(self, owner, date_str, object_name):
if owner:
metaData['meta_data']['owner'] = owner
if date_str:
metaData['meta_data']['created'] = date_str
metaData['meta_data'][''] = object_name
def createContainer(self, container_name):
self.driver.create_container(container_name)
def deleteContainer(self, container_name):
self.driver.delete_container()
def listFiles(self, container_name):
pass
def uploadFile(self):
pass
def uploadFiles(self):
pass
def getFile(self, file_name) -> bytes:
obj = driver.get_object(self.container_name, file_name)
gen = driver.download_object_as_stream(obj)
file_as_bytes = next(gen)
return file_as_bytes
def getFiles(self, files):
file_list = []
for f in files:
file_list.append(getFile(f))
def isJson(myjson):
try:
json_object = json.loads(myjson)
except ValueError:
return False
return True
# FILE_PATH = 'rabbit.svg'
# cls = get_driver(Provider.GOOGLE_STORAGE)
# driver = GoogleStorageDriver(key=client_email, secret=private_key, ...)
# container = driver.get_container(container_name='SMCS')
# extra = {'meta_data': {'owner': 'myuser', 'created': '2018-11-14'}}
# with open(FILE_PATH, 'rb') as iterator:
# obj = driver.upload_object_via_stream(iterator=iterator,
# container=container,
# object_name='rabbit.svg',
# extra=extra)