2016-11-10 27 views
1

我想在GAE上部署一個應用程序。Python的urlretrieve文件到谷歌雲存儲

urlretrieve(img_url,os.path.join(os.getcwd(),img_dir,str(img_name) + '.jpg')) 

它,因爲我在本地運行腳本工作正常,只要(在本地保存的圖像),但我無法找到:
我使用urlretrieve功能從urllib.request包python腳本下載圖片與GCS一樣的替代方案。

如果有人能指出我如何實現這一點,我將不勝感激。

謝謝!

+0

它如何不適合你?你得到一個錯誤?哪個錯誤? –

回答

2

documentation顯示瞭如何使用幾個簡單的方法來讀取和寫入雲存儲存儲區。

而不是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 

並調用寫入文件。

+0

它沒有按預期工作:) PS:很抱歉的回覆 –

相關問題