Skip to content

Commit 1a187b8

Browse files
committed
Update README.
1 parent 2603594 commit 1a187b8

6 files changed

Lines changed: 226 additions & 203 deletions

File tree

.github/gcpcloudstorage@2x.jpg

75.6 KB
Loading

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![GitHub Stars](https://img.shields.io/github/stars/toddbirchard/tableau-extraction.svg?style=flat-square&colorB=ebcb8b&colorA=4c566a)](https://github.com/hackersandslackers/googlecloud-storage-tutorial/stargazers)
88
[![GitHub Forks](https://img.shields.io/github/forks/toddbirchard/tableau-extraction.svg?style=flat-square&colorA=4c566a&colorB=ebcb8b)](https://github.com/hackersandslackers/googlecloud-storage-tutorial/network)
99

10-
![Google Cloud Storage Python SDK Tutorial](https://storage.googleapis.com/hackersandslackers-cdn/2019/06/gcp-cloudstorage@2x.jpg)
10+
![Google Cloud Storage Python SDK Tutorial](./.github/gcpcloudstorage@2x.jpg)
1111

1212
Source for the accompanying tutorial here: [https://hackersandslackers.com/google-cloud-storage-with-python/](https://hackersandslackers.com/google-cloud-storage-with-python/)
1313

googlecloud_storage_tutorial/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
def init_script():
1818
"""Initialize script demonstration."""
19-
print(upload_files(BUCKET_NAME, BUCKET_DIR, LOCAL_DIR))
20-
print(list_files())
21-
print(download_random_file(LOCAL_DIR))
22-
print(rename_file(fake.unique.first_name()))
23-
print(delete_file(BUCKET_NAME))
19+
upload_files(BUCKET_NAME, BUCKET_DIR, LOCAL_DIR)
20+
list_files()
21+
download_random_file(LOCAL_DIR)
22+
rename_file(fake.unique.first_name())
23+
delete_file(BUCKET_NAME)
2424
upload_files(BUCKET_NAME, BUCKET_DIR, LOCAL_DIR)

googlecloud_storage_tutorial/storage.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,75 +19,65 @@ def list_files() -> List[Optional[str]]:
1919
"""
2020
List all objects with file extension in a GCP bucket.
2121
22-
:param str bucket_name: Human-readable GCP bucket name.
23-
:param str bucket_dir: Bucket directory in which object exists.
24-
2522
:returns: List[Optional[str]]
2623
"""
2724
blobs = bucket.list_blobs(prefix=BUCKET_DIR)
2825
blob_file_list = [blob.name for blob in blobs if "." in blob.name]
2926
return blob_file_list
3027

3128

32-
def pick_random_file() -> str:
29+
def pick_random_file() -> Tuple[Blob, str]:
3330
"""
3431
Pick a `random` file from GCP bucket.
3532
36-
:param str bucket_name: Human-readable GCP bucket name.
37-
38-
:returns: str
33+
:returns: Tuple[Blob, str]
3934
"""
4035
blobs = list_files()
4136
rand = randint(0, len(blobs) - 1)
4237
blob = bucket.blob(blobs[rand])
4338
return blob, blob.name
4439

4540

46-
def download_random_file(local_dir: str) -> Tuple[Blob, str]:
41+
def download_random_file(local_dir: str) -> None:
4742
"""
4843
Download random file from GCP bucket.
4944
50-
:param str bucket_name: Human-readable GCP bucket name.
51-
:param str bucket_dir: Bucket directory in which object exists.
5245
:param str local_dir: Local file path to upload/download files.
5346
54-
:returns: str
47+
:returns: None
5548
"""
5649
blob, blob_filename = pick_random_file()
5750
blob.download_to_filename(f"{local_dir}/{blob.name.split('/')[-1]}")
58-
return blob, blob_filename
51+
print(f"Downloaded {blob_filename} to `{local_dir}`.")
5952

6053

61-
def delete_file(bucket_name: str) -> str:
54+
def delete_file(bucket_name: str) -> None:
6255
"""
6356
Delete file from GCP bucket.
6457
6558
:param str bucket_name: Human-readable GCP bucket name.
66-
:param str bucket_dir: Bucket directory in which object exists.
6759
68-
:returns: str
60+
:returns: None
6961
"""
7062
blob, blob_filename = pick_random_file()
7163
bucket.delete_blob(blob_filename)
72-
return f"{blob_filename} deleted from bucket: {bucket_name}."
64+
print(f"{blob_filename} deleted from bucket: {bucket_name}.")
7365

7466

75-
def rename_file(new_filename: str) -> str:
67+
def rename_file(new_filename: str) -> None:
7668
"""
7769
Rename a file in GCP bucket.
7870
79-
:param str bucket_name: Human-readable GCP bucket name.
80-
:param str bucket_dir: Bucket directory from which to extract object.
8171
:param str new_filename: New file name for Blob object.
8272
83-
:returns: str
73+
:returns: None
8474
"""
8575
blob, blob_filename = pick_random_file()
8676
bucket.rename_blob(blob, new_name=new_filename)
87-
return f"{blob_filename} renamed to {new_filename}."
77+
print(f"{blob_filename} renamed to {new_filename}.")
8878

8979

90-
def upload_files(bucket_name: str, bucket_dir: str, local_dir: str) -> str:
80+
def upload_files(bucket_name: str, bucket_dir: str, local_dir: str) -> None:
9181
"""
9282
Upload files to GCP bucket.
9383
@@ -100,7 +90,6 @@ def upload_files(bucket_name: str, bucket_dir: str, local_dir: str) -> str:
10090
files = [f for f in listdir(local_dir) if isfile(join(local_dir, f))]
10191
for file in files:
10292
local_file = f"{local_dir}/{file}"
103-
print(f"local file = {local_file}\n")
10493
blob = bucket.blob(f"{bucket_dir}/{file}")
10594
blob.upload_from_filename(local_file)
106-
return f"Uploaded {files} to '{bucket_name}' bucket."
95+
print(f"Uploaded {files} to '{bucket_name}' bucket.")

0 commit comments

Comments
 (0)