Anybody know of an example of this? I haven't been able to find one in Google's documentation.
相关问题
- How to get the background from multiple images by
- java.lang.NullPointerException at java.io.PrintWri
- Why do Dataflow steps not start?
- __call__() missing 1 required positional argument:
- Try to load image with Highgui.imread (OpenCV + An
相关文章
- Is there a size limit for HTTP response headers on
- How do I create a persistent volume claim with Rea
- appcfg.py command not found
- How do I append metadata to an image in Matlab?
- Google app engine datastore string encoding proble
- GKE does not scale to/from 0 when autoscaling enab
- Angular route not working when used with Google Ap
- Python open jp2 medical images - Scipy, glymur
I was also highly confused as google doesn't really provide a working example of the getServingUrl() for JAVA programmers - Ollie, I think the example code you have given above is in Python?
I did some coding to see how it works and here is a working code snippet in java (one can easily take the working example from google's site: https://cloud.google.com/appengine/docs/java/blobstore/ and replace the code written under the Serve.java with this piece of code):
This will take the image you have post to your blobstore, crop it so that it becomes a nice square with 200 as its width and height and then print it out in HTML so that you can see the thumbnail.
Hope this helps someone out there!
I have been using the following code to upload images and then serve them using the CDN. Please refer to the comments in the code for the explanation.
Here're the model classes
Let me know if anything is not clear here.
Despite the documentation, I was pretty confused for a while too.
Now that I understand it better (I think!), I'll offer an example.
get_serving_url()
is in thegoogle.appengine.ext.blobstore
class, and takes one positional argument, theBlobKey
.A
BlobKey
can be constructed from a string:blobstore.BlobKey('this is the key')
.So, that gives us everything we need for a basic implementation of
get_serving_url()
:All fine and dandy so far.
The function also takes three keyword arguments, as documented. These are
size
,crop
,secure_url
, andrpc
.secure_url = True
simply returns anhttps
url instead ofhttp
(default,False
)rpc
is anRPC
object of some settings for asynchronous processing. I don't understand it enough to explain, or indeed to use it myself!crop = True
crops the image square, by even proportions.size
is what confused me at first. It does not generate different URLs per se. The only difference is in the suffix=sXX
, which you are free to set yourself.Personally, I store the original size URL in my
db.Model
, and then doimgUrl+'=s150'
(for example) wherever used. There is no need to callget_serving_url()
for each different size you need, no performance hit, because it is doing exactly the same.Note also that the size specified is the largest dimension of the image. This is curiously hidden in the docs - I assumed it must be width for a while, but if the image is 'portrait', of course it is the height.
You can also append
-c
(equivalent tocrop=True
).So, for our more complete (though I lack the knowledge to demonstrate use of an
RPC
object) example:Those URL's can then be stored in the Datastore in whichever model they're relevant to. This is preferred over using the completely different
db.BlobProperty
, which is not really for images at all. It's also more expensive, and less efficient.Of course, I would suggest you store only
url
(as above), because it's so easy to change the size by suffixing the string! In fact, it's something you can really just do in your Jinja template (or equivalent) - where you would probably otherwise specifywidth=
and crop by doing the same in CSS.get_serving_url
is documented here. There's no end-to-end example per-se, but it's pretty straightforward: You pass it a blob key, along with optional resize and crop options, and it gives you back a URL. You can use that URL anywhere you want to reference the image, and it'll be served up by the infrastructure, resized and cropped appropriately.Note that this is only for images uploaded to the blobstore. Anything uploaded to the regular datastore and stored in a
BlobProperty
you'll have to resize, crop, and serve yourself.