使用python将数据写入google云存储

vhmi4jdf  于 2023-01-27  发布在  Python
关注(0)|答案(5)|浏览(140)

我无法找到一种方法来写一个数据集从我的本地机器到谷歌云存储使用python。我已经研究了很多,但没有找到任何线索,关于这一点。需要帮助,谢谢

mwyxok5s

mwyxok5s1#

简单示例,使用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 repo中:https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/storage/cloud-client

ki1q1bka

ki1q1bka2#

当我们想把一个字符串写入GCS bucket blob时,唯一需要做的修改是使用blob.upload_from_string(your_string)而不是blob.upload_from_filename(source_file_name)

from google.cloud import storage

def write_to_cloud(your_string):
    client = storage.Client()
    bucket = client.get_bucket('bucket123456789')
    blob = bucket.blob('PIM.txt')
    blob.upload_from_string(your_string)
disbfnqx

disbfnqx3#

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

oug3syen4#

在前面的答案中,我仍然忽略了最简单的方法,即使用open()方法。
可以按如下方式使用blob.open()

from google.cloud import storage
    
def write_file():
    client = storage.Client()
        bucket = client.get_bucket('bucket-name')
        blob = bucket.blob('path/to/new-blob-name.txt') 
        ## Use bucket.get_blob('path/to/existing-blob-name.txt') to write to existing blobs
        with blob.open(mode='w') as f:
            for line in object: 
                f.write(line)

您可以在此处找到更多示例和片段:https://github.com/googleapis/python-storage/tree/main/samples/snippets

7qhs6swi

7qhs6swi5#

from google.cloud import storage

def write_to_cloud(buffer):
    client = storage.Client()
    bucket = client.get_bucket('bucket123456789')
    blob = bucket.blob('PIM.txt')
    blob.upload_from_file(buffer)

虽然Brandon's answer确实将文件上传到了Google云,但它是通过上传文件而不是写入文件来完成的,这意味着在上传到云之前,文件需要存在于磁盘上。
我提出的解决方案使用一个“内存中”的有效负载(buffer参数),然后将其写入云。要写入内容,您需要使用upload_from_file而不是upload_from_filename,其他一切都是相同的。

相关问题