与Google App Engine(Python)一起使用的Google云端存储示例

时间:2014-10-07 17:54:33

标签: python google-app-engine google-cloud-storage

任何人都有使用谷歌应用引擎(python)谷歌云存储的代码示例?

到目前为止,我见过的最好的答案来自我之前发布的问题的回答:https://code.google.com/p/appengine-gcs-client/source/browse/trunk/python/demo/main.py

但是,无论连接如何,代码似乎都能成功运行,而且我似乎无法在我的GCS存储桶中找到这些文件

1 个答案:

答案 0 :(得分:2)

main.py此脚本直接上传到GCS,通过更改create_upload_url函数并删除gb_bucket_name,它将使用blob存储。

import os
import urllib
import webapp2

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

class MainHandler(webapp2.RequestHandler):
  def get(self):
    upload_url = blobstore.create_upload_url('/upload', gs_bucket_name='bucket_name')
    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> <input type="submit"
        name="submit" value="Submit"> </form></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]
    self.redirect('/serve/%s' % blob_info.key())

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)

app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/upload', UploadHandler),
                               ('/serve/([^/]+)?', ServeHandler)],
                              debug=True)