简体   繁体   中英

Upload directory with sub-directories in java and azure blob storage sdk

I use this code for upload files to azure blob storage, but when i try load directory with sub-directories i get error "FileNotFoundException encountered: C:\\upload\\bin" :(Access is denied), is any solution to load files and directories in source directory?

try {
        CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient serviceClient = account.createCloudBlobClient();

        // Container name must be lower case.
        CloudBlobContainer container = serviceClient.getContainerReference(containerName);
        container.createIfNotExists();
        File source = new File(path);
        if (source.list().length > 0) {
            for (File file : source.listFiles()) {
                CloudBlockBlob blob = container.getBlockBlobReference(file.getName());
                if (blob.exists() == false) {
                    File sourceFile = new File(source + "\\" + file.getName());
                    blob.upload(new FileInputStream(sourceFile), sourceFile.length());
                    System.out.println("File " + source + "\\" + file.getName() + " Load to blob storage");
                } else System.out.println("File " + source + "\\" + file.getName() + " Already exist in storage");
            }
        } else System.out.println("In folder " + path + " are no files ");
    } catch (FileNotFoundException fileNotFoundException) {
        System.out.print("FileNotFoundException encountered: ");
        System.out.println(fileNotFoundException.getMessage());
        System.exit(-1);

    } catch (StorageException storageException) {
        System.out.print("StorageException encountered: ");
        System.out.println(storageException.getMessage());
        System.exit(-1);
    } catch (Exception e) {
        System.out.print("Exception encountered: ");
        System.out.println(e.getMessage());
        System.exit(-1);
    }

As @ZhaoxingLu-Microsoft said, the file object generated by source.listFiles() is enough for gettting the absolute file path via file.getAbsolutePath() , so you can write your code as below.

if (blob.exists() == false) {
    blob.uploadFromFile(file.getAbsolutePath());
} else System.out.println("File " + file.getAbsolutePath() + " Already exist in storage");

I test your code in my environment, it also works. However, per my experience, your issue FileNotFoundException encountered: C:\\upload\\bin" :(Access is denied) was caused by lacking the permission of accessing files under C: or C:\\upload\\bin . So you need to run your code as administrator on your current Windows environment, as the figures below.

Fig 1. Run your code as administrator if using IntelliJ

在此处输入图片说明

Fig 2. Run your code as administrator if using Eclipse

在此处输入图片说明

Fig 3. Run your code as administrator via Command Prompt

在此处输入图片说明


Update : On Azure Blob Storage, the file and directory structure is depended on the blob name. So if you want to see the file structure like the figures below, you can use the code String blobName = file.getAbsolutePath().replace(path, ""); to get the blob name.

Fig 4. The file and directory structure built on my local machine 在此处输入图片说明

Fig 5. The same above on Azure Blob Storage via Azure Storage Explorer 在此处输入图片说明

Here is my complete code.

private static final String path = "D:\\upload\\";
private static final String storageConnectionString = "<your storage connection string>";
private static final String containerName = "<your container for uploading>";

private static CloudBlobClient serviceClient;

public static void upload(File file) throws InvalidKeyException, URISyntaxException, StorageException, IOException {
    // Container name must be lower case.
    CloudBlobContainer container = serviceClient.getContainerReference(containerName);
    container.createIfNotExists();
    String blobName = file.getAbsolutePath().replace(path, "");
    CloudBlockBlob blob = container.getBlockBlobReference(blobName);
    if (blob.exists() == false) {
        blob.uploadFromFile(file.getAbsolutePath());
    } else {
        System.out.println("File " + file.getAbsolutePath() + " Already exist in storage");
    }
}

public static void main(String[] args)
        throws URISyntaxException, StorageException, InvalidKeyException, IOException {
    CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
    serviceClient = account.createCloudBlobClient();
    File source = new File(path);
    for (File fileOrDir : source.listFiles()) {
        boolean isFile = fileOrDir.isFile();
        if(isFile) {
            upload(fileOrDir);
        } else {
            for(File file: fileOrDir.listFiles()) {
                upload(file);
            }
        }

    }
}

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