如何在 java 中的 azure 父目录下添加子目录?



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

jSON输入为:

{   "accountName" : "name", 
"accountkey"  : "keyOfAzureAccount",
"directoryStructure" : "directory1/directory2/directory3/directory4/directory5"
}

我期望在 azure 帐户中一对一创建这些目录。像目录 5 一样,它将在目录 4 中。目录 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;
}
}

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

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

尝试这样的事情(代码是 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;
}

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

最新更新