Spaces:
Sleeping
Sleeping
Delete storage_service.py
Browse files- storage_service.py +0 -44
storage_service.py
DELETED
@@ -1,44 +0,0 @@
|
|
1 |
-
import json
|
2 |
-
from google.cloud import storage
|
3 |
-
from google.oauth2 import service_account
|
4 |
-
from googleapiclient.http import MediaIoBaseDownload
|
5 |
-
|
6 |
-
|
7 |
-
class GoogleCloudStorage:
|
8 |
-
def __init__(self, service_account_key_string):
|
9 |
-
credentials_dict = json.loads(service_account_key_string)
|
10 |
-
credentials = service_account.Credentials.from_service_account_info(credentials_dict)
|
11 |
-
self.client = storage.Client(credentials=credentials, project=credentials_dict['project_id'])
|
12 |
-
|
13 |
-
def check_file_exists(self, bucket_name, file_name):
|
14 |
-
blob = self.client.bucket(bucket_name).blob(file_name)
|
15 |
-
return blob.exists()
|
16 |
-
|
17 |
-
def upload_file(self, bucket_name, destination_blob_name, file_path):
|
18 |
-
blob = self.client.bucket(bucket_name).blob(destination_blob_name)
|
19 |
-
blob.upload_from_filename(file_path)
|
20 |
-
print(f"File {file_path} uploaded to {destination_blob_name} in GCS.")
|
21 |
-
|
22 |
-
def upload_file_as_string(self, bucket_name, destination_blob_name, content):
|
23 |
-
blob = self.client.bucket(bucket_name).blob(destination_blob_name)
|
24 |
-
blob.upload_from_string(content)
|
25 |
-
print(f"String content uploaded to {destination_blob_name} in GCS.")
|
26 |
-
return None
|
27 |
-
|
28 |
-
def download_as_string(self, bucket_name, source_blob_name):
|
29 |
-
blob = self.client.bucket(bucket_name).blob(source_blob_name)
|
30 |
-
return blob.download_as_text()
|
31 |
-
|
32 |
-
def make_blob_public(self, bucket_name, blob_name):
|
33 |
-
blob = self.client.bucket(bucket_name).blob(blob_name)
|
34 |
-
blob.make_public()
|
35 |
-
print(f"Blob {blob_name} is now publicly accessible at {blob.public_url}")
|
36 |
-
|
37 |
-
def get_public_url(self, bucket_name, blob_name):
|
38 |
-
blob = self.client.bucket(bucket_name).blob(blob_name)
|
39 |
-
return blob.public_url
|
40 |
-
|
41 |
-
def upload_image_and_get_public_url(self, bucket_name, file_name, file_path):
|
42 |
-
self.upload_file(bucket_name, file_name, file_path)
|
43 |
-
self.make_blob_public(bucket_name, file_name)
|
44 |
-
return self.get_public_url(bucket_name, file_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|