如何在java中从azure存储下载目录和压缩文件夹

发布于 2025-01-18 05:15:37 字数 1526 浏览 0 评论 0原文

我已经尝试过这样的尝试。我无法下载

CloudStorageAccount account = CloudStorageAccount.parse("connection string");
CloudBlobClient serviceClient = account.createCloudBlobClient();
CloudBlobContainer container = serviceClient.getContainerReference(containername);
for (com.microsoft.azure.storage.blob.ListBlobItem blobItem : container.listBlobs()) {

            // If the item is a blob, a virtual directory.
            if (blobItem instanceof CloudBlobDirectory) {
                CloudBlobDirectory blobDir = (CloudBlobDirectory) blobItem;
                downloadDirectory(blobDir);
               
            }
        }
    
    public static void downloadDirectory(CloudBlobDirectory blobDir)
            throws IOException, StorageException, URISyntaxException {
    
        if (blobDir.getPrefix().equals(destFilePath)) {          
               for (ListBlobItem blobInDir : blobDir.listBlobs()) {
                    if (blobInDir instanceof CloudBlockBlob) {
                        ByteArrayOutputStream outputStream =new ByteArrayOutputStream();
                        ((CloudBlockBlob) blobInDir).download(outputStream);
                        ((CloudBlockBlob) blobInDir).downloadToFile(destFilePath);                      
                        CloudBlockBlob blob = (CloudBlockBlob) blobInDir;
                        
                        blob.downloadToFile(destFilePath);
                    } 
                }
        }
    
     
    
    }

我尝试从Azure存储中下载文件夹,但我无法下载作为文件夹

I already tried like this. I cant able to download

CloudStorageAccount account = CloudStorageAccount.parse("connection string");
CloudBlobClient serviceClient = account.createCloudBlobClient();
CloudBlobContainer container = serviceClient.getContainerReference(containername);
for (com.microsoft.azure.storage.blob.ListBlobItem blobItem : container.listBlobs()) {

            // If the item is a blob, a virtual directory.
            if (blobItem instanceof CloudBlobDirectory) {
                CloudBlobDirectory blobDir = (CloudBlobDirectory) blobItem;
                downloadDirectory(blobDir);
               
            }
        }
    
    public static void downloadDirectory(CloudBlobDirectory blobDir)
            throws IOException, StorageException, URISyntaxException {
    
        if (blobDir.getPrefix().equals(destFilePath)) {          
               for (ListBlobItem blobInDir : blobDir.listBlobs()) {
                    if (blobInDir instanceof CloudBlockBlob) {
                        ByteArrayOutputStream outputStream =new ByteArrayOutputStream();
                        ((CloudBlockBlob) blobInDir).download(outputStream);
                        ((CloudBlockBlob) blobInDir).downloadToFile(destFilePath);                      
                        CloudBlockBlob blob = (CloudBlockBlob) blobInDir;
                        
                        blob.downloadToFile(destFilePath);
                    } 
                }
        }
    
     
    
    }

I tried to download a folder from azure storage, but I cant able to download as a folder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

快乐很简单 2025-01-25 05:15:37

您可以使用此 Java 代码从容器(Azure 存储帐户)下载该文件夹。由于我无法测试它,因此我的 IDE 存在问题。但你可以在你的环境中尝试一下。

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidKeyException;

public class DownloadDirectory {

    public static String destFilePath = "C:\\Folder1\\";
    public static String storageConnectionString="XXXXXX";

    public static void main(String[] args)
            throws InvalidKeyException, URISyntaxException, StorageException, IOException {
        System.setProperty("server.port", "4000");

        CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient serviceClient = account.createCloudBlobClient();
        CloudBlobContainer container = serviceClient.getContainerReference("testblob");

        for (ListBlobItem blobItem : container.listBlobs()) {

            // If the item is a blob, a virtual directory.
            if (blobItem instanceof CloudBlobDirectory) {

                CloudBlobDirectory blobDir = (CloudBlobDirectory) blobItem;
                downloadDirectory(blobDir);
            }
        }
    }

    public static void downloadDirectory(CloudBlobDirectory blobDir)
            throws IOException, StorageException, URISyntaxException {

        Files.createDirectories(Paths.get(destFilePath + blobDir.getPrefix()));

        for (ListBlobItem blobInDir : blobDir.listBlobs()) {

            if (blobInDir instanceof CloudBlockBlob) {
                CloudBlockBlob blob = (CloudBlockBlob) blobInDir;
                blob.downloadToFile(destFilePath + blob.getName());
            } else {
                downloadDirectory((CloudBlobDirectory) blobInDir);
            }
        }

    }
}

参考:从 blob 存储下载虚拟目录

You can use this Java code to download the folder from Container(Azure Storage Account). Since i am not able to test it there is problem in my IDE. but you can try out in your environment.

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidKeyException;

public class DownloadDirectory {

    public static String destFilePath = "C:\\Folder1\\";
    public static String storageConnectionString="XXXXXX";

    public static void main(String[] args)
            throws InvalidKeyException, URISyntaxException, StorageException, IOException {
        System.setProperty("server.port", "4000");

        CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient serviceClient = account.createCloudBlobClient();
        CloudBlobContainer container = serviceClient.getContainerReference("testblob");

        for (ListBlobItem blobItem : container.listBlobs()) {

            // If the item is a blob, a virtual directory.
            if (blobItem instanceof CloudBlobDirectory) {

                CloudBlobDirectory blobDir = (CloudBlobDirectory) blobItem;
                downloadDirectory(blobDir);
            }
        }
    }

    public static void downloadDirectory(CloudBlobDirectory blobDir)
            throws IOException, StorageException, URISyntaxException {

        Files.createDirectories(Paths.get(destFilePath + blobDir.getPrefix()));

        for (ListBlobItem blobInDir : blobDir.listBlobs()) {

            if (blobInDir instanceof CloudBlockBlob) {
                CloudBlockBlob blob = (CloudBlockBlob) blobInDir;
                blob.downloadToFile(destFilePath + blob.getName());
            } else {
                downloadDirectory((CloudBlobDirectory) blobInDir);
            }
        }

    }
}

Reference : download virtual directory from blob storage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文