如何通过java服务将azure中的文件从一个目录复制到另一个目录?[英] How to copy file from one directory to other in azure through java service?

问题描述

我正在创建将文件从一个目录复制到另一个目录的应用程序.

JSON输入是:

{   "accountName" : "name", 
    "accountKey"  : "key",
    "source" : "directory1/directory2/directory3/directory4",
    "destination" : "directory1/directory2",
    "fileToCopy"    : "1"
}

目录4在目录3下,3在2下,2在1.

下.

想要将目录4的名为" 1"的文件复制为目录2.

我的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;
    }

我遇到错误,

Exception is com.microsoft.azure.storage.StorageException: The specified parent path does not exist.

,但我在Azure存储帐户中指定了父路径.

如果可能的话,需要有关代码和任何参考代码的建议.

推荐答案

根据例外,该问题是由azure文件存储中不存在的文件的父目录引起的,如下图在这里.

在此处输入图像说明

因此,您需要首先检查并创建这些父目录,从根到孩子,例如以下代码的目标路径,当您需要获取目录参考时.

.

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

然后,您可以将文件直接从源将文件复制到下面的目标.

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://www.itbaoku.cn/post/978515.html

相关标签/搜索