4
votes

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.

2
What if two of your users upload files with the same name? - Nick Johnson

2 Answers

18
votes

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();
-5
votes

You can query the BlobInfo objects by filename.