Writing data to google cloud storage using python

北慕城南 提交于 2020-01-13 11:59:27

问题


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


回答1:


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




回答2:


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()


来源:https://stackoverflow.com/questions/43682521/writing-data-to-google-cloud-storage-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!