簡體   English   中英

將視頻上傳到Google應用引擎blobstore

[英]Uploading a video to google app engine blobstore

我正在嘗試將視頻文件與具有大量屬性的記錄相關聯,但似乎無法允許用戶以一種形式執行所有操作 - 命名視頻,提供說明並回答一些問題,然后上傳文件。

以下是我要執行的步驟:

  1. 向用戶提供包含具有以下字段的表單的頁面:名稱,描述,文件選擇器。
  2. 該文件存儲為blob,id與名稱和描述一起記錄。

有沒有人有任何我可以學習的例子,或者你可以指點我的教程? 來自谷歌的那個只顯示上傳文件並被重定向到它。

感謝和抱歉的新問題!

這是我用來上傳圖片並將它們與文章相關聯的代碼。 最難的是獲取文章ID以獲取上傳處理程序,我通過將文件名設置為文章ID來解決問題。

from lib import urllib2_file
from lib.urllib2_file import UploadFile

# this view serves a task in a queue
def article(request):
       article = Article.objects.get(id=form.cleaned_data['article'])

       try:
            image = StringIO(urllib2.urlopen(image_url).read())
        except (urllib2.HTTPError, DownloadError):
            article.parsed = True
            article.save()
        else:
            image = UploadFile(image, '.'.join([str(article.id), image_url.rsplit('.', 1)[1][:4]]))
            upload_url = blobstore.create_upload_url(reverse('Articles.views.upload'))

            try:
                urllib2.urlopen(upload_url, {'file': image})
            except (DownloadError, RequestTooLargeError):
                pass

    return HttpResponse(json.dumps({'status': 'OK'}))

def upload(request):
    if request.method == 'POST':
        blobs = get_uploads(request, field_name='file', populate_post=True)

        article = Article.objects.get(id=int(blobs[0].filename.split('.')[0]))
        article.media = blobs[0].filename
        article.parsed = True
        article.save()

        return HttpResponseRedirect(reverse('Articles.views.upload'))
    else:
        return HttpResponse('meow')

    def upload(request):
        if request.method == 'POST':
            blobs = get_uploads(request, field_name='file', populate_post=True)

            article = Article.objects.get(id=int(blobs[0].filename.split('.')[0]))
            article.media = blobs[0].filename
            article.parsed = True
            article.save()

            return HttpResponseRedirect(reverse('Articles.views.upload'))
        else:
            return HttpResponse('meow')

# this serves the image
def image(request):
    blob = BlobInfo.gql("WHERE filename='%s' LIMIT 1" % request.form.cleaned_data['id'])[0]

    return HttpResponse(BlobReader(blob.key()).read(),
                        content_type=blob.content_type)

你還需要這個http://fabien.seisen.org/python/urllib2_file/

我就是這樣做的。 它比你想象的更直截了當。 請注意以下內容取自Blobstore概述 “當Blobstore重寫用戶的請求時,上傳文件的MIME部分將其主體清空,並將blob鍵添加為MIME部分標題。 所有其他表單字段和部分將被保留並傳遞給上傳處理程序 。” 在上傳處理程序中,您可以使用其他表單字段執行任何操作。

    class Topic(db.Model):
        title = db.StringProperty(multiline=False)
        blob = blobstore.BlobReferenceProperty()
        imageurl = db.LinkProperty()

    class MainHandler(webapp.RequestHandler):
        def get(self):
            upload_url = blobstore.create_upload_url('/upload')
            self.response.out.write('<html><body>')
            self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
            self.response.out.write("""Upload File: <input type="file" name="file"><br>
            <div><label>Title:</label></div>
            <div><textarea name="title" rows="1" cols="25"></textarea></div><input type="submit" 
                name="submit" value="Submit"> </form>""")
            self.response.out.write('<br><br><h2>TOPIC LIST</h2><table border="1"><tr><td>')
            for topic in Topic.all():                
                self.response.out.write('<div><img src="%s=s48"/>' % topic.imageurl)
                self.response.out.write('<div><b>Image URL: </b><i>%s</i></div>' % topic.imageurl)
                self.response.out.write('<div><b>Title: </b><i>%s</i></div>' % topic.title)
            self.response.out.write('</td></tr></table><br>') 
            self.response.out.write('</body></html>')

    class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
        def post(self):
            upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
            blob_info = upload_files[0]
            topic = Topic()
            topic.title = self.request.get("title")
            topic.blob = blob_info.key()
            topic.imageurl = images.get_serving_url(str(blob_info.key()))
            topic.put()        
            self.redirect('/')
def main():
    application = webapp.WSGIApplication(
          [('/', MainHandler),
           ('/upload', UploadHandler),
          ], debug=True)
    run_wsgi_app(application)

暫無
暫無

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

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