简体   繁体   中英

Delete files from blobstore using file serving URL

In my app (GWT on GAE) we are storing on our database the serving URL that is stored on blobstore. When user selects one of these files and clicks "delete", we need to delete the file from blobstore.

This is our code, but it is not deleting the file at all:

public void remove(String fileURL)
{
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    String key = getBlobKeyFromURL(fileURL);
    BlobKey blobKey = new BlobKey(key);
    blobstoreService.delete(blobKey);
}

Where fileURL looks like this:

http://lh6.ggpht.com/d5VC0ywISACeJRiC3zkzaZug-tPsaI_LGt93-e_ATGTCwnGLao4yTWjLVppQ

And getBlobKeyFromURL() would return what is after the last "/", in this example:

d5VC0ywISACeJRiC3zkzaZug-tPsaI_LGt93-e_ATGTCwnGLao4yTWjLVppQ

* EDIT: * Seems that what getBlobKeyFromURL() returns is not the blobKey. The blobKey is a different string that, with /download?blob-key= before, returns the fileURL . So the question now would be: how can I get the blobKey from the URL?

Could you please advice?

Thanks

There is no way to derive the original blobkey from the serving URL. If this is something you want to do, then I suggest storing the URL -> BlobKey mapping in the datastore when you generate the URL.

Following code snipped returns blobkey if you use URL parameter such as www.example.com/?name=yourUrlwanttodelete

public class ShowImage extends HttpServlet { private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobs toreService();

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req, res); }

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String par = req.getParameter("name"); if (par != null) { Query query = new Query("__BlobInfo__");

query.addFilter("filename", Query.FilterOperator.EQUAL, req.getParameter("name"));

DatastoreService datastore = DatastoreServiceFactory.getDatas toreService(); PreparedQuery pq = datastore.prepare(query); List<Entity> entList = pq.asList(FetchOptions.Builder.w ithLimit(1)); if (entList.size() > 0) { BlobKey blobKey = new BlobKey(entList.get(0).getKey(). getName());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM