Created
January 22, 2016 20:51
-
-
Save stuntgoat/063282a1044e75186461 to your computer and use it in GitHub Desktop.
This file contains 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
import io | |
import logging | |
logging.getLogger('googleapiclient.discovery').setLevel(logging.WARNING) | |
logging.getLogger('oauth2client.client').setLevel(logging.WARNING) | |
from apiclient.discovery import build | |
from apiclient.http import ( | |
MediaIoBaseDownload, | |
MediaFileUpload, | |
) | |
from oauth2client.client import GoogleCredentials | |
LOGGER = logging.getLogger('storage') | |
def get_storage(): | |
credentials = GoogleCredentials.get_application_default() | |
return build('storage', 'v1', credentials=credentials) | |
def download(bucket, object_name, to_path): | |
service = get_storage() | |
request = service.objects().get_media(bucket=bucket, | |
object=object_name) | |
fh = io.FileIO(to_path, mode='w') | |
downloader = MediaIoBaseDownload(fh, request, chunksize=1024 * 1024) | |
done = False | |
while done is False: | |
status, done = downloader.next_chunk() | |
if status: | |
LOGGER.info("Download %d%%." % int(status.progress() * 100)) | |
LOGGER.info("Download Complete!") | |
def upload(file_path, fn, bucket): | |
media = MediaFileUpload(file_path, mimetype='application/octet-stream', | |
chunksize=1024 * 1024, resumable=True) | |
service = get_storage() | |
o = service.objects() | |
o.insert(name=fn, bucket=bucket, media_body=media).execute() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment