![](/img/trans.png)
[英]How to upload a file to Google Cloud Storage on Python 3?
[英]Python urlretrieve a file to Google Cloud Storage
我正在嘗試在GAE上部署應用程序。
我的python腳本使用urllib.request
包中的urlretrieve
函數下載圖像:
urlretrieve(img_url,os.path.join(os.getcwd(),img_dir,str(img_name) + '.jpg'))
它工作正常,只要我在本地運行腳本(在本地保存圖像),但我無法找到對GCS執行相同操作的替代方法。
如果有人能指出我如何實現這一目標,我將不勝感激。
謝謝!
該文檔顯示了如何使用幾種簡單的方法來讀取和寫入雲存儲桶。
而不是urlretrieve
,只需抓取圖像的內容,然后將響應寫入雲存儲中的文件。
image_binary = urllib.urlopen(img_url).read()
並且說您稍微修改雲存儲樣本:
import cloudstorage as gcs
def create_file(self, filename, contents, mime_type='image/jpeg'):
write_retry_params = gcs.RetryParams(backoff_factor=1.1)
gcs_file = gcs.open(filename,
'w',
content_type=mime_type,
retry_params=write_retry_params)
gcs_file.write(contents)
gcs_file.close()
# Maybe do other too stuff like make the file publicly
# accessible
並調用它來寫入文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.