Zenanon
Zenanon

Reputation: 185

Google AppEngine Blobstore: Downloading a Blob by Filename in Java

Suppose I've uploaded a bunch of files (images in this case, if it matters) to GAE's BlobStore.
Later, I want to be able to download those files from somewhere else.
I know that I can use BlobStoreService's serve method to grab a blob by BlobKey, but how do I get the blobkey associated with a given filename?
I can't seem to find any built-in functionality for this.

Upvotes: 4

Views: 3232

Answers (2)

systempuntoout
systempuntoout

Reputation: 74134

BlobInfo metadata that contains the filename attribute is stored in read-only __BlobInfo__ entities in the datastore.

Query query = new Query("__BlobInfo__"); 
query.addFilter("filename", FilterOperator.EQUAL, filename); 
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 
PreparedQuery pq = datastore.prepare(query); 
List<Entity> entList = pq.asList(FetchOptions.Builder.withLimit(1)); 
String name = entList.get(0).getKey().getName();

Upvotes: 18

jtahlborn
jtahlborn

Reputation: 53694

You can query the BlobInfo objects by filename.

Upvotes: -5

Related Questions