我正在尝试在我的一个谷歌云存储桶中创建一个新文件。 这是我写的代码:
InputStream stream = new ByteArrayInputStream(report.getBytes(StandardCharsets.UTF_8.name()));
GcsFilename filename = new GcsFilename(BUCKET_NAME, csvFileName);
GcsFileOptions options = GcsFileOptions.getDefaultInstance();
GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, options);
copy(stream, Channels.newOutputStream(outputChannel));
代码运行没有错误,但桶仍然是空的。 我 99% 确定我编写的代码是正确的(它在另一个项目中对我有用),但问题出在 GCS 存储桶设置上。 谢谢。
最佳答案
我认为 Raghuram Kasyap 链接的问题应该有所帮助,但以防万一您想使用 NIO Filesystem Provider for Google Cloud Storage ,它会像这样:
InputStream stream = new ByteArrayInputStream(report.getBytes(StandardCharsets.UTF_8.name()));
try (FileSystem fs = CloudStorageFileSystem.forBucket(BUCKET_NAME)) {
Path dest = fs.getPath(filename);
Files.copy(stream, dest);
}
上面的链接有你需要添加到项目中的依赖;它是 google-cloud-java 的一部分。我尝试了上面的代码,它对我有用。
关于java - 如何在 GCS bucket java 上创建新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49851797/