使用谷歌应用引擎将图像发送到谷歌云存储 [英] Sending images to google cloud storage using google app engine

查看:20
本文介绍了使用谷歌应用引擎将图像发送到谷歌云存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 中使用谷歌应用引擎编写了一个简单的 webapp,它允许用户上传图像并将其存储在某个地方(现在我只是使用基于教程的 blob 商店).

I wrote a simple webapp using google app engine in python that allows users to upload images and have it stored somewhere (for now I'm just using the blob store based on the tutorials).

现在我想将图像发送到谷歌云存储,但不知道该怎么做.它们在打开文件时提供两种模式:a"和r".据我所知,它们都不适用于二进制流.

Now I want to send the images to google cloud storage, but am not sure how to do this. They provide two modes when opening a file: "a" and "r". Neither of them, to my knowledge, are for binary streams.

如何将图像发送到谷歌云存储?代码片段或引用会很好.我还计划发送小型音频样本和其他二进制数据.

How can I send an image to google cloud storage? Code snippets or references would be nice. I am planning to send small audio samples as well, and other binary data.

另外,如果用户希望删除该图像,我该如何删除它?似乎没有可用的删除方法.

Also, how can I delete the image if a user wishes to delete it? There doesn't seem to be a delete method available.

推荐答案

这是一个简单的上传处理程序,它将写入上传到 bigstore 的 blob.请注意,您需要将应用的服务帐号添加到管理 bigstore 存储桶的团队帐号中.

Here's a simple upload handler that will write the blob that was uploaded to bigstore. Note you will need to have added your app's service account to the team account that is managing the bigstore bucket.

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
  def post(self):
    upload_files = self.get_uploads('file')
    blob_info = upload_files[0]
    in_file_name = files.blobstore.get_file_name(blob_info.key())
    infile = files.open(in_file_name)
    out_file_name = '/gs/mybucket/' + blob_info.filename
    outfile = files.gs.create(out_file_name,
                              mime_type = blob_info.content_type)

    f = files.open(outfile, 'a')
    chunkSize = 1024 * 1024
    try:
      data = infile.read(chunkSize)
      while data:
        f.write(data)
        data = infile.read(chunkSize)
    finally:
      infile.close()
      f.close()

    files.finalize(outfile)

这篇关于使用谷歌应用引擎将图像发送到谷歌云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆