简体   繁体   English

如何通过Java服务在Azure中将文件从一个目录复制到另一个目录?

[英]How to copy file from one directory to other in azure through java service?

I am creating application to copy file from one directory to another. 我正在创建应用程序以将文件从一个目录复制到另一个目录。

jSON input is : jSON输入为:

{   "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. 目录4在目录3下,目录3在目录2下,目录2在目录1下。

Want to copy file named as "1" from directory 4 to directory 2. 要将名为“ 1”的文件从目录4复制到目录2。

My java code is : 我的Java代码是:

@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. 但是我在我的azure存储帐户中指定了父路径。

Need suggestion on code and any reference code if possible. 如果可能,需要有关代码和任何参考代码的建议。

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 . 据例外,这个问题是由文件的父目录造成Azure上的文件存储不存在的,因为从下图这里

在此处输入图片说明

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM