gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:46 29 4
gpt4 key购买 nike

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

关于python - 应用引擎 : cannot import name storage python 2. 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51767866/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com