问题
I am creating application to copy file from one directory to another.
jSON input is :
{ "accountName" : "name",
"accountKey" : "key",
"source" : "directory1/directory2/directory3/directory4",
"destination" : "directory1/directory2",
"fileToCopy" : "1"
}
Directory 4 is under directory 3, 3 is under 2 and 2 is under 1.
Want to copy file named as "1" from directory 4 to directory 2.
My java code is :
@Override
public JSONObject copyFile(JSONObject jsonInput) throws IOException, 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 source = jsonInput.get("source").toString();
String destination = jsonInput.get("destination").toString();
String fileToCopy = jsonInput.get("fileToCopy").toString();
String[] sourceNameArray = source.split("\\s*/\\s*");
System.out.println(sourceNameArray.length);
String[] destinationNameArray = destination.split("\\s*/\\s*");
System.out.println(destinationNameArray.length);
CloudFileShare share = fileClient
.getShareReference(sourceNameArray[0].toLowerCase().replaceAll("[-+.^:,!@#$%&*()_~`]", ""));
CloudFileDirectory rootDir = share.getRootDirectoryReference();
for (int i=0; i< sourceNameArray.length; i++)
{
String directoryToCreate = sourceNameArray[i];
CloudFileDirectory directory = rootDir.getDirectoryReference(directoryToCreate);
if(i==sourceNameArray.length-1)
{
CloudFile fileToCopyFromSorce = directory.getFileReference(fileToCopy);
for (int j=0; j< destinationNameArray.length; j++)
{
String directoryToCreateForDestination = destinationNameArray[j];
CloudFileDirectory directoryForDestination = rootDir.getDirectoryReference(directoryToCreateForDestination);
if(j==destinationNameArray.length-1){
CloudFile fileDestination = directoryForDestination.getFileReference(fileToCopy);
// is next line required?
//fileToCopyFromSorce.create(1);
fileDestination.startCopy(fileToCopyFromSorce);
System.out.println("copied to destination");
jsonOutput.put("status", "successful");
}
rootDir = directoryForDestination;
}
}
rootDir = directory;
}
} catch (Exception e) {
System.out.println("Exception is " + e);
jsonOutput.put("status", "unsuccessful");
jsonOutput.put("exception", e.toString());
}
return jsonOutput;
}
I am getting error as,
Exception is com.microsoft.azure.storage.StorageException: The specified parent path does not exist.
But I have specified parent path in my azure storage account.
Need suggestion on code and any reference code if possible.
回答1:
According to the exception, the issue was caused by the parent directories of a file not exist on Azure File Storage, as the figure below from here.
So you need to check and create these parent directories one by one from the root to the kids firstly, such as the code below for destination path, when you need to get the directory reference.
String destination = "directory1/directory2";
CloudFileDirectory rootDir = share.getRootDirectoryReference();
String[] destinationNameArray = destination.split("/");
CloudFileDirectory kidDir = rootDir;
for(String name: destinationNameArray) {
kidDir = kidDir.getDirectoryReference(name);
kidDir.createIfNotExists();
}
Then you can directly copy a file from source to target as below.
String source = "directory1/directory2/directory3/directory4";
String destination = "directory1/directory2";
String fileName = "1";
CloudFileDirectory sourceDir = rootDir.getDirectoryReference(source);
CloudFileDirectory destinationDir = rootDir.getDirectoryReference(destination);
CloudFile sourceFile = sourceDir.getFileReference(fileName);
CloudFile destinationFile = destinationDir.getFileReference(fileName);
destinationFile.startCopy(sourceFile);
来源:https://stackoverflow.com/questions/44236289/how-to-copy-file-from-one-directory-to-other-in-azure-through-java-service