I am a newbie to Python and Google App Engine.
I need to store an image in GAE Datastore (not blobstore - there are lot of examples to store in blobstore and I have got it working in blobstore).
The docs has it as follows:
movie = Movie()
...
movie.picture = db.Blob(urlfetch.Fetch(picture_url).content)
movie.put()
My question is how do I obtain picture_url? I cannot use create_upload_url method since this is for Blobstore.
Thanks in advance
This question is phrased in a way no one can answer it because you need to tell people how you intend to get the the image data to begin with.
The code you're using is to download the an image from some URL and save it to you blobstore. You should be able to use any publicly accessible image url from flickr or facebook or any other website as the url. You need to decide what url you want to use.
If you want the user to upload the data, then you would need the user to issue a POST request with your image in the multipart/form-data. You would acceess the data in your request handler's request.POST object. You would not fetch it from a url in that case.
If you're using the Blobstore, GAE automatically parses the POST request data, saves it in the Blobstore, and provides a url for you. If you're not using the Blobstore, you'll only have the image data in your request object in memory.
db.Blob(urlfetch.Fetch(picture_url).content)
is for fetching images from another web sites.
if you want to store user-uploaded something, you'd better use
data = self.request.get('data') # where 'data' is the data field from your HTML form
if data :
db.Blob(data)
Related
I am making an application in which the users can upload some pictures so that others can see them. Since some of these can be a bit large, I need to generate smaller images to give a preview of the content.
I already have the uploaded images in GCS, in urls with the form: "https://storage.googleapis.com/...", but from what I can see in the Images API docs, it uses the blobstore, which I am not using (it's been superseded). How can I serve the thumbnails from the gcs link to avoid making the users load the full image? I would really appreciate any code example.
UPDATE:
I tried to copy the example with an image from my app using images.Image with filename as suggested, but it gives me a TransformationError, and a NotImageError if I don't try any transformations:
def get(self):
teststr ='/gs/staging.trn-test2.appspot.com/TestContainer/Barcos-2017-02-12-145657.jpg'
img = images.Image(filename=teststr)
img.resize(width=80, height=100)
thumbnail = img.execute_transforms(output_encoding=images.JPEG)
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(thumbnail)
What am I missing?
In general you can use the Blobstore API, but with GCS as underlying storage instead of the Blobstore, see Using the Blobstore API with Google Cloud Storage. IMHO just the storage is superseded, not the API itself:
Note: You should consider using Google Cloud Storage rather than Blobstore for storing blob data.
For the Image class in particular (from your link) you can use the filename optional constructor argument instead of the blob_key one (which triggers the above-mentioned blobstore API + GCS usage under the hood):
filename: String of the the file name of a Google Storage file that
contains the image data. Must be in the format
`/gs/bucket_name/object_name`.
From its __init__() function:
if filename:
self._blob_key = blobstore.create_gs_key(filename)
else:
self._blob_key = _extract_blob_key(blob_key)
I have been reading all over stackoverflow concerning datastore vs blobstore for storing and retrieving image files. Everything is pointing towards blobstore except one: privacy and security.
In the datastore, the photos of my users are private: I have full control on who gets a blob. In the blobstore, however, anyone who knows the url can conceivable access my users photos? Is that true?
Here is a quote that is supposed to give me peace of mind, but it's still not clear. So anyone with the blob key can still access the photos? (from Store Photos in Blobstore or as Blobs in Datastore - Which is better/more efficient /cheaper?)
the way you serve a value out of the Blobstore is to accept a request
to the app, then respond with the X-AppEngine-BlobKey header with the
key. App Engine intercepts the outgoing response and replaces the body
with the Blobstore value streamed directly from the service. Because
app logic sets the header in the first place, the app can implement
any access control it wants. There is no default URL that serves
values directly out of the Blobstore without app intervention.
All of this is to ask: Which is more private and more secure for trafficking images, and why: datastore or blobstore? Or, hey, google-cloud-storage (which I know nothing about presently)
If you use google.appengine.api.images.get_serving_url then yes, the url returned is public. However the url returned is not guessable from a blob's key, nor does the url even exist before calling get_serving_url. (Or after calling delete_serving_url).
If you need access control on top of the data in the blobstore you can write your own handlers and add the access control there.
BlobProperty is just as private and secure as BlobStore, all depends on your application which serves the requests. your application can implement any permission checking before sending the contents to the user, so I don't see any difference as long as you serve all the images yourself and don't intentionally create publicly available URLs.
Actually, I would not even thinlk about storing photos in the BlobProperty, because this way the data ends up in the database instead of the BlobStore and it costs significantly more to store data in the database. BlobStore, on the other hand, is cheap and convenient.
I have a datastore storing images as a blob property. I want to use get_serving_url to serve images on the fly.
def urlserve(self):
return images.get_serving_url(str(self.key.urlsafe()),350)
It gives me a URL like
http://localhost:8097/_ah/img/ahBkZXZ-cmFqaW5pbmF0aW9uciYLEglpbWFnZWxpc3QiDWRlZmF1bHRfaW1hZ2UMCxIDSW1nGKkHDA=s350
But I can't able to serve using that page.
So I could only use blobstore image to do this, if so do i need to create a blobstore for my image and store the blobreferenceproperty in my datastore? Or is there better way?
If you are using db then you should store the blob_info.key() in the blobstore.BlobReferenceProperty, otherwise if you're using the ndb then you should store the key in the ndb.BlobKeyProperty.
For the image serving URL you don't have to calculate it all the time, but you could simply store the value of it to your Model at the same time that you are storing the BlobKey.
I was looking over code google provided about how to save data on Blobstore. I need to save a large jason file. All the codes provided by google shows how to do it using a form.
I have a servlet, An android application will send the jason and the servlet will have to save that.
The DataStoreService class has a "put" method that does that but looks like blobstore does not have a "put" method.
Can someone please give me some example code that does this? Thanks so much in advance
You can save data to Blobstore programmatically via the new FileService API.
How does one get image data (image url, image info) from datastore through an api?
Is there a tutorial or a project that lets you upload images like FotoRatan but also has an api which can be used to get the image and image info from another website?
Here is an example how to store and serve files (maybe images) using Blobstore: http://code.google.com/appengine/docs/java/blobstore/overview.html#Uploading_a_Blob
You should also look at Picasa Web Albums Data API