appengine fileservice using remote api

I am trying to programmatically upload some files to the blobstore and also update something in the datastore. It could be done using a specialized servlet and CURL to do it, but it would be more elegant to use the remote api. Is it possible to use the file service thru appengine's remote apis? If yes, why am I getting this exception when executing bos.write(b)? Thanks code:

String localFileName = "c:\\actcut fail.jpg";
                AppEngineFile aeF = fileService.createNewBlobFile("", "actcutfail.jpg");
                FileWriteChannel writeChannel = (FileWriteChannel) fileService.openWriteChannel(aeF, true); 
                BufferedOutputStream bos = new BufferedOutputStream(Channels.newOutputStream(writeChannel));
                FileInputStream fis = new FileInputStream(localFileName);
                try {
                    while (true) {
                        int b = fis.read();
                        bos.write(b);
                    }
                } catch (EOFException eofex) {
                    // end of file reached
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    fis.close();
                    bos.flush();
                    bos.close();
                    writeChannel.closeFinally();
                    System.out.println("uploaded blob file with key=" + fileService.getBlobKey(aeF).getKeyString());
                }

exception:

java.io.IOException
    at com.google.appengine.api.files.FileServiceImpl.translateException(FileServiceImpl.java:601)
    at com.google.appengine.api.files.FileServiceImpl.makeSyncCall(FileServiceImpl.java:574)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:510)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:255)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:52)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:44)
    at java.nio.channels.Channels.write(Channels.java:63)
    at java.nio.channels.Channels.access$000(Channels.java:47)
    at java.nio.channels.Channels$1.write(Channels.java:134)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:78)
    at cri.uploaddevalle.UploadDevalle.main(UploadDevalle.java:61)
Caused by: com.google.apphosting.api.ApiProxy$ApplicationException: ApplicationError: 10: File not opened: /blobstore/writable:GlPgcwTQdPtgYVY_Jpk9hg
    at com.google.appengine.api.files.dev.LocalFileService.throwError(LocalFileService.java:206)
    at com.google.appengine.api.files.dev.LocalFileService.append(LocalFileService.java:352)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.callInternal(ApiProxyLocalImpl.java:498)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:452)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:430)
    at java.util.concurrent.Executors$PrivilegedCallable$1.run(Executors.java:463)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.util.concurrent.Executors$PrivilegedCallable.call(Executors.java:460)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
java.io.IOException
    at com.google.appengine.api.files.FileServiceImpl.translateException(FileServiceImpl.java:601)
    at com.google.appengine.api.files.FileServiceImpl.makeSyncCall(FileServiceImpl.java:574)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:510)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:255)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:52)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:44)
    at java.nio.channels.Channels.write(Channels.java:63)
    at java.nio.channels.Channels.access$000(Channels.java:47)
    at java.nio.channels.Channels$1.write(Channels.java:134)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
    at cri.uploaddevalle.UploadDevalle.main(UploadDevalle.java:69)
Caused by: com.google.apphosting.api.ApiProxy$ApplicationException: ApplicationError: 10: File not opened: /blobstore/writable:GlPgcwTQdPtgYVY_Jpk9hg
    at com.google.appengine.api.files.dev.LocalFileService.throwError(LocalFileService.java:206)
    at com.google.appengine.api.files.dev.LocalFileService.append(LocalFileService.java:352)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.callInternal(ApiProxyLocalImpl.java:498)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:452)
    at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:430)
    at java.util.concurrent.Executors$PrivilegedCallable$1.run(Executors.java:463)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.util.concurrent.Executors$PrivilegedCallable.call(Executors.java:460)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

Upvotes: 1

Views: 457

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101149

The File API isn't currently fully supported over remote_api. You should be able to get a file upload URL with it, but you'll need to post to a custom servlet if you want to actually upload files.

Upvotes: 4

Related Questions