简体   繁体   English

如何使用python flask从Google云存储提供图像

[英]How to serve an image from google cloud storage using python flask

I'm currently working on a project running flask on the Appengine standard environment, and I'm attempting to serve an image that has been uploaded onto Google Cloud Storage on my project's default Appengine storage bucket. 我目前正在一个在Appengine标准环境下运行flask的项目中工作,并且正在尝试在我的项目的默认Appengine存储分区中提供已上传到Google Cloud Storage的图像。

This is the routing code I currently have: 这是我目前拥有的路由代码:

# main.py

from google.appengine.api import images
from flask import Flask, send_file

app = Flask(__name__)

...

@app.route("/sample_route")
def sample_handler():
    myphoto = images.Image(filename="/gs/myappname.appspot.com/mysamplefolder/photo.jpg")
    return send_file(myphoto)
...

However, I am getting an AttributeError: 'Image' object has no attribute 'read' error. 但是,我得到了AttributeError: 'Image' object has no attribute 'read'错误。

The question is, how do I serve an image sourced from google cloud storage using an arbitrary route using python and flask? 问题是,如何使用python和flask的任意路由来提供来自Google云存储的图像?

EDIT: 编辑:

I am actually trying to serve an image that I have uploaded to the default Cloud Storage Bucket in my app engine project. 我实际上是在尝试提供已上传到我的App Engine项目中默认云存储桶中的图像。

I've also tried to serve the image using the following code without success: 我还尝试使用以下代码为图片提供服务,但未成功:

# main.py

from google.appengine.api import images
from flask import Flask, send_file

app = Flask(__name__)

...

@app.route("/sample_route")
def sample_handler():
    import cloudstorage as gcs
    gcs_file = gcs.open("/mybucketname/mysamplefolder/photo.jpg")
    img = gcs_file.read()
    gcs_file.close()

    return send_file(img, mimetype='image/jpeg')
...

I've used the GoogleAppEngineCloudStorageClient Python library and loaded images with code similar to the following example: 我使用了GoogleAppEngineCloudStorageClient Python库,并使用类似于以下示例的代码加载了图像:

from google.appengine.api import app_identity
import cloudstorage
from flask import Flask, send_file
import io, os

app = Flask(__name__)

# ...

@app.route('/imagetest')
def test_image():

  # Use BUCKET_NAME or the project default bucket.
  BUCKET_NAME = '/' + os.environ.get('MY_BUCKET_NAME',
                                     app_identity.get_default_gcs_bucket_name())
  filename = 'mytestimage.jpg'
  file = os.path.join(BUCKET_NAME, filename)

  gcs_file = cloudstorage.open(file)
  contents = gcs_file.read()
  gcs_file.close()

  return send_file(io.BytesIO(contents),
                   mimetype='image/jpeg')

Are you trying to accomplish something like this: 您是否正在尝试完成以下操作:

@app.route("/sample_route")
def sample_handler():
    import urllib2
    import StringIO

    request = urllib2.Request("{image url}")

    img = StringIO.StringIO(urllib2.urlopen(request).read())
    return send_file(img, mimetype='image/jpeg') # display in browser

or 要么

    return send_file(img) # download file

The image url is needed, not a relative path. 图片网址是必需的,而不是相对路径。 You could just do a redirect to the image url, but they would get a 301. 您可以直接重定向到图片网址,但会收到301。

Using google-cloud-storage==1.6.0 使用google-cloud-storage == 1.6.0

from flask import current_app as app, send_file, abort
from google.cloud import storage
import tempfile

@app.route('/blobproxy/<filename>', methods=['GET'])
def get(filename):
    if filename:
        client = storage.Client()
        bucket = client.get_bucket('yourbucketname')
        blob = bucket.blob(filename)
        with tempfile.NamedTemporaryFile() as temp:
            blob.download_to_filename(temp.name)
            return send_file(temp.name, attachment_filename=filename)
    else:
        abort(400)

I recommend looking at the docs for the path or string converters for your route, and NamedTemporaryFile defaults to delete=True so no residue. 我建议您查看您的路线的pathstring转换器的文档 ,并且NamedTemporaryFile默认为delete=True因此没有残基。 flask also figures out the mimetype if you give it a file name, as is the case here. 如果您给它提供了文件名, flask还会计算出mimetype类型,例如此处的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python 3 在谷歌云托管的 flask 网站中显示来自谷歌云存储的图像 - Display image from google cloud storage in flask website hosted on google cloud using python 3 如何在Python 2中使用Flask在Google云存储中添加子目录 - How to add subdirectory in google cloud storage using flask in python 2 如何使用Python在Android发送的Google Cloud Storage上存储图像 - how to store an image on Google Cloud Storage sent by Android using Python 如何从Python脚本上传Google Cloud Storage上的字节图像 - How to upload a bytes image on Google Cloud Storage from a Python script 如何提供Google Cloud Storage映像? - How to serve Google Cloud Storage images? 如何在Flask上使用python的qrcode提供生成的QR图像 - How to serve a generated QR Image using python's qrcode on Flask 使用谷歌应用引擎python将图像从外部链接上传到谷歌云存储 - Uploading an Image from an external link to google cloud storage using google app engine python 从云端存储到网页(Google“ Hello World”应用)提供小图片(jpg)的最简单代码是什么? - What is the simplest code to serve a small image (jpg) from cloud storage to a webpage (Google 'Hello World' app)? 如何使用 Python 将图像上传到谷歌云存储,而不将图像保存在我的本地磁盘上? - How to upload to google cloud storage an image using Python, without saving the image on my local disk? 使用Python的Google云存储 - Google Cloud Storage using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM