-
Notifications
You must be signed in to change notification settings - Fork 2
/
storageservice.py
155 lines (129 loc) · 4.3 KB
/
storageservice.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import base64
import hashlib
import binascii
import datetime
import streamlit as st
from streamlit.runtime.uploaded_file_manager import UploadedFile
from google.oauth2 import service_account
from google.cloud import storage
from google.cloud.storage.retry import _should_retry
from google.api_core import retry
from typing import Optional, Iterable, Union, Tuple
from io import BytesIO
# Default project and bucket
default_gproject = st.secrets["gcp_service_account"]["project_id"]
default_bucket = st.secrets["firebase"]["storageBucket"]
# Create API client.
credentials = service_account.Credentials.from_service_account_info(
dict(st.secrets["gcp_service_account"])
)
client = storage.Client(credentials=credentials)
_INITIAL_DELAY = 1.0 # seconds
_MAXIMUM_DELAY = 4.0
_DELAY_MULTIPLIER = 2.0
_DEADLINE = 60.0
retry_custom = retry.Retry(
_should_retry, _INITIAL_DELAY, _MAXIMUM_DELAY, _DELAY_MULTIPLIER, _DEADLINE
)
def compute_bytes_md5hash(data: bytes):
md5hex = hashlib.md5(data).hexdigest()
encoded_bytes = base64.b64encode(binascii.unhexlify(md5hex))
sig = encoded_bytes.rstrip(b"\n").decode("UTF-8")
return sig
def get_buckets(
project: str = default_gproject,
prefix: Optional[str] = None,
page_size: int = 500,
timeout: int = 8,
retry: retry.Retry = retry_custom,
print_names: bool = False,
) -> Iterable[storage.Bucket]:
buckets = client.list_buckets(
project=project,
prefix=prefix,
page_size=page_size,
timeout=timeout,
retry=retry,
)
if print_names:
for bucket in buckets:
print(bucket)
return buckets
def get_blobs(
bucket_or_name: Union[str, storage.Bucket] = default_bucket,
prefix: Optional[str] = None,
page_size: int = 500,
timeout: int = 8,
retry: retry.Retry = retry_custom,
print_names: bool = False,
) -> Optional[Iterable[storage.Blob]]:
"""
Prints a list of all the blobs in the specified storage bucket,
and returns them as an iterable
"""
blobs = client.list_blobs(
bucket_or_name, prefix=prefix, page_size=page_size, timeout=timeout, retry=retry
)
bucket_name = bucket_or_name if type(bucket_or_name) == str else bucket_or_name.name
if print_names:
for blob in blobs:
print(blob)
return blobs
def get_bucket(
bucket_name: str, timeout: int = 8, retry: retry.Retry = retry_custom
) -> Optional[storage.Bucket]:
bucket = client.lookup_bucket(bucket_name, timeout=timeout, retry=retry)
return bucket
def create_bucket(
bucket_name: str,
project: str = default_gproject,
billing_project: str = default_gproject,
bucket_location: str = "us-east1",
timeout: int = 8,
retry: retry.Retry = retry_custom,
) -> storage.bucket.Bucket:
bucket = client.create_bucket(
bucket_name,
location=bucket_location,
project=project,
user_project=billing_project,
timeout=timeout,
retry=retry,
)
return bucket
def upload_blob_data(
blob_name: str,
blob_data: Union[UploadedFile, str],
bucket_name: str = default_bucket,
content_type: Optional[str] = None,
timeout: int = 8,
retry: retry.Retry = retry_custom,
) -> Tuple[storage.Blob, str]:
bucket = client.bucket(bucket_name)
blob = bucket.blob(blob_name)
if blob.exists(client, timeout=timeout, retry=retry):
return (blob, get_blob_url(blob))
if type(blob_data) == UploadedFile:
blob.upload_from_file(
blob_data, content_type=content_type, timeout=timeout, retry=retry
)
elif type(blob_data) == str:
blob.upload_from_string(
blob_data, content_type=content_type, timeout=timeout, retry=retry
)
return (blob, get_blob_url(blob))
def get_blob_url(blob: storage.Blob):
url = blob.generate_signed_url(
expiration=datetime.timedelta(minutes=60), version="v4", method="GET"
)
return url
def download_blob_data(
bucket_name: str, blob_name: str, return_type: str = "bytes"
) -> Union[BytesIO, str]:
bucket = client.bucket(bucket_name)
content = None
if return_type == "bytes":
content = bucket.blob(blob_name).download_as_bytes()
elif return_type == "string":
content = bucket.blob(blob_name).download_as_text(encoding="utf-8")
return content