按照说明操作后,AppEngine上的Google云端存储中的文件丢失

时间:2013-03-19 04:10:40

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

我使用此处提供的说明将文件写入Google云端存储:

https://developers.google.com/appengine/docs/java/googlestorage/overview

代码运行并成功执行,但是,在登录到我的存储帐户后,我没有在我的存储桶中看到新写入的文件。

关于为什么的任何想法?

所以这是导出代码:

This is the code I am using:

        try {   
            // 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://storage.googleapis.com/my_bucket/my_object
             */
            GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
              .setBucket(bucket)
              .setKey(key)
              .setAcl("public-read");

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

            // Open a channel for writing
            boolean lockForWrite = false;
            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"));

            Iterator<String> it = spRes.iterator();

            while (it.hasNext()) {
                out.println(it.next());
            }

            // 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
            lockForWrite = true;
            writeChannel = fileService.openWriteChannel(writableFile,
                    lockForWrite);

            // Now finalize
            writeChannel.closeFinally();
        } catch (IOException e) {
            result = "Failed to export";
            e.printStackTrace();
        }

3 个答案:

答案 0 :(得分:3)

我认为您尚未添加应用程序的电子邮件,您可以在您的appengine应用程序的应用程序设置下找到。

然后,您需要在Google API Console下的团队中为Google云端存储添加此电子邮件,并使用是所有者权限。确保您还使用在 UploadOptions 中在线浏览器工具中创建的相同存储桶名称。

答案 1 :(得分:1)

看起来我在Google云端控制台中设置了2个不同的项目。我正在更新错误的项目。

立即行动。

感谢您的帮助。

答案 2 :(得分:0)

正如Ankur所说,您必须在appengine上部署代码才能写入云端存储。否则文件将只存储在本地硬盘上。