-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathcloudstoragedownload.py
More file actions
62 lines (50 loc) · 1.78 KB
/
cloudstoragedownload.py
File metadata and controls
62 lines (50 loc) · 1.78 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
59
60
61
62
#!/usr/bin/python
# -*- coding: utf-8 -*-
# cloudstoragedownload.py
# It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP).
# Download an object from a Cloud Storage bucket to a local file.
# You must provide 3 parameters:
# BUCKET_NAME = Bucket name
# OBJECT_NAME = Object name in the bucket
# LOCAL_FILE_NAME = Local file name
import sys
import os
from google.cloud import storage
from google.cloud.exceptions import NotFound
from google.cloud.exceptions import Forbidden
def main():
# Make a list of command line arguments, omitting the [0] element
# which is the script itself.
args = sys.argv[1:]
if len(args) < 3:
print('Not enough parameters.\n'\
'Proper Usage is: python cloudstoragedownload.py '\
'<BUCKET_NAME> <OBJECT_NAME> <LOCAL_FILE_NAME>')
sys.exit(1)
bucket_name = args[0]
blob_name = args[1]
local_file_name = args[2]
print('Bucket: ' + bucket_name)
print('Object: ' + blob_name)
print('Local file: ' + local_file_name)
print('Downloading an object from a Cloud Storage bucket to a local file ...')
# Instantiate the client.
client = storage.Client()
try:
# Get the bucket.
bucket = client.get_bucket(bucket_name)
# Instantiate the object.
blob = bucket.blob(blob_name)
# Downloads an object from the bucket.
blob.download_to_filename(local_file_name)
print('\nDownloaded')
except NotFound:
print('Error: Bucket/Blob does NOT exists!!')
pass
except Forbidden:
print('Error: Forbidden, you do not have access to it!!')
pass
return
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()