【问题标题】:Google cloud storage file location in google app engine谷歌应用引擎中的谷歌云存储文件位置
【发布时间】:2012-10-13 09:41:03
【问题描述】:

我使用下面的代码向我的文件谷歌云存储写入了一些东西。

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)时,文件还没有被编辑。有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我在本地运行应用程序,当我把它放到谷歌应用引擎云中时它确实工作了,谢谢....
    【解决方案2】:

    可能有两个问题。

    1. 您可能忘记向存储桶添加权限。检查https://developers.google.com/appengine/docs/java/googlestorage/overview#Prerequisites怎么做。

    2. 您正在尝试更新文件。在 writeChannel.closeFinally() 之后,文件变为只读。您不能更改/更新/附加到它。

    【讨论】:

    • 是的,我给了存储桶的权限。实际问题是我在本地运行:)
    猜你喜欢
    • 2014-10-30
    • 1970-01-01
    • 2015-02-01
    • 2013-05-17
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多