2012-09-17 23 views
1

在我的應用程序中,我創建了一個在App Engine儀表板中正確上傳的blob。但是,這個創建的文件需要通過電子郵件發送給相應的人。爲了做到這一點,我需要將文件本身作爲附件或靜態網址,以供此人下載。我無法弄清楚如何從blobkey獲取靜態URL。從AppEngine接收blob中的靜態URL

這是創建一個文件中的代碼,它沒有什麼特別的,但:

file_name = files.blobstore.create(mime_type='text/csv') 
with files.open(file_name, 'a') as f: 
    f.write(dataset) 
files.finalize(file_name) 
blob_key = files.blobstore.get_blob_key(file_name) 
blob_info = blobstore.BlobInfo.get(blob_key) 

new_url = blob_key.urlsafe() 

回答

5

如果要提供服務的文件,看看Blobstore Overview - Serving a Blob

如果您想將其作爲附件發送,請參閱Attachments Documentation。您將需要fetch the contents of the blob,然後將其附加到郵件。

from google.appengine.ext import blobstore 

# blob_key = ... 

# Instantiate a BlobReader for a given Blobstore value. 
blob_reader = blobstore.BlobReader(blob_key) 

# Read the entire value into memory. This may take a while depending 
# on the size of the value and the size of the read buffer, and is not 
# recommended for large values. 
blob_contents = blob_reader.read() 
+0

忽略我的(刪除)答案。這個答案和它的鏈接解釋了這一切。 –

0

看看在AppEngine Blobstore docs,他們解釋如何檢索與Blob存儲條目工作的一個好工作。以下是文檔中的示例。

from google.appengine.ext import blobstore 
from google.appengine.ext.webapp import blobstore_handlers 
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): 
    def get(self, resource): 
    resource = str(urllib.unquote(resource)) 
    blob_info = blobstore.BlobInfo.get(resource) 
    self.send_blob(blob_info) 
0

我的用例不同,但我通過查找URL路徑從blobstore提供靜態內容。這裏是模型和get函數。

class StaticContent(db.Model): 
    body = db.BlobProperty() 
    content_type = db.StringProperty() 
    last_modified = db.DateTimeProperty(required=True, auto_now=True) 
    etag = aetycoon.DerivedProperty(lambda x: hashlib.sha1(x.body).hexdigest()) 

def get(path): 
    return StaticContent.get_by_key_name(path) 

你可以看到step1 tag of my master branch in my git hub repo

我wepapp2處理程序的詳細說明,你可能還檢查了Nick Johnson's blog post on serving static content via a blogstore