2
votes

I am trying to create a little server in Google Cloud Platform that receives an image and put it in Storage.

But when server is deploying always writes in logs the same error:

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)

My function is like this:

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 }

Can someone help me?

1
Is your code running on App Engine Standard or Flexible environment? - LundinCast
Have you followed the steps explained in the google.cloud import storage: cannot import storage ? - Yurci

1 Answers

1
votes

I found a solution for me:

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()

Now I can put images at Storage in GCP. Thanks for comment and see my question!