使用gae for java将数据存储到谷歌云存储

时间:2012-03-18 03:04:24

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

我正在尝试将应用引擎应用中的数据存储到Google云存储中。

如果我这样做本地代码运行正常,但我没有看到任何数据被存储。

如果我将应用程序上传到应用程序引擎,那么在运行代码时我得到一个空指针异常: AppEngineFile writableFile = fileService.createNewGSFile(optionsBuilder.build());

我使用的完整代码是

        // 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://commondatastorage.googleapis.com/my_bucket/my_object
         */
        GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
          .setBucket("blood")
          .setKey("1234567")
          .setAcl("public-read")
          .setMimeType("text/html");//.setUserMetadata("date-created", "092011", "owner", "Jon");


        // Create your object
        AppEngineFile writableFile = fileService.createNewGSFile(optionsBuilder.build());

        // Open a channel for writing
        boolean lockForWrite = true; // Do you want to exclusively lock this object?
        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"));
        out.println("The woods are lovely and deep.");
        out.println("But I have promises too keep.");

        // Close the object without finalizing.
        out.close();

        // Finalize the object
        writeChannel.closeFinally();
        System.out.println("Completed writing to google storage");

2 个答案:

答案 0 :(得分:1)

在Google服务中使用开发服务和文件服务时,它实际上并没有写入您的Google存储空间存储桶,而是写入本地文件系统 - 您是否希望将其写入Google存储空间?

对于空指针,异常需要查看堆栈跟踪以尝试缩小出错的地方。

答案 1 :(得分:0)

所以问题是app引擎无权读取/写入云存储中创建的存储桶。