get_serving_url silently fails with django-storages, app engine, python 3.x

68 Views Asked by At

I am trying to get the seving_url for a project that uses django-storages with google cloud storage for media files.

I am trying to serve the files with get_serving_url, but I get a silent failure here with no text logged in the exception.

The blobkey generates correctly from what I can see however the image = images.get_serving_url(blobkey, secure_url=True) raises an exception with no error text.

This is what I have done:

#storage_backends.py

class GoogleCloudMediaStorage(GoogleCloudStorage):
    """GoogleCloudStorage suitable for Django's Media files."""

    def __init__(self, *args, **kwargs):
        if not settings.MEDIA_URL:
            raise Exception('MEDIA_URL has not been configured')
        kwargs['bucket_name'] = setting('GS_MEDIA_BUCKET_NAME')
        super(GoogleCloudMediaStorage, self).__init__(*args, **kwargs)

    #this works fine
    def url(self, name):
        """.url that doesn't call Google."""
        return urljoin(settings.MEDIA_URL, name)

    #https://programtalk.com/python-examples/google.appengine.api.images.get_serving_url/_
    #This does not work yet
    def serving_url(self, name):
        logging.info('serving url called')
        if settings.DEBUG:
            return urljoin(settings.MEDIA_URL, name)
        else:    
            # Your app's GCS bucket and any folder structure you have used. 
            try:
                logging.info('trying to get serving_url')
                filename = settings.GS_MEDIA_BUCKET_NAME + '/' + name
                logging.info(filename)
                blobkey = blobstore.create_gs_key('/gs/' + filename) 
                logging.info('This is a blobkey')
                logging.info(blobkey)
                image = images.get_serving_url(blobkey, secure_url=True)
                return image
            except Exception as e:
                logging.warn('didnt work')
                logging.warn(e)
                return urljoin(settings.MEDIA_URL, name)

I have appengine-python-standard installed and I have wrapped my application

#main.py

from antiques_project.wsgi import application
from google.appengine.api import wrap_wsgi_app

app = wrap_wsgi_app(application)

I also have this in my app.yaml

app_engine_apis: true

I have tried to generate the blobkey in different ways (with and without bucket) I have also tried secure_url = False and True So far nothing seems to work

EDIT: Got a traceback in the logs:

Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/appengine/api/images/init.py", line 2013, in get_serving_url_hook rpc.check_success() File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/appengine/api/apiproxy_stub_map.py", line 614, in check_success self.__rpc.CheckSuccess() File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/appengine/api/apiproxy_rpc.py", line 149, in CheckSuccess raise self.exception File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/appengine/runtime/default_api_stub.py", line 276, in _CaptureTrace f(**kwargs) File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/appengine/runtime/default_api_stub.py", line 269, in _SendRequest raise self._TranslateToError(parsed_response) File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/appengine/runtime/default_api_stub.py", line 138, in _TranslateToError raise apiproxy_errors.ApplicationError(response.application_error.code, google.appengine.runtime.apiproxy_errors.ApplicationError: ApplicationError: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/srv/config/storage_backends.py", line 50, in serving_url image = images.get_serving_url(blobkey, secure_url=True) File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/appengine/api/images/init.py", line 1911, in get_serving_url return rpc.get_result() File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/appengine/api/apiproxy_stub_map.py", line 648, in get_result return self.__get_result_hook(self) File "/layers/google.python.pip/pip/lib/python3.10/site-packages/google/appengine/api/images/init.py", line 2015, in get_serving_url_hook raise _ToImagesError(e, readable_blob_key) google.appengine.api.images.TransformationError

0

There are 0 best solutions below