简体   繁体   English

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

[英]Google cloud storage file location in google app engine

I have used below code to write something to my file 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();

It seems it's writing and then reading, but when I check in the cloud storage area (using firefox), the file hasn't been edited. 看来是写然后读,但是当我检查云存储区域(使用firefox)时,该文件尚未编辑。 Any idea? 任何想法?

Is this in the production or development environment? 这是在生产还是开发环境中?

When you're using the dev server writes to Google Storage are simulate and are not written to a real bucket. 使用开发服务器时,对Google Storage的写入是模拟的,不会写入真实的存储桶。

There could be two problems. 可能有两个问题。

  1. You could forget to add permissions to your bucket. 您可能会忘记为存储桶添加权限。 Check https://developers.google.com/appengine/docs/java/googlestorage/overview#Prerequisites how to do it. 检查https://developers.google.com/appengine/docs/java/googlestorage/overview#前提条件如何执行。

  2. You are trying to update the file. 您正在尝试更新文件。 After writeChannel.closeFinally() the file becomes read-only. 在writeChannel.closeFinally()之后,文件变为只读。 You can not change/update/append to it. 您不能对其进行更改/更新/添加。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Google App Engine(Java)将文件上传到Google云端存储 - Upload file to Google cloud storage from Google App Engine (Java) 适用于Java和Google云存储的Google App Engine - Google App Engine for Java and Google Cloud Storage 从App Engine Java将urlconnection文件保存到Google云存储 - saving a urlconnection file to google cloud storage from app engine java Google Cloud Storage通过App Engine定价 - Google Cloud Storage thru App Engine pricing Google App Engine:从Google Cloud Storage中读取 - Google App Engine : Read from Google Cloud Storage 通过Google App Engine(JAVA)将文件(图像或视频)从JSP上传到Google云存储 - Upload File (image or video) from JSP to google cloud storage via Google app engine(JAVA) 使用 Java 通过 Google App Engine 将文件上传到 Google Cloud Storage:(没有这样的文件或目录) - Uploading Files to Google Cloud Storage via Google App Engine using Java: (No such file or directory) 如何使用Java App Engine正确上传(映像)文件到Google云端存储? - How to properly upload (image) file to Google Cloud Storage using Java App Engine? 使用blobstore api从云存储中检索文件的Google App引擎Java错误 - google app engine java error using blobstore api to retrieve file from cloud storage 如何获取本地开发服务器中Google云存储上载文件的公共链接(Google App Engine + JAVA) - How to get public link for the uploaded file on google cloud storage in local dev server(Google App engine+JAVA)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM