【问题标题】:App-engine: cannot import name storage python 2.7应用引擎:无法导入名称存储 python 2.7
【发布时间】:2019-01-16 23:15:24
【问题描述】:

我正在尝试在 Google Cloud Platform 中创建一个小型服务器,用于接收图像并将其放入存储中。

但是当服务器部署时总是在日志中写入同样的错误:

ImportError:无法导入名称存储 在(/base/data/home/apps/my_environment/my_server:000000000000000.000000000000000000/main.py:15) 在 LoadObject (/base/alloc/tmpfs/dynamic_runtimes/python27/8882c914eb6132e9_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py:85) 在 _LoadHandler (/base/alloc/tmpfs/dynamic_runtimes/python27/8882c914eb6132e9_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py:299) 在句柄处 (/base/alloc/tmpfs/dynamic_runtimes/python27/8882c914eb6132e9_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py:240)

我的功能是这样的:

from google.cloud import storage

...

def post_icon(request):
    file = request.files['file']
    package = request.form['package']
    if file and _allowed_file(file.filename):
        gcs = storage.Client()
        bucket = gcs.get_bucket(CLOUD_STORAGE_APP_ICONS_SEGMENT)
        blob = bucket.blob(file.filename)
        blob.upload_from_string(
        file.read(),
        content_type=file.content_type
        )
        return {'code': 200 , 'message': blob.public_url }

    else:
        return {'code': 400 }

有人可以帮我吗?

【问题讨论】:

标签: python google-app-engine import


【解决方案1】:

我找到了适合我的解决方案:

import googleapiclient.discovery
import googleapiclient.http

storage = googleapiclient.discovery.build('storage', 'v1')

def post_icon(request):
    file = request.files['file']
    package = request.form['package']
    app = request.form['app']
    if file and _allowed_file(file.filename) and not _have_app_icon(package):
        body = {
            'name': file.filename,
        }
        req = storage.objects().insert(
            bucket=CLOUD_STORAGE_APP_ICONS_SEGMENT, body=body,
            media_body=googleapiclient.http.MediaIoBaseUpload(
                 file.stream, 'image/jpeg'))
        resp = req.execute()

现在我可以将图像放在 GCP 中的存储中。感谢您的评论并查看我的问题!

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 2018-11-24
    • 2017-10-22
    • 2015-07-10
    • 2021-11-26
    • 1970-01-01
    • 2018-06-01
    • 2013-07-05
    • 1970-01-01
    相关资源
    最近更新 更多