Home > OS >  GCS generate sign url with python 2.7 and google app engine
GCS generate sign url with python 2.7 and google app engine

Time:03-08

I am trying to generate signUrl in python 2.7 using v4 signing process as given here below is the code given on the link:

def generate_singed_url(bucket_name, blob_name):
    """Generates a v4 signed URL for downloading a blob.

    Note that this method requires a service account key file. You can not use
    this if you are using Application Default Credentials from Google Compute
    Engine or from the Google Cloud SDK.
    """
    # bucket_name = 'your-bucket-name'
    # blob_name = 'your-object-name'

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(blob_name)

    url = blob.generate_signed_url(
        version="v4",
        # This URL is valid for 15 minutes
        expiration=datetime.timedelta(minutes=15),
        # Allow GET requests using this URL.
        method="GET",
    )

    print("Generated GET signed URL:")
    print(url)
    print("You can use this URL with any user agent, for example:")
    print("curl '{}'".format(url))
    return url

This is how I am trying to import the storage:

from google.cloud import storage

But I am getting the error as:

File "<input>", line 1, in <module>
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
 module = self._system_import(name, *args, **kwargs)
ImportError: No module named google.cloud

I tried installing google-cloud-storage library also tried installing lots of other google specific libraries but it's still giving the same import error.

Tried: ImportError: No module named google.cloud

ModuleNotFoundError: No module named 'google.cloud' (uninstalling the libraries and again installing)

Edit: how can i generate signurl using python2.7 and app engine ?

CodePudding user response:

I was running two python versions in my mac: python2.7 and python3 , the libraries was already installed for python3 but was missing for python2.7. Used below command to install the library:

python2.7 -m pip install --upgrade google-cloud-storage --user

CodePudding user response:

....Answering based on your comments....

If you were using Python3, you would have a virtual env and a requirements.txt file and when you deploy your project to Production, GAE would first install the contents of your requirements.txt file before running your App.

Since you're running Python2, you don't have that requirements.txt file and virtual env concept with GAE. Your App has to be uploaded together with any third party library you need (i.e. any library outside of these has to uploaded with your App). You do this via the 'lib' folder that I mentioned in the comments - (instructions for creating the folder can be found here). I believe the instructions are simple enough to follow.

I would say to first try using the lib concept on your local machine (this would mean installing the cloud storage library to that folder). It also means you have to run your app with dev_appserver.py.

Note that when you install google cloud storage client to the lib folder and run your app with dev_appserver.py, GAE will use the package in your lib folder instead of the one installed globally on your laptop.

If it works (i.e. you're able to create signed urls on your local machine), then go ahead and deploy to production.

If you have problems creating the lib folder and installing packages to it, let me know.

  • Related