gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:19:49 24 4
gpt4 key购买 nike

我正在尝试将数据从 App Engine 应用存储到 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");

最佳答案

将开发服务和文件服务与 Google Storage 一起使用时,它实际上不会写入您的 Google Storage 存储桶,而是写入本地文件系统 - 您是否希望它写入 Google Storage?

对于空指针异常,需要查看堆栈跟踪来尝试缩小问题范围。

关于java - 使用 gae for java 将数据存储到谷歌云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9755670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com