有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java和azure blob存储sdk中上载包含子目录的目录

我使用此代码将文件上载到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);
    }

共 (1) 个答案

  1. # 1 楼答案

    正如@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");
    

    我在我的环境中测试您的代码,它也可以工作。但是,根据我的经验,您的问题FileNotFoundException encountered: C:\upload\bin" :(Access is denied)是由于缺少访问C:C:\upload\bin下文件的权限而导致的。因此,您需要在当前Windows环境中以管理员身份运行代码,如下图所示

    图1。如果使用IntelliJ,请以管理员身份运行代码

    enter image description here

    图2。如果使用Eclipse,则以管理员身份运行代码

    enter image description here

    图3。通过命令提示符以管理员身份运行代码

    enter image description here


    更新: 在Azure Blob存储上,文件和目录结构取决于Blob名称。因此,如果您想查看如下图所示的文件结构,可以使用代码String blobName = file.getAbsolutePath().replace(path, "");获取blob名称

    图4。在本地计算机上构建的文件和目录结构 enter image description here

    图5。通过Azure存储资源管理器在Azure Blob存储上执行上述操作 enter image description here

    这是我的完整代码

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