Created
January 19, 2018 16:26
-
-
Save martjanz/1141834117ff14f34ace55334e50db0e to your computer and use it in GitHub Desktop.
Google Cloud Storage on Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Google Cloud Storage snippet | |
# | |
# Requirements: glob, os, google-cloud-storage | |
# | |
import glob | |
import os | |
from google.cloud import storage | |
GCS_PROJECT = '???' # i.e. Google Cloud project name | |
GCS_BUCKET = '???' # i.e. Google Cloud bucket name | |
# Initialize client | |
client = storage.Client(project=GCS_PROJECT) | |
# Get storage bucket (like a base path) | |
bucket = client.get_bucket(GCS_BUCKET) | |
# | |
# Upload files | |
# | |
LOCAL_BASE_PATH = './uploads/' | |
GCS_BASE_PATH = '/' # Absolute path (optional, assumes bucket root if not provided) ** | |
# Array where public file links will be stored | |
files_uri = [] | |
for filename in glob.iglob(os.path.join(LOCAL_BASE_PATH, '*.*')): | |
blob = storage.Blob(GCS_BASE_PATH + os.path.basename(filename), bucket) | |
blob.upload_from_filename(filename) | |
# Make this blob (file) public | |
blob.make_public() | |
# Retrieve download URI for the file | |
files_uri += [blob.media_link] | |
# Remove local file | |
os.remove(filename) | |
# URLs to uploaded files | |
return files_uri | |
# | |
# Download files | |
# | |
BASE_PATH = './' | |
FILES_TO_DOWNLOAD = [] | |
# Create base data path if not exist | |
if not os.path.exists(BASE_PATH): | |
os.makedirs(BASE_PATH) | |
# Download files from GCS to ./data directory | |
for file in FILES_TO_DOWNLOAD: | |
local_file_path = os.path.join(BASE_PATH, os.path.basename(file['filename'])) | |
# Check if file previously exists | |
if not os.path.exists(local_file_path): | |
client = storage.Client(project=file['project']) | |
bucket = client.get_bucket(file['bucket']) | |
blob = storage.Blob(file['filename'], bucket) | |
blob.download_to_filename(local_file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment