簡體   English   中英

使用gae for Java將數據存儲到Google雲端存儲

[英]Storing data to google cloud storage using gae for java

我正在嘗試將數據從App Engine應用程序存儲到Google Cloud Storage。

如果我在本地執行,代碼可以正常運行,但是我看不到任何存儲的數據。

如果我將應用程序上傳到應用程序引擎,則在以下位置運行代碼時會得到空指針異常: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");

在Google Storage中使用dev服務和文件服務時,它實際上不會寫入Google Storage存儲桶,而是寫入本地文件系統-您是否希望它寫入Google Storage?

對於空指針異常,需要查看堆棧跟蹤以嘗試縮小問題范圍。

因此,問題在於應用程序引擎無權讀取/寫入雲存儲中創建的存儲桶。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM