-
Notifications
You must be signed in to change notification settings - Fork 0
/
notebook_starter.py
94 lines (59 loc) · 2.31 KB
/
notebook_starter.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
88
89
90
91
92
93
94
# To display the output of all commands
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all" # none last
# ======== Setting up project and bucket ===========
#PROJECT_ID = 'your-google-cloud-project'
PROJECT_ID = 'deep-learning-course-v3'
BUCKET_NAME='lesson1' # Optional (we will create one)
from google.cloud import storage
storage_client = storage.Client(project=PROJECT_ID)
# ======== Working with buckets ===========
f'# List buckets'
buckets = storage_client.list_buckets()
bucket_names=[x.name for x in buckets]
bucket_names
f'# Create dummy bucket'
SUFFIX="-0124234e7" # To help making names unique
BUCKET_NAME="dummy_bucket"+SUFFIX
if BUCKET_NAME not in bucket_names:
bucket = storage_client.create_bucket(BUCKET_NAME)
f'# Access bucket called {BUCKET_NAME}'
#bucket = storage_client.get_bucket(BUCKET_NAME)
bucket = storage_client.get_bucket(BUCKET_NAME)
f'# List files on bucket {bucket.name}'
[print(x.name) for x in bucket.list_blobs()]
#[print(x) for x in bucket.list_blobs()]
# ======== Transfering files ===========
DESTINATION_FILE_NAME=SOURCE_FILE_NAME="dummy_file"
f'# Create a dummy file to upload'
!echo "hello there" >> dummy_file
!ls -lh
f'# Upload file {SOURCE_FILE_NAME} to bucket'
blob = bucket.blob(DESTINATION_FILE_NAME)
blob.upload_from_filename(SOURCE_FILE_NAME)
[print(x.name) for x in bucket.list_blobs()]
f'# Download file {DESTINATION_FILE_NAME}'
blob = bucket.blob(DESTINATION_FILE_NAME)
blob.download_to_filename(SOURCE_FILE_NAME+"downloaded")
!ls -lh
#====== working with pickle and cloud ======
import pickle
PICKLE_FILE_NAME="objects.pkl"
LIST_OBJECTS_TO_SAVE=[a,b,c]
# Saving the objects to local file
pickle.dump(LIST_OBJECTS_TO_SAVE, open(PICKLE_FILE_NAME,"wb"), protocol=-1)
!ls -lh
# Copy file to cloud
SOURCE_FILE_NAME=DESTINATION_FILE_NAME=PICKLE_FILE_NAME
f'# Upload file {SOURCE_FILE_NAME} to bucket'
blob = bucket.blob(DESTINATION_FILE_NAME)
blob.upload_from_filename(SOURCE_FILE_NAME)
# Verify
[print(x.name) for x in bucket.list_blobs()]
# Copy file from Cloud into notebook
f'# Download file {DESTINATION_FILE_NAME}'
blob = bucket.blob(DESTINATION_FILE_NAME)
blob.download_to_filename(SOURCE_FILE_NAME+"downloaded")
!ls -lh
# Getting back the objects
a1,b1,c1 = pickle.load(open(PICKLE_FILE_NAME+"downloaded","rb"))