0
我正在通過java服務在azure存儲帳戶中創建目錄。如何在java中的azure父目錄下添加子目錄?
JSON輸入:
{ "accountName" : "name",
"accountkey" : "keyOfAzureAccount",
"directoryStructure" : "directory1/directory2/directory3/directory4/directory5"
}
什麼我期待是創建這些目錄單下一個在蔚藍的帳戶。像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("[-+.^:,[email protected]#$%&*()_~`]", ""));
if (share.createIfNotExists()) {
System.out.println("New share created named as "
+ directoryName.toLowerCase().replaceAll("[-+.^:,[email protected]#$%&*()_~`]", ""));
}
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代碼,以便根據需要創建目錄?
謝謝。有效。 –
嗨,請看以下問題:https://stackoverflow.com/questions/44236289/how-to-copy-file-from-one-directory-to-other-in-azure-through-java-service –