Ela*_*nda 1 java google-cloud-storage
我有这个代码可以将文件从谷歌存储下载到本地存储:
@Override
public void downloadFile(URI originFileLocation, URI destinationFileLocation) throws IOException {
StorageFileId gcsFileId = fileIdCreator.createFromUri(originFileLocation);
Get getRequest = gsClient.objects().get(gcsFileId.getBucket(), gcsFileId.getObjectId());
File localFile = new File(destinationFileLocation);
if (!localFile.exists())
{
localFile.createNewFile();
}
try (FileOutputStream fileOutputStream = new FileOutputStream(localFile))
{
getRequest.getMediaHttpDownloader().setDirectDownloadEnabled(false);
getRequest.executeMediaAndDownloadTo(fileOutputStream);
}
}
Run Code Online (Sandbox Code Playgroud)
是否有将 API 下载directory and all its subDirectories到本地存储的 API ?
不可以。Google Cloud Storage 没有目录的内置概念。相反,只有包含“/”字符的对象名称。为方便起见,用户界面和命令行实用程序假装存在此类概念,但 API 没有真正的概念。相反,您需要使用对象列表方法来遍历您感兴趣的对象并单独调用以下载每个对象。