-
Notifications
You must be signed in to change notification settings - Fork 8
/
cloudStorageManagerLib.py
87 lines (67 loc) · 2.96 KB
/
cloudStorageManagerLib.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
"""
Helpful functions for managing Google Cloud Storage (GCS) buckets and blobs
geeViz.cloudStorageManagerLib includes functions for renaming, seeing if a blob exists, and deleting blobs.
"""
"""
Copyright 2024 Ian Housman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Script to help with basic cloudStorage management
# Intended to work within the geeViz package
######################################################################
import geeViz.geeView
from google.cloud import storage
######################################################################
# Function to list all blobs in a bucket
# Returns a list of blob objects
# Use the blob.name to get the name
def list_blobs(bucket_name):
# storage client instance
storage_client = storage.Client(project=geeViz.geeView.project_id)
# get bucket by name
bucket = storage_client.get_bucket(bucket_name)
# list files stored in bucket
all_blobs = bucket.list_blobs()
return list(all_blobs)
######################################################################
# Function to batch rename blobs in a bucket
def rename_blobs(bucket_name, old_name, new_name):
"""Renames a group of blobs."""
# storage client instance
storage_client = storage.Client(project=geeViz.geeView.project_id)
# get bucket by name
bucket = storage_client.get_bucket(bucket_name)
# list files stored in bucket
all_blobs = list(bucket.list_blobs())
# Renaming all files to lowercase:
for blob in all_blobs:
on = str(blob.name)
nn = on.replace(old_name, new_name)
new_blob = bucket.rename_blob(blob, new_name=nn)
print("Blob {} has been renamed to {}".format(on, nn))
######################################################################
# Return wether a filename exists or not
def gcs_exists(bucket, filename):
storage_client = storage.Client(project=geeViz.geeView.project_id)
stats = storage.Blob(bucket=storage_client.bucket(bucket), name=filename).exists(
storage_client
)
return stats
######################################################################
# !! Dangerous !! - cannot be undone
# Delete a specified filename
def delete_blob(bucket, filename):
storage_client = storage.Client(project=geeViz.geeView.project_id)
out = storage.Blob(bucket=storage_client.bucket(bucket), name=filename).delete(
storage_client
)
print("Deleted:", filename)
######################################################################