-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-fetch.py
55 lines (41 loc) · 1.38 KB
/
data-fetch.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
# Databricks notebook source
# MAGIC %pip install google-cloud-storage tqdm
# COMMAND ----------
import os
from pathlib import Path
from shutil import rmtree
from google.cloud import storage
from tqdm import tqdm
# COMMAND ----------
DATA_DIR = '/Volumes/prd_csc_mega/sColom15/vColom15/Data'
dbutils.fs.mkdirs(DATA_DIR) # Creates the given directory if it does not exist
# COMMAND ----------
# MAGIC %sh du -h /Volumes/prd_csc_mega/sColom15/vColom15/Data
# COMMAND ----------
# Check if the given file path already exists on DBFS
def dbfs_file_exists(path):
try:
dbutils.fs.ls(path)
return True
except Exception as e:
if 'java.io.FileNotFoundException' in str(e):
return False
else:
raise
# COMMAND ----------
bucket_name = 'validaciones_tmsa'
storage_client = storage.Client.create_anonymous_client()
blobs = storage_client.list_blobs(bucket_name)
# blobs includes files in nested folders
for blob in tqdm(blobs):
# skip folders
if blob.name.endswith("/"):
continue
target = f'{DATA_DIR}/{blob.name}'
# Only download files that are not yet downloaded
if not dbfs_file_exists(target):
dbutils.fs.mkdirs(str(Path(target).parent))
print(f'downloading to: {target}')
blob.download_to_filename(filename=target, client=storage_client)
# COMMAND ----------
# MAGIC %sh du -h /Volumes/prd_csc_mega/sColom15/vColom15/Data