我使用此处给出的说明向 Google Cloud Storage 写入了一个文件:
https://developers.google.com/appengine/docs/java/googlestorage/overview
代码运行并成功执行,但是,登录到我的存储帐户后,我没有在我的存储桶中看到新写入的文件。
关于为什么的任何想法?
所以这是导出代码:
This is the code I am using:
try {
// Get the file service
FileService fileService = FileServiceFactory.getFileService();
/**
* Set up properties of your new object
* After finalizing objects, they are accessible
* through Cloud Storage with the URL:
* http://storage.googleapis.com/my_bucket/my_object
*/
GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
.setBucket(bucket)
.setKey(key)
.setAcl("public-read");
// Create your object
AppEngineFile writableFile = fileService
.createNewGSFile(optionsBuilder.build());
// Open a channel for writing
boolean lockForWrite = false;
FileWriteChannel writeChannel = fileService.openWriteChannel(
writableFile, lockForWrite);
// For this example, we write to the object using the
// PrintWriter
PrintWriter out = new PrintWriter(Channels.newWriter(
writeChannel, "UTF8"));
Iterator<String> it = spRes.iterator();
while (it.hasNext()) {
out.println(it.next());
}
// Close without finalizing and save the file path for writing
// later
out.close();
String path = writableFile.getFullPath();
// Write more to the file in a separate request:
writableFile = new AppEngineFile(path);
// Lock the file because we intend to finalize it and
// no one else should be able to edit it
lockForWrite = true;
writeChannel = fileService.openWriteChannel(writableFile,
lockForWrite);
// Now finalize
writeChannel.closeFinally();
} catch (IOException e) {
result = "Failed to export";
e.printStackTrace();
}