簡體   English   中英

從 API 下載文件並使用 python 將其保存/上傳到 Google 存儲

[英]Download file from API and save/upload it to Google Storage using python

我正在嘗試從 API 下載文件並將其保存/上傳到 Google Storage。 誰能幫助我做錯了什么?

from google.cloud import storage
import urllib

#create storage client
storage_client = storage.Client()

#Get file:
url = 'https://....'
filename = 'test.xlsx'

bucket = storage_client.get_bucket('my-data-test-bucket')

#Upload:
filedata = urllib.urlretrieve(url, filename)
datafile = bucket.blob(filename)
datafile.upload_from_filename(filedata)

錯誤消息: TypeError:預期的字符串或緩沖區

如果我嘗試將其保存為這樣的字符串: datafile.upload_from_string(filedata)

我收到錯誤: TypeError: ('test.xlsx', <httplib.HTTPMessage instance at 0x7f9fc927a6c8>) 無法轉換為字節

我對 Python 沒有太多經驗,所以這可能是個愚蠢的問題。 我一直在努力解決這個問題,我感謝所有幫助:)

datafile.upload_from_filename(filedata) filedata可以嘗試用filename或所需文件的路徑替換文件數據嗎

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM