Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing data to google cloud storage using python

I cannot find a way to to write a data set from my local machine into the google cloud storage using python. I have researched a a lot but didn't find any clue regarding this. Need help, thanks

like image 776
Aditya Bhatt Avatar asked Apr 28 '17 14:04

Aditya Bhatt


People also ask

How do I upload data to Google Cloud Storage?

Drag and drop the desired files from your desktop or file manager to the main pane in the Google Cloud console. Click the Upload Files button, select the files you want to upload in the dialog that appears, and click Open.

How do I import a CSV file into Google Cloud Storage?

Find your app and select your app options caret ( ^ ). Select All Settings > Raw Data Export > CSV Upload. Select Google Cloud Storage from the dropdown menu. Upload your Service Account Key credential file.


2 Answers

Quick example, using the google-cloud Python library:

from google.cloud import storage

def upload_blob(bucket_name, source_file_name, destination_blob_name):
  """Uploads a file to the bucket."""
  storage_client = storage.Client()
  bucket = storage_client.get_bucket(bucket_name)
  blob = bucket.blob(destination_blob_name)

  blob.upload_from_filename(source_file_name)

  print('File {} uploaded to {}.'.format(
      source_file_name,
      destination_blob_name))

More examples are in this GitHub repo: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/storage/cloud-client

like image 152
Brandon Yarbrough Avatar answered Nov 15 '22 16:11

Brandon Yarbrough


from googleapiclient import discovery

from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

service = discovery.build('storage', 'v1', credentials=credentials)

filename = 'file.csv'

bucket = 'Your bucket name here'         

body = {'name': 'file.csv'}

req = service.objects().insert(bucket=bucket, body=body, media_body=filename)

resp = req.execute()
like image 35
HImanshu Narang Avatar answered Nov 15 '22 17:11

HImanshu Narang