java - 谷歌应用程序引擎中的谷歌云存储文件位置

标签 java google-app-engine google-api google-api-java-client google-cloud-storage

我使用下面的代码将一些内容写入我的文件谷歌云存储。

FileService fileService = FileServiceFactory.getFileService();
    GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
       .setBucket(BUCKETNAME)
       .setKey(FILENAME)
       .setMimeType("text/html")
       .setAcl("public_read")
       .addUserMetadata("myfield1", "my field value");
    AppEngineFile writableFile =
         fileService.createNewGSFile(optionsBuilder.build());
    // Open a channel to write to it
     boolean lock = false;
     FileWriteChannel writeChannel =
         fileService.openWriteChannel(writableFile, lock);
     // Different standard Java ways of writing to the channel
     // are possible. Here we use a PrintWriter:
     PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
     out.println("The woods are lovely dark and deep.");
     out.println("But I have promises to keep.");
     // 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
     lock = true;
     writeChannel = fileService.openWriteChannel(writableFile, lock);
     // This time we write to the channel directly
     writeChannel.write(ByteBuffer.wrap
               ("And miles to go before I sleep.".getBytes()));

     // Now finalize
     writeChannel.closeFinally();
     resp.getWriter().println("Done writing...");

     // At this point, the file is visible in App Engine as:
     // "/gs/BUCKETNAME/FILENAME"
     // and to anybody on the Internet through Cloud Storage as:
     // (http://storage.googleapis.com/BUCKETNAME/FILENAME)
     // We can now read the file through the API:
     String filename = "/gs/" + BUCKETNAME + "/" + FILENAME;
     AppEngineFile readableFile = new AppEngineFile(filename);
     FileReadChannel readChannel =
         fileService.openReadChannel(readableFile, false);
     // Again, different standard Java ways of reading from the channel.
     BufferedReader reader =
             new BufferedReader(Channels.newReader(readChannel, "UTF8"));
     String line = reader.readLine();
     resp.getWriter().println("READ:" + line);

    // line = "The woods are lovely, dark, and deep."
     readChannel.close();

看起来是先写后读,但是当我检查云存储区域(使用firefox)时,该文件尚未被编辑。有什么想法吗?

最佳答案

这是在生产环境还是开发环境中?

当您使用开发服务器时,对 Google Storage 的写入是模拟的,而不是写入真实的存储桶。

关于java - 谷歌应用程序引擎中的谷歌云存储文件位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13030756/

相关文章:

java - 在java中运行时更改json属性的值为1

python - 是否有 Google 的 Vision API 将返回的潜在标签的完整列表?

python - 如何在 Google App Engine 中打印?

google-app-engine - 在 GAE 应用程序中超时刷新 Google oauth token

php - Google OAuth - 私有(private) IP 需要 device_id 和 device_name

java - 仅使用业务 key 保存对象。 [ hibernate ]

java - 我如何让多个组件响应 JSF 中的单个事件?

java - Java 8 中的惯用集合迭代

php - 使用部署在 Google App Engine 上的 PHP 应用程序发送邮件

google-api - 使用常规 Google 帐户作为服务帐户