-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgcp_helpers.py
More file actions
58 lines (45 loc) · 1.97 KB
/
gcp_helpers.py
File metadata and controls
58 lines (45 loc) · 1.97 KB
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
from google.cloud import storage
import os
from data import GCP_CREDENTIALS, STORAGE_LINK_HEADER
def init_client():
storage_client = storage.Client.from_service_account_json(GCP_CREDENTIALS)
return storage_client
def upload_blob(bucket_name, source_file_name):
"""Uploads a file to the bucket."""
# The ID of your GCS bucket
# bucket_name = "your-bucket-name"
# The path to your file to upload
# source_file_name = "local/path/to/file"
# The ID of your GCS object
destination_blob_name = os.path.basename(source_file_name)
storage_client = init_client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(
f"File {source_file_name} uploaded to {destination_blob_name}."
)
def download_blob(bucket_name, source_blob_name, destination_path):
"""Downloads a blob from the bucket."""
# The ID of your GCS bucket
# bucket_name = "your-bucket-name"
# The ID of your GCS object
# source_blob_name = "storage-object-name"
# The path to which the file should be downloaded
destination_file_name = os.path.join(destination_path,source_blob_name)
storage_client = init_client()
bucket = storage_client.bucket(bucket_name)
# Construct a client side representation of a blob.
# Note `Bucket.blob` differs from `Bucket.get_blob` as it doesn't retrieve
# any content from Google Cloud Storage. As we don't need additional data,
# using `Bucket.blob` is preferred here.
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
print(
"Downloaded storage object {} from bucket {} to local file {}.".format(
source_blob_name, bucket_name, destination_file_name
)
)
def construct_storage_link(bucket_name, object_name):
return STORAGE_LINK_HEADER+bucket_name+'/'+object_name
# https://storage.googleapis.com/audio-data-hack/depthmodel.h5