我正在通过java服务在azure存储帐户中创建目录。
jSON输入是:
{ "accountName" : "name",
"accountkey" : "keyOfAzureAccount",
"directoryStructure" : "directory1/directory2/directory3/directory4/directory5"
}
我期待的是在azure帐户中创建一对一的目录。就像directory5将在directory4中。 directory4将在directory3中。 directory3将在directory2中,而directory2将在directory1中。
我的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代码,以便可以根据需要创建目录?
答案 0 :(得分: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;
}
基本上,您从作为根目录的共享开始,当您开始创建子目录时,您将继续更新根目录的引用。