Google云存储文件位置在Google App Engine中



我使用以下代码将一些内容写入我的文件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时,将进行模拟,而未写入真正的存储桶。

可能有两个问题。

  1. 您可能会忘记在存储桶中添加权限。检查https://developers.google.com/appengine/docs/java/googlestorage/overview#prerequisites如何做。

  2. 您正在尝试更新文件。writechannel.closefinally()文件仅读取之后。您无法更改/更新/附加。

相关内容

  • 没有找到相关文章