2011-03-20 94 views
1

我用this question作爲模板來解決同樣的問題,但是我在發帖時遇到了問題。我有這些組件:如何將Web圖像保存到App Engine的blobstore?

  1. HTML 形式帶有圖像URL的文本框。這個職位,以...
  2. 一個處理,是以貼出網址,對其進行編碼,並使用urlfetch再次發佈它...
  3. 單獨文件上傳處理,做實際節能。

如果我使用文件輸入,組件#3本身工作正常。但我不太明白如何從圖片網址獲得urlfetch的需求。我的流程要麼超時,要麼得到最終處理程序的500響應。

# 1 
class URLMainHandler(RequestHandler): 
    def get(self): 
     return render_response('blob/upload_url.html', 
           upload_url=url_for('blobstore/upload/url')) 
# 2   
class URLUploadHandler(RequestHandler): 
    def post(self): 
     import urllib 
     # Get the posted image URL. 
     data = urllib.urlencode({'file': self.request.form.get('file')}) 
     # Post image to blobstore by calling POST on the file upload handler. 
     result = urlfetch.fetch(url=blobstore.create_upload_url(url_for('blobstore/upload')), 
           payload=data, 
           method=urlfetch.POST) 

     return self.redirect(url_for('blobstore/url'), result.status_code) 

# 3 
class UploadHandler(RequestHandler, BlobstoreUploadMixin): 
    def post(self): 
     # 'file' is the name of the file upload field in the form. 
     upload_files = self.get_uploads('file') 
     blob_info = upload_files[0] 
     response = redirect_to('blobstore/serve', resource=blob_info.key()) 
     # Clear the response body. 
     response.data = '' 
     return response 

再次,this is the process I'm following。謝謝你的幫助!

+0

#2處理程序只有30秒來獲取文件並將其上傳到blobstore處理程序。 – systempuntoout 2011-03-20 21:00:18

回答

3

您可以在不使用blobstore api的情況下實現同樣的功能。我認爲你必須得到URL並使用urlfetch()。content方法獲取內容並將其存儲爲blob屬性。

url = "imageurl" 
result = urlfetch.fetch(url) 
if result.status_code == 200: 
    prof.avatar = db.Blob(result.content) 

有關作爲blob存儲和提供數據存儲圖像的進一步參考。

你可以看到這個帖子以獲得更多關於store-images-in-datastore

+0

謝謝 - 這簡單得多。我也發現這是一個很好的服務圖片資源:http://code.google.com/appengine/articles/python/serving_dynamic_images.html – 2011-03-23 02:33:43

3

你不能只包括圖像作爲Blob存儲區HTTP請求的有效負載,並期望它瞭解它做什麼。 blobstore預計會有一個application/multipart-form-data類型的消息,這是瀏覽器在上傳到Blobstore時提供的消息。有一個圖書館爲你做這個here

SDK的未來版本將包括以編程方式在blobstore中存儲blob的功能,該功能可以避免需要這種令人討厭的黑客攻擊。

但是,如果您的圖像大小小於1MB,則更簡單的解決方案是將圖像直接存儲在數據存儲中,如Abdul在其答案中所示。

+0

謝謝,尼克。是否可以混合並匹配Blobstore,以便在上傳圖片後提供圖片,以便您不必爲每個模型製作自定義處理程序?一個例子是在這裏http://stackoverflow.com/questions/4763715/tipfy-how-to-display-blob-in-template/4767873#4767873,但它不適合我。畢竟,BlobProperty沒有密鑰。 Blobstore中是否有一個功能可以彌合這個差距? – 2011-03-23 02:32:46

+1

@Wraith不,您只能使用Blobstore來提供存儲在Blobstore中的圖像。儘管如此,您可以創建一個模型來存儲圖像,並使用ReferenceProperty來引用來自其他模型的圖像。 – 2011-03-23 03:03:09

+0

使用hack建議。給出這個國王的錯誤............. RequestTooLargeError:API調用file.Append()的請求太大。感謝這篇文章,我們希望相同的API訪問將很快提供...... – 2011-08-13 10:57:08

相關問題