简体   繁体   中英

How to download a directory and zipped folder from azure storage in java

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM