简体   繁体   中英

In Google appengine how to delete a file originaly stored using FileService

I am trying to delete a file stored using FileService. In a few cases the deletion succeeds but in most it doesn't and I haven't found a pattern. I am using eclipse on windows 7 on the local server. how can I delete the file?

Edit: It works when I upload to the appengine.

Here is the storing code:

try 
                            {
                                FileService fileService = FileServiceFactory.getFileService();
                                AppEngineFile file = fileService.createNewBlobFile(content_type, fileName);
                                boolean lock = true;
                                FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
                                byte[] b1 = new byte[BUFFER_SIZE];
                                int readBytes1;
                                while ((readBytes1 = is.read(b1)) != -1) 
                                {
                                    writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
                                }

                                writeChannel.closeFinally();

                                blobKey = fileService.getBlobKey(file);
                                item_image_blob_key = blobKey.getKeyString();
                            } 
                            catch (Exception e) 
                            {
                                System.out.println(e.getLocalizedMessage());
                                e.printStackTrace(response.getWriter());
                            }

and here is the delete attempt:

@Override
public long deleteItem(Long id) 
{
    System.out.println(LOG +"   Trying to delete item with this id: " + id);

    Key parent = KeyFactory.createKey("MffItems", MFF_ITEM_ROOT_KEY);
    Key key = KeyFactory.createKey(parent, "Item", id);

    try
    {
        Entity e = datastore.get(key); 
        String image_key = (String) e.getProperty("image_blob_key");

        BlobKey blobKey = new BlobKey(image_key);
        final AppEngineFile f = fileService.getBlobFile(blobKey);

        if(f.isReadable())
        {
            System.out.println(LOG + "  file is readable");

            if(f.hasFinalizedName())
            {
                System.out.println(LOG + "  file has finalized name:  " + f.getNamePart());

                fileService.delete(f); // Problematic line
                datastore.delete(key);
                return id;
            }
        }
    }
    catch(Exception e)
    {
        System.out.println(LOG + "   " + e.toString() + "  " + e.getMessage());
    }

    // TODO Auto-generated method stub
    return -1;
}

Here is the error I'm getting:

I_MFF_ItemService Trying to delete item with this id: 2 I_MFF_ItemService file is readable I_MFF_ItemService file has finalized name: TXP5bVCmBVugDOxktBGv_w Jun 30, 2013 12:24:03 PM com.google.appengine.api.blobstore.dev.LocalBlobstoreService$1 run WARNING: Could not delete blob: java.io.IOException: Could not delete: C:\\Users\\Gideon\\Desktop\\dev\\workspace2\\ItemManager1.41\\war\\WEB-INF\\appengine-generated\\TXP5bVCmBVugDOxktBGv_w at com.google.appengine.api.blobstore.dev.FileBlobStorage.deleteBlob(FileBlobStorage.java:79) at com.google.appengine.api.blobstore.dev.LocalBlobstoreService$1.run(LocalBlobstoreService.java:153) at java.security.AccessController.doPrivileged(Native Method) at com.google.appengine.api.blobstore.dev.LocalBlobstoreService.deleteBlob(LocalBlobstoreService.java:146) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang .reflect.Method.invoke(Unknown Source) at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.callInternal(ApiProxyLocalImpl.java:521) at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:475) at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:452) at java.util.concurrent.Executors$PrivilegedCallable$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.util.concurrent.Executors$PrivilegedCallable.call(Unknown Source) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

I_MFF_ItemService java.io.IOException: Blobstore failure Blobstore failure Jun 30, 2013 12:24:27 PM com.google.appengine.api.datastore.dev.LocalDatastoreService$PersistDatastore persist INFO: Time to persist datastore: 20 ms

This is a known bug with the development server, caused by the file being locked in the file system. See this logged issue .

Stopping/restarting the development server is a workaround.

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