【问题标题】:How to upload an image from client side form to Google cloud storage bucket?如何将图像从客户端表单上传到谷歌云存储桶?
【发布时间】:2015-02-18 17:08:18
【问题描述】:

有一个上传图片的表单。现在我想将它存储在谷歌云存储中并返回到页面上打印。形式是:-

<form action="http://master-engine-799.appspot.com/uploadimage" method="POST" enctype="multipart/form-data">
Upload File: <input type="file" name="file"><br>
<input type="submit" name="submit" value="Submit"> 
</form>

.py 文件是

import webapp2
import logging

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers


class UploadImageHandler(webapp2.RequestHandler):    
  def post(self):        
# code that will upload the image to my bucket on cloud storage    
upload_url = blobstore.create_upload_url('/upload',gs_bucket_name='userimagebucket')

class UploadImageHandler(webapp2.RequestHandler):
    # code to show uploaded image in bucket
app = webapp2.WSGIApplication([('/uploadimage', UploadImageHandler)],
                              debug=True)

【问题讨论】:

    标签: html google-app-engine python-2.7 google-cloud-storage webapp2


    【解决方案1】:

    create_upload_url 提供的upload_url 是您要用作表单本身的“操作”的URL,而不是/uploadimage。所以你的 HTML 表单应该看起来更像:

    <form action="CONTENTS_OF_UPLOAD_URL_HERE" method="POST" enctype="multipart/form-data">
      <input type="file" name="file"><br>
      <input type="submit" name="submit" value="Submit">
    </form>
    

    然后,您的上传处理程序可以将用户重定向到显示结果的页面。示例:

    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]
        self.redirect('/serve/%s' % blob_info.key())
    

    【讨论】:

    • 实际上表单在客户端。它会从表单调用服务。那么我将如何提供upload_url,因为它将被动态创建......
    【解决方案2】:
    from __future__ import with_statement
    
    import cloudstorage as gcs
    
    import webapp2
    import logging
    
    from google.appengine.ext import blobstore
    from google.appengine.ext.webapp import blobstore_handlers
    
    
    
    def CreateFile(filename,imageFile):
      with gcs.open(filename, 'w', content_type = 'image/jpeg') as f:
        f.write(imageFile)
        f.close()
    
    
      blobstore_filename = '/gs' + filename
      return blobstore.create_gs_key(blobstore_filename)
    
    class MyImageHandler(webapp2.RequestHandler):
          def post(self):
            bucket='yourbucketname'
            imageFile = self.request.get('file')
            naemofFile=self.request.get('filename1')
            fileName='/yourbucketname'+'/'+naemofFile
            blob_key = CreateFile(fileName,imageFile)
            logging.info("Blob-Key "+blob_key)
            imageUrl = 'https://%(bucket)s.storage.googleapis.com/%(file)s' % {'bucket':bucket, 'file':naemofFile}
    
        app = webapp2.WSGIApplication([('/myimagehandler', MyImageHandler)],
                                      debug=True)
    

    表格是这样的

    <html>
    <body>
    <form action="http://your-app-id.appspot.com/myimagehandler" method="POST" enctype="multipart/form-data">
       <input type="file" name="file"><br>
    File Name : <input type="text" name="filename1">   <input type="submit" name="submit" value="Submit"> </form>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 2015-03-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 2016-03-21
      相关资源
      最近更新 更多