使用python将数据写入谷歌云存储

时间:2017-04-28 14:23:09

标签: python-3.x google-cloud-storage

我找不到使用python将本地计算机上的数据集写入谷歌云存储的方法。我已经研究了很多,但没有找到任何关于此的线索。需要帮助,谢谢

2 个答案:

答案 0 :(得分:5)

快速示例,使用google-cloud Python库:

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

此GitHub回购中有更多示例:https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/storage/cloud-client

答案 1 :(得分:1)

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