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:
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()
2 Comments:
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
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
Post a Comment
<< Home