Skip to main content
 首页 » 编程设计

python之应用引擎 : cannot import name storage python 2. 7

2024年12月31日8zhwl

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

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

ImportError: cannot import name storage at (/base/data/home/apps/my_environment/my_server:000000000000000.000000000000000000/main.py:15) at LoadObject (/base/alloc/tmpfs/dynamic_runtimes/python27/8882c914eb6132e9_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py:85) at _LoadHandler (/base/alloc/tmpfs/dynamic_runtimes/python27/8882c914eb6132e9_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py:299) at Handle (/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 } 

有人可以帮助我吗?

请您参考如下方法:

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

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 的存储中。感谢您发表评论并查看我的问题!