The Golden Spot

I hope this helps someone who is learning about Linux and web application programming with Python, Django, and Javascript.

Friday, January 22, 2016

Upload / Download to Google Cloud Storage with the Python API

It took a bit of experimentation but here's a way to upload large files to the Google Storage API:
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()
view raw storage.py hosted with ❤ by GitHub

2 Comments:

Anonymous eu4 console commands said...

Hello, i want to upload files to Google cloud storage in java
i have already created upload file gui in jsp now i want to upload file directly on google cloud storage through jsp/java code
how i can do this ?
please help me



10:36 AM  
Anonymous ark scorched earth obelisk locations said...

Good tutorial. Very helpful. Assuming that I have a folder "folder-1" inside my bucket "awesome-bucket", how do I upload to "folder-1" which is inside the "awesome-bucket" bucket, using the command line? Thanks



6:43 AM  

Post a Comment

<< Home