-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudstoragedriver.py
More file actions
46 lines (33 loc) · 1.36 KB
/
cloudstoragedriver.py
File metadata and controls
46 lines (33 loc) · 1.36 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
from google.cloud import storage
from google.cloud.storage import Blob
import sys
import os
class Cloud():
def __init__(self, path='data/'):
self.path = path
self.client = storage.Client.from_service_account_json('config/service_account.json')
self.landsat_bucket = 'gcp-public-data-landsat'
"""
Downloads a single Bucket of Tiffs from the paste week's worth of USGS Satellite data using Google Cloud API.
"""
def DownloadBucket(self, bucketID):
blob_uri = bucketID.split(self.landsat_bucket)[1] + '/'
print("FETCHING BLOB - ", blob_uri)
bucket = self.client.get_bucket(self.landsat_bucket)
blobs = bucket.list_blobs(prefix=blob_uri[1:])
directory = self.path + blob_uri
for b in blobs:
print('BLOB NAME: ', b.name)
blob_file_name = b.name.split('/')[-1]
datapath = self.ensure_path(self.path, blob_uri, blob_file_name)
with open(datapath + blob_file_name, 'wb') as file_obj:
b.download_to_file(file_obj)
return bucket
# thanks Stack Overflow :D
def ensure_path(self, datapath, blob_uri, blob_name):
cd = os.getcwd()
path = cd + datapath + blob_uri
if not os.path.exists(path):
os.makedirs(path)
open(path + blob_name, 'w').close()
return path