I am trying to write file to Google Cloud Storage (GCS) using the following code.
AppIdentityService appIdentityService = AppIdentityServiceFactory.getAppIdentityService();
Stirng defaultBucketName = appIdentityService.getDefaultGcsBucketName();
String fileName = "MyGcsFile-1.json";
String mimeType = "application/json";
GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename file = new GcsFilename(defaultBucketName, fileName);
GcsFileOptions.Builder builder = new GcsFileOptions.Builder();
GcsFileOptions options = builder.mimeType(mimeType).build();
GcsOutputChannel channel = gcsService.createOrReplace(file, options);
PrintWriter writer = new PrintWriter(Channels.newWriter(channel, "UTF8"));
writer.print(fileContent);
writer.close();
channel.close();
But when I try to fetch the file from GCS using code:
AppIdentityService appIdentityService = AppIdentityServiceFactory.getAppIdentityService();
Stirng defaultBucketName = appIdentityService.getDefaultGcsBucketName();
String fileName = "MyGcsFile-1.json";
GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename gcsFile = new GcsFilename(defaultBucketName, fileName);
GcsInputChannel readChannel = gcsService.openReadChannel(gcsFile, new Long(0));
InputStream is = Channels.newInputStream(readChannel);
JsonFactory j = new JsonFactory();
org.codehaus.jackson.JsonParser jp = j.createJsonParser(is);
getting the FileNotFoundException
java.io.FileNotFoundException
at com.google.appengine.api.files.FileServiceImpl.translateException(FileServiceImpl.java:615)
at com.google.appengine.api.files.FileServiceImpl.makeSyncCall(FileServiceImpl.java:594)
at com.google.appengine.api.files.FileServiceImpl.read(FileServiceImpl.java:553)
at com.google.appengine.api.files.FileServiceImpl.read(FileServiceImpl.java:431)
at com.google.appengine.api.files.FileReadChannelImpl.read(FileReadChannelImpl.java:84)
at com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService.readObjectAsync(LocalRawGcsService.java:271)
at com.google.appengine.tools.cloudstorage.SimpleGcsInputChannelImpl$1.call(SimpleGcsInputChannelImpl.java:87)
at com.google.appengine.tools.cloudstorage.SimpleGcsInputChannelImpl$1.call(SimpleGcsInputChannelImpl.java:81)
at com.google.appengine.tools.cloudstorage.RetryHelper.doRetry(RetryHelper.java:108)
at com.google.appengine.tools.cloudstorage.RetryHelper.runWithRetries(RetryHelper.java:166)
at com.google.appengine.tools.cloudstorage.RetryHelper.runWithRetries(RetryHelper.java:156)
at com.google.appengine.tools.cloudstorage.SimpleGcsInputChannelImpl.read(SimpleGcsInputChannelImpl.java:81)
at sun.nio.ch.ChannelInputStream.read(Unknown Source)
at sun.nio.ch.ChannelInputStream.read(Unknown Source)
at sun.nio.ch.ChannelInputStream.read(Unknown Source)
at org.codehaus.jackson.impl.ByteSourceBootstrapper.ensureLoaded(ByteSourceBootstrapper.java:507)
at org.codehaus.jackson.impl.ByteSourceBootstrapper.detectEncoding(ByteSourceBootstrapper.java:129)
at org.codehaus.jackson.impl.ByteSourceBootstrapper.constructParser(ByteSourceBootstrapper.java:224)
at org.codehaus.jackson.JsonFactory._createJsonParser(JsonFactory.java:785)
at org.codehaus.jackson.JsonFactory.createJsonParser(JsonFactory.java:561)
Where is the problem in the above code ? In writing file to GCS ? or Reading file from GCS ?
Here My App Engine is created before 1.9.0 release. So that there is no default bucket created for my account. Here I have to create a default bucket manually. Following are the details from Google documentation :
Using the default GCS bucket
The default bucket requires no further activation, configuration or permissions, nor are you required to sign up for GCS. The bucket is simply there and ready to use, with a free quota. You do not need to make your app billable if you use this option.
The default bucket name is typically <app_id>.appspot.com, where you replace with your app ID. You can find the bucket name in the App Engine Admin console Application Settings page, under the label Google Cloud Storage Bucket. Alternatively, you can use the App Identity getDefaultGcsBucketName method to find the name programmatically.
It looks like you might be missing:
channel.write(ByteBuffer src)
When you are storing the file.
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.