Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docstrings to all classes and functions #20

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions eedl/google_cloud.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import os
import re
from pathlib import Path
from typing import Union
from typing import List, Union

import requests


from google.cloud import storage # type: ignore


def get_public_export_urls(bucket_name: str, prefix: str = ""):
def get_public_export_urls(bucket_name: str, prefix: str = "") -> List[str]:
"""
Downloads items from a *public* Google Storage bucket without using a GCloud login. Filters only to files
with the specified prefix
:param bucket_name:
:param prefix: A prefix to use to filter items in the bucket - only URLs where the path matches this prefix will be returned - defaults to all files
:return: list of urls
Downloads items from a *public* Google Cloud Storage Bucket without using a GCloud login. Filters only to files.
with the specified prefix.

:param bucket_name: Name of the Google Cloud Storage Bucket to pull data from.
:type bucket_name: str
:param prefix: A prefix to use to filter items in the bucket - only URLs where the path matches this prefix will be returned - defaults to all files.
:type prefix: str
:return: A list of urls.
:rtype: List[str]
"""

base_url = "http://storage.googleapis.com/"
base_url = "https://storage.googleapis.com/"
request_url = f"{base_url}{bucket_name}/"

# get the content of the bucket (it needs to be public
Expand All @@ -33,7 +37,17 @@ def get_public_export_urls(bucket_name: str, prefix: str = ""):
return filtered


def download_public_export(bucket_name: str, output_folder: Union[str, Path], prefix: str = ""):
def download_public_export(bucket_name: str, output_folder: Union[str, Path], prefix: str = "") -> None:
"""

:param bucket_name: Name of the Google Cloud Storage Bucket to pull data from.
:type bucket_name: str
:param output_folder: Destination folder for exported data.
:type output_folder: Union[str, Path]
:param prefix: A prefix to use to filter items in the bucket - only URLs where the path matches this prefix will be returned - defaults to all files.
:type prefix: str
:return: None.
"""
# get the urls of items in the bucket with the specified prefix
urls = get_public_export_urls(bucket_name, prefix)

Expand All @@ -49,13 +63,26 @@ def download_export(bucket_name: str,
output_folder: Union[str, Path],
prefix: str,
delimiter: str = "/",
autodelete: bool = True):
autodelete: bool = True) -> None:

"""Downloads a blob from the bucket.

Modified from Google Cloud sample documentation at
https://cloud.google.com/storage/docs/samples/storage-download-file#storage_download_file-python
and
https://cloud.google.com/storage/docs/samples/storage-list-files-with-prefix

:param bucket_name: Name of the Google Cloud Storage Bucket to pull data from.
:type bucket_name: str
:param output_folder: Destination folder for exported data.
:type output_folder: Union[str, Path]
:param prefix: A prefix to use to filter items in the bucket - only URLs where the path matches this prefix will be returned - defaults to all files.
:type prefix: str
:param delimiter: Delimiter used for getting the list of blobs in the Google Cloud Storage Bucket. Defaults to "/"
:type delimiter: str
:param autodelete: Bool for deleting blobs once contents have been installed. Defaults to True
:type autodelete: bool
:return: None
"""
# The ID of your GCS bucket
# bucket_name = "your-bucket-name"
Expand Down
Loading