Pregunta

I am storing images on appengine using the GCS library.

After uploading the image I use the blobstore create_gs_key function and store the result on a Picture entity:

gs_key = blobstore.create_gs_key('/gs%s' % gcs_file_name

Picure entity:

class Picture(ndb.Model):
    cdate = ndb.DateTimeProperty(auto_now_add=True)
    filename = ndb.StringProperty(required=True)
    gs_key = ndb.StringProperty(required=True)

From the docs: 'You can safely persist the blob key generated by this function just as you can persist ordinary blob keys in the Blobstore API'

Every time I need to display a thumbnail of the picture I need to do this:

from google.appengine.api import images

pictures = []
for img in ndb.Picture.query().iter():
    pictures.append(images.get_serving_url(img.gs_key, 48, True))

So wondering if instead of storing the gs_key I could just store the get_serving_url or it is better to continue just storing the gs_key since the serving_url may change over time?

¿Fue útil?

Solución

The serving url will be available until you cancel it using delete_serving_url. It would be in your best interest to keep both the gs_key and the serving url.

class Picture(ndb.Model):
    cdate = ndb.DateTimeProperty(auto_now_add=True)
    filename = ndb.StringProperty(required=True)
    gs_key = ndb.StringProperty(required=True)
    _serving_url = ndb.StringProperty(indexed=False)

    @property
    def serving_url(self):
        if self.gs_key:
            if not self._serving_url:
                self._serving_url = images.get_serving_url(self.gs_key,48,True)
                self.put()
            return self._serving_url
        return ''

Each call to get_serving_url creates an RPC request so minimizing these would be ideal. Keeping the gs_key will let you delete the serving url at a later time if required, or let you do alternate operations with the uploaded file (e.g. creating more than one size thumbnail)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top