如何使用 JAVA 在 Blob 存储容器中一起下载存储在根和子文件夹中的多个文件?
Posted
技术标签:
【中文标题】如何使用 JAVA 在 Blob 存储容器中一起下载存储在根和子文件夹中的多个文件?【英文标题】:How to download multiple files store in root and subfolder together in Blob Storage Container using JAVA? 【发布时间】:2021-08-28 03:13:23 【问题描述】:目前我想下载我存储在我的 blob 存储容器中的所有文件,下面是我下载文件的代码。
CloudBlobContainer cloudBlobContainer = StorageAccount(azureStorageProperties,clientCredential).createCloudBlobClient()
.getContainerReference(container);
System.out.println("Access " + container + " container successful. Now start to download the file.");
Iterable<ListBlobItem> blobs = cloudBlobContainer.listBlobs();
for (ListBlobItem blob : blobs)
CloudBlockBlob cloudBlob = (CloudBlockBlob) blob;
cloudBlob.downloadToFile(inputPath + "/" + cloudBlob.getName());
但是,当我尝试下载时遇到问题,控制台显示以下错误。
java.lang.ClassCastException: class com.microsoft.azure.storage.blob.CloudBlobDirectory cannot be cast to class com.microsoft.azure.storage.blob.CloudBlockBlob
This is the dump screen of my blob storage container. Please take a look
请帮助我如何下载根目录和子文件夹中的所有文件而不会出现此错误。谢谢。
【问题讨论】:
【参考方案1】:也许这些代码能给你一些启发:
package org.example;
import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.ResultSegment;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
public class App
public static final String storageConnectionString = "";
public static void main( String[] args )
String inputPath = "";
try
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("");
Iterable<ListBlobItem> blobs = container.listBlobs();
for (ListBlobItem blob : container.listBlobs())
System.out.println("URI of blob is: " + blob.getUri());
if (blob.getUri().toString().endsWith("/"))
String[] arr = blob.getUri().toString().split("/");
String directoryname = arr[arr.length-1];
CloudBlobDirectory directory = container.getDirectoryReference(directoryname);
downloadFile(directory,inputPath);
else
CloudBlockBlob cloudBlob = (CloudBlockBlob) blob;
File file = new File(inputPath + "\\" + cloudBlob.getName());
file.mkdirs();
cloudBlob.downloadToFile(file.getAbsolutePath());
catch (URISyntaxException e)
e.printStackTrace();
catch (InvalidKeyException e)
e.printStackTrace();
catch (StorageException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
private static void downloadFile(CloudBlobDirectory directory, String inputPath)
try
for (ListBlobItem blob : directory.listBlobs())
System.out.println("URI of blob is: " + blob.getUri());
if (blob.getUri().toString().endsWith("/"))
String[] arr = blob.getUri().toString().split("/");
String directoryname = arr[arr.length-1];
CloudBlobDirectory directory1 = directory.getDirectoryReference(directoryname);
downloadFile(directory1,inputPath);
else
CloudBlockBlob cloudBlob = (CloudBlockBlob) blob;
File file = new File(inputPath + "\\" + cloudBlob.getName());
file.mkdirs();
cloudBlob.downloadToFile(file.getAbsolutePath());
catch (StorageException e)
e.printStackTrace();
catch (URISyntaxException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
finally
【讨论】:
哇!!!这正是我正在寻找的!谢谢弗兰克!!真的为我节省了很多时间来弄清楚。【参考方案2】:如果您还没有遇到过这个问题,请参考How to upload and download blobs from Azure Blob Storage with Java
使用Azure Storage samples using v12 Java client libraries 下载文件。
azure blob storage 的存储其实是扁平的,其实根本没有所谓的文件夹。 您必须结合路径和文件名作为文件名传递方法。如果你列出当前容器中的文件,你会发现它们的形式类似于这样:
folder1/folder2/filename.suffix
// Download storage file to local file.
String downloadPath = filePath + "testfiles/" + "downloadSample.txt";
File downloadFile = new File(downloadPath);
try
if (!Files.exists(downloadFile.toPath()) && !downloadFile.createNewFile())
throw new RuntimeException("Failed to create new upload file.");
catch (IOException e)
throw new RuntimeException("Failed to create new upload file.");
【讨论】:
以上是关于如何使用 JAVA 在 Blob 存储容器中一起下载存储在根和子文件夹中的多个文件?的主要内容,如果未能解决你的问题,请参考以下文章
如何在没有文件路径的情况下将文件上传到 Azure Blob 存储容器的根目录