![](/img/trans.png)
[英]Create new csv file in Google Cloud Storage from cloud function
[英]How to create a new CSV file and then store it in Google Cloud Storage?
我有以下代碼:
import csv
from google.cloud import storage
import os
os.environ["Credientials"]
storage_client = storage.Client()
mylist = ['Row1','Row2','Row3'] # Rows I'm making for CSV file
with open('Afile.csv', 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) # Making CSV file
wr.writerow(mylist)
s = myfile
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))
upload_blob('BucketName', s ,'storage location') # Uploading the newly made CSV file to Google Cloud Storage
在創建新CSV文件(由s
)的地方,我正嘗試將新創建的文件上傳到Google Cloud存儲,而不是從本地文件路徑上傳。 運行此命令時,出現以下錯誤:
TypeError: expected string or bytes-like object
如何使它正常工作,以便我可以制作一個新的CSV文件,然后將其上傳到Google Cloud Storage? 如果可以的話?
謝謝您的幫助!
with open('Afile.csv', 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) # Making CSV file
wr.writerow(mylist)
s = myfile
s
是file like object
的file like object
而不是字符串或字節的對象。 您實際上是在傳遞文件本身,而不是傳遞文件名。 您可能應該將Afile.csv
作為字符串傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.