Spaces:
Runtime error
Runtime error
File size: 3,037 Bytes
28a2563 2895710 a801eec b38f031 a801eec b38f031 2895710 b38f031 2895710 b38f031 2895710 b38f031 2895710 b38f031 a801eec 28a2563 a801eec 2895710 a801eec 2895710 a801eec 28a2563 a801eec 28a2563 a801eec 28a2563 a801eec 28a2563 |
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 |
from google.cloud import storage
import json
import os
class CloudStorageManager:
def __init__(self, bucket_name, credentials_str):
credentials_dict = json.loads(credentials_str)
self.client = storage.Client.from_service_account_info(credentials_dict)
self.bucket_name = bucket_name
def upload_file(self, file_path, destination_file_name):
bucket = self.client.bucket(self.bucket_name)
blob = bucket.blob(destination_file_name)
blob.upload_from_filename(file_path)
print(f'File {destination_file_name} uploaded to {self.bucket_name}.')
def download_file(self, source_file_name, destination_path):
bucket = self.client.bucket(self.bucket_name)
blob = bucket.blob(source_file_name)
blob.download_to_filename(destination_path)
print(f'File {source_file_name} downloaded to {destination_path}.')
def delete_file(self, file_name):
bucket = self.client.bucket(self.bucket_name)
blob = bucket.blob(file_name)
blob.delete()
print(f'File {file_name} deleted from {self.bucket_name}.')
def get_file_by_uuid(self, uuid):
bucket = self.client.bucket(self.bucket_name)
blobs = bucket.list_blobs(prefix=uuid)
for blob in blobs:
if blob.name.endswith('.glb'):
return blob.name
return None
def main():
# Replace 'your_bucket_name' with your actual bucket name
bucket_name = os.getenv('GOOGLE_BUCKET_NAME')
# Replace 'your_credentials_str' with your actual credentials string
# credentials_str = """
# {
# "type": "service_account",
# "project_id": "your_project_id",
# "private_key_id": "your_private_key_id",
# "private_key": "-----BEGIN PRIVATE KEY-----\nYourPrivateKey\n-----END PRIVATE KEY-----\n",
# "client_email": "your_client_email",
# "client_id": "your_client_id",
# "auth_uri": "https://accounts.google.com/o/oauth2/auth",
# "token_uri": "https://oauth2.googleapis.com/token",
# "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
# "client_x509_cert_url": "your_client_x509_cert_url"
# }
# """
credentials_str = SERVICE_ACOUNT_STUFF = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_JSON')
# Create an instance of CloudStorageManager
storage_manager = CloudStorageManager(bucket_name, credentials_str)
# Example usage:
# Upload a file
storage_manager.upload_file('local_file_path', 'destination_file_name')
# Download a file
storage_manager.download_file('source_file_name', 'local_destination_path')
# Delete a file
storage_manager.delete_file('file_name_to_delete')
# Get file by UUID
uuid = 'your_uuid'
file_name = storage_manager.get_file_by_uuid(uuid)
if file_name:
print(f"File with UUID '{uuid}' found: {file_name}")
else:
print(f"No file found with UUID '{uuid}'")
if __name__ == "__main__":
main()
|