【问题标题】:How to add sub directory under parent directory in azure in java ?java - 如何在azure中的父目录下添加子目录?
【发布时间】:2017-05-26 05:15:18
【问题描述】:

我正在通过 java 服务在 azure 存储帐户中创建目录。

jSON 输入是:

{   "accountName" : "name", 
    "accountkey"  : "keyOfAzureAccount",

    "directoryStructure" : "directory1/directory2/directory3/directory4/directory5"
}

我期望的是在 azure 帐户中一对一地创建这些目录。像 directory5 将在 directory4 内。目录 4 将在目录 3 中。目录 3 将在目录 2 中,目录 2 将在目录 1 中。

我的java代码是这样的:

@Override
    public JSONObject createDynamicDirectory(JSONObject jsonInput) throws InvalidKeyException, URISyntaxException {
        CloudFileClient fileClient = null;
        String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName="+jsonInput.get("accountName")+";"+"AccountKey="+jsonInput.get("accountkey");
        System.out.println(storageConnectionString);
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        JSONObject jsonOutput = new JSONObject();
        try {
            fileClient = storageAccount.createCloudFileClient();
            String directoryName = jsonInput.get("directoryStructure").toString();

            String[] directoryNameArray = directoryName.split("\\s*/\\s*");
            System.out.println(directoryNameArray.length);

            CloudFileShare share = fileClient
                    .getShareReference(directoryNameArray[0].toLowerCase().replaceAll("[-+.^:,!@#$%&*()_~`]", ""));
            if (share.createIfNotExists()) {
                System.out.println("New share created named as "
                        + directoryName.toLowerCase().replaceAll("[-+.^:,!@#$%&*()_~`]", ""));
            }
            for(int i=0;i<directoryNameArray.length;i++)
            {
                CloudFileDirectory rootDir = share.getRootDirectoryReference();
                CloudFileDirectory parentDirectory = rootDir.getDirectoryReference(directoryNameArray[i]);
                if (parentDirectory.createIfNotExists()) {
                    System.out.println("new directory created named as " + directoryName);
                    jsonOutput.put("status", "successful");
                }
            }

        } catch (Exception e) {
            System.out.println("Exception is " + e);
            jsonOutput.put("status", "unsuccessful");
            jsonOutput.put("exception", e.toString());
        }
        return jsonOutput;
    }
}

此代码根据需要从 directory1 创建共享。但问题是,在同一个共享下,它会创建所有目录1、2、3、4、5。不像需要的一对一目录。

如何实现我的 java 代码,以便可以根据需要创建目录?

【问题讨论】:

    标签: java json azure logic file-handling


    【解决方案1】:

    试试这样的(代码在 C# 中)

            var parentDirectory = share.GetRootDirectoryReference();
            for (var i=1; i< directoryNameArray.Length; i++)
            {
                var directoryToCreate = directoryNameArray[i];
                var directory = parentDirectory.GetDirectoryReference(directoryToCreate);
                directory.CreateIfNotExists();
                Console.WriteLine("Created directory - " + directoryToCreate);
                parentDirectory = directory;
            }
    

    基本上,您从共享作为根目录开始,当您开始创建子目录时,您会不断更新根目录的引用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    相关资源
    最近更新 更多