0

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 ?

user3817378
  • 119
  • 10

2 Answers2

0

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
user3817378
  • 119
  • 10
  • Tip for you. You can create the equivalent of a virtual env for your Python2 projects by creating a ```lib folder``` in your project root environment and installing your dependencies to it. This way, you don't do global installations. Follow the documentation here - https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27 – NoCommandLine Mar 07 '22 at 15:49
  • Thanks @NoCommandLine , I have another question about how can i generate sign url with python2.7 and app engine ? – user3817378 Mar 07 '22 at 18:03
  • I thought you fixed your issue. You added an answer where you said you installed google cloud for python2.7 Are you saying that did not resolve your problem? – NoCommandLine Mar 07 '22 at 21:19
  • @NoCommandLine Yes ,I solved the issue with the help of mentioned answer but in local mac. – user3817378 Mar 08 '22 at 02:50
  • @NoCommandLine But when I try to upload the code to gcp , getting same error. Hence I edited the question headline as : generating signurl with python2.7 and google app engine. Is there any other gae library which generate signurl? – user3817378 Mar 08 '22 at 02:51
0

....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.

NoCommandLine
  • 5,044
  • 2
  • 4
  • 15