Skip to content

Commit

Permalink
Update README.
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbirchard committed Aug 8, 2024
1 parent 2603594 commit 1a187b8
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 203 deletions.
Binary file added .github/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![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)
[![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)

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

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

Expand Down
10 changes: 5 additions & 5 deletions googlecloud_storage_tutorial/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

def init_script():
"""Initialize script demonstration."""
print(upload_files(BUCKET_NAME, BUCKET_DIR, LOCAL_DIR))
print(list_files())
print(download_random_file(LOCAL_DIR))
print(rename_file(fake.unique.first_name()))
print(delete_file(BUCKET_NAME))
upload_files(BUCKET_NAME, BUCKET_DIR, LOCAL_DIR)
list_files()
download_random_file(LOCAL_DIR)
rename_file(fake.unique.first_name())
delete_file(BUCKET_NAME)
upload_files(BUCKET_NAME, BUCKET_DIR, LOCAL_DIR)
37 changes: 13 additions & 24 deletions googlecloud_storage_tutorial/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,75 +19,65 @@ def list_files() -> List[Optional[str]]:
"""
List all objects with file extension in a GCP bucket.
:param str bucket_name: Human-readable GCP bucket name.
:param str bucket_dir: Bucket directory in which object exists.
:returns: List[Optional[str]]
"""
blobs = bucket.list_blobs(prefix=BUCKET_DIR)
blob_file_list = [blob.name for blob in blobs if "." in blob.name]
return blob_file_list


def pick_random_file() -> str:
def pick_random_file() -> Tuple[Blob, str]:
"""
Pick a `random` file from GCP bucket.
:param str bucket_name: Human-readable GCP bucket name.
:returns: str
:returns: Tuple[Blob, str]
"""
blobs = list_files()
rand = randint(0, len(blobs) - 1)
blob = bucket.blob(blobs[rand])
return blob, blob.name


def download_random_file(local_dir: str) -> Tuple[Blob, str]:
def download_random_file(local_dir: str) -> None:
"""
Download random file from GCP bucket.
:param str bucket_name: Human-readable GCP bucket name.
:param str bucket_dir: Bucket directory in which object exists.
:param str local_dir: Local file path to upload/download files.
:returns: str
:returns: None
"""
blob, blob_filename = pick_random_file()
blob.download_to_filename(f"{local_dir}/{blob.name.split('/')[-1]}")
return blob, blob_filename
print(f"Downloaded {blob_filename} to `{local_dir}`.")


def delete_file(bucket_name: str) -> str:
def delete_file(bucket_name: str) -> None:
"""
Delete file from GCP bucket.
:param str bucket_name: Human-readable GCP bucket name.
:param str bucket_dir: Bucket directory in which object exists.
:returns: str
:returns: None
"""
blob, blob_filename = pick_random_file()
bucket.delete_blob(blob_filename)
return f"{blob_filename} deleted from bucket: {bucket_name}."
print(f"{blob_filename} deleted from bucket: {bucket_name}.")


def rename_file(new_filename: str) -> str:
def rename_file(new_filename: str) -> None:
"""
Rename a file in GCP bucket.
:param str bucket_name: Human-readable GCP bucket name.
:param str bucket_dir: Bucket directory from which to extract object.
:param str new_filename: New file name for Blob object.
:returns: str
:returns: None
"""
blob, blob_filename = pick_random_file()
bucket.rename_blob(blob, new_name=new_filename)
return f"{blob_filename} renamed to {new_filename}."
print(f"{blob_filename} renamed to {new_filename}.")


def upload_files(bucket_name: str, bucket_dir: str, local_dir: str) -> str:
def upload_files(bucket_name: str, bucket_dir: str, local_dir: str) -> None:
"""
Upload files to GCP bucket.
Expand All @@ -100,7 +90,6 @@ def upload_files(bucket_name: str, bucket_dir: str, local_dir: str) -> str:
files = [f for f in listdir(local_dir) if isfile(join(local_dir, f))]
for file in files:
local_file = f"{local_dir}/{file}"
print(f"local file = {local_file}\n")
blob = bucket.blob(f"{bucket_dir}/{file}")
blob.upload_from_filename(local_file)
return f"Uploaded {files} to '{bucket_name}' bucket."
print(f"Uploaded {files} to '{bucket_name}' bucket.")
Loading

0 comments on commit 1a187b8

Please sign in to comment.