简体   繁体   English

在Java和Azure Blob存储SDK中上载带有子目录的目录

[英]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? 我使用此代码将文件上传到azure blob存储,但是当我尝试使用子目录加载目录时,出现错误“遇到FileNotFoundException:C:\\ upload \\ bin” :(访问被拒绝),是加载文件和源目录中的目录?

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. 就像@ ZhaoxingLu-Microsoft所说的那样, source.listFiles()生成的file对象足以通过file.getAbsolutePath()获取绝对文件路径,因此您可以编写以下代码。

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. 但是,根据我的经验, FileNotFoundException encountered: C:\\upload\\bin" :(Access is denied)问题FileNotFoundException encountered: C:\\upload\\bin" :(Access is denied)是由于缺少对C:C:\\upload\\bin下文件的访问权限所致。因此,您需要运行如下图所示,以管理员身份在当前Windows环境中执行代码。

Fig 1. Run your code as administrator if using IntelliJ 图1.如果使用IntelliJ,请以管理员身份运行代码

在此处输入图片说明

Fig 2. Run your code as administrator if using Eclipse 图2.如果使用Eclipse以管理员身份运行代码

在此处输入图片说明

Fig 3. Run your code as administrator via Command Prompt 图3.通过命令提示符以管理员身份运行代码

在此处输入图片说明


Update : On Azure Blob Storage, the file and directory structure is depended on the blob name. 更新 :在Azure Blob存储上,文件和目录结构取决于Blob名称。 So if you want to see the file structure like the figures below, you can use the code String blobName = file.getAbsolutePath().replace(path, ""); 因此,如果您要查看如下图所示的文件结构,可以使用代码String blobName = file.getAbsolutePath().replace(path, ""); to get the blob name. 获取Blob名称。

Fig 4. The file and directory structure built on my local machine 图4.在本地计算机上构建的文件和目录结构 在此处输入图片说明

Fig 5. The same above on Azure Blob Storage via Azure Storage Explorer 图5.通过Azure Storage Explorer在Azure Blob存储上的上述操作 在此处输入图片说明

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);
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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