繁体   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