我正在尝试在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 }
有人可以帮我吗?
答案 0 :(得分:0)
我为我找到了解决方案:
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的存储中了。感谢您的评论,并看到我的问题!