Skip to content

Instantly share code, notes, and snippets.

@Hispar
Last active February 28, 2020 08:43
Show Gist options
  • Save Hispar/3b919d289cb495d7e570ddce37584eb7 to your computer and use it in GitHub Desktop.
Save Hispar/3b919d289cb495d7e570ddce37584eb7 to your computer and use it in GitHub Desktop.
Google Cloud Storage python
# -*- coding: utf-8 -*-
# Python imports
import glob
import os
# 3rd Party imports
from django.conf import settings
from google.cloud import storage
# App imports
def get_bucket():
client = storage.Client(credentials=settings.GS_CREDENTIALS, project=settings.GS_PROJECT_ID)
bucket = client.get_bucket(settings.GS_BUCKET_NAME)
return bucket
def copy_directory(origin, destination):
client = storage.Client(credentials=settings.GS_CREDENTIALS, project=settings.GS_PROJECT_ID)
bucket = client.get_bucket(settings.GS_BUCKET_NAME)
blobs = client.list_blobs(bucket, prefix=origin)
for blob in blobs:
bucket.copy_blob(blob, bucket, blob.name.replace(origin, destination))
def upload_directory_to_gcs(bucket, local_path, gcs_path):
assert os.path.isdir(local_path)
for local_file in glob.glob(local_path + '/**'):
if not os.path.isfile(local_file):
continue
upload_from_filename(bucket, local_path, local_file, gcs_path)
def get_file_from_bucket(bucket, remote_path):
blob = bucket.blob(remote_path)
return blob.download_as_string()
def upload_from_filename(bucket, local_path, local_file, gcs_path):
remote_path = os.path.join(gcs_path, local_file[1 + len(local_path):])
blob = bucket.blob(remote_path)
blob.upload_from_filename(local_file)
def upload_from_file(bucket, file, file_dest):
filename = file_dest + file.name
blob = bucket.blob(filename)
blob.upload_from_file(file, rewind=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment