首页 > 解决方案 > 将文件从 Azure 文件共享目录移动到 unix 目录和备份目录(在 Azure 文件共享中)

问题描述

需要将所有文件从 Azure 文件共享上的目录移动到 Unix 目录。移动后,将这些文件备份到备份目录中。

我编写了一种方法,该方法根据文件名将文件从 Azure 文件共享目录移动到 unix 目录。但我需要对其进行更改,以便它移动所有文件并进行备份。源目录地址如下所示:- Z:\Business Backup 目录已创建,即:- Z:\Business\Backup 并且 Business 下没有子目录,只是文件和名称以 Data_Files_yyyymmdd 开头。

第二步,需要将目录中的所有文件移动到unix目录。

编辑:1-我已经对代码进行了一些编辑,因为我在工具中运行它。并将代码调用为:- maincode(AzureStorageConnectionString);

但我收到错误:- [错误] com.microsoft.azure.storage.StorageException:指定的资源名称包含无效字符。我试图修复它但无法修复。我尝试将 backupFileShareName 更改为不同的名称,如下所示,但两者都不起作用。尝试 1) 静态字符串 backupFileShareName = "Business/Backup"; 试试 2) 静态字符串 backupFileShareName = "Backup";

static String connectionString = "DefaultEndpointsProtocol=https;AccountName=elkdemmastershare;AccountKey=ZdqwMyhGDBVJWy85IapP5CnzavK2cGzVUCqyQIKwhdcWbI0bGE/WNkQsW+CPWWRJN1JITFkYaWm0bGqOIEJnUg==;EndpointSuffix=core.windows.net";
static String fileShareName = "Business";
static String localRootDirPath = "/cogn_shared/TgtFiles/test_data/";
static String backupFileShareName = "Business/Backup";

public static void download(CloudFileDirectory root, CloudFileDirectory backup)throws StorageException, URISyntaxException, FileNotFoundException {
    System.out.println("=>\t" + root.getName());
    ResultSegment < ListFileItem > list = root.listFilesAndDirectoriesSegmented();
    for (ListFileItem item: list.getResults()) {
        URI uri = item.getUri();
        //Need to move all the files from a directory on Azure file share to Unix directory.Once it is moved take a backup of these files in a backup directory.
        //I have written a method which move the file from Azure file share directory to unix directory based on file names.But i need to change it so that it moves all the files and take backup.
        //Need to move all the files from the directory to unix directory.
        String path = uri.getPath();
        String localPath = localRootDirPath + path;
        String itemName = new File(path).getName();
        boolean flag = isDir(root, itemName);
        System.out.println(item.getUri() + "\t" + path + "\t" + itemName + "\t" + flag);
        if (flag) {
            // Create local directory
            new File(localPath).mkdirs();
            CloudFileDirectory next = root.getDirectoryReference(itemName);
            // Create cloud directory for backup
            CloudFileDirectory backupNext = backup.getDirectoryReference(itemName);
            backupNext.createIfNotExists();
            // Recursion
            download(next, backupNext);
        } else {
            // Download file to local
            FileOutputStream fos = new FileOutputStream(localPath);
            CloudFile file = root.getFileReference(itemName);
            file.download(fos);
            // Start Copy to cloud directory for backup without upload again
            CloudFile backupFile = backup.getFileReference(itemName);
            backupFile.startCopy(file);
            System.out.println("Downloaded " + path);
        }
    }
}

public static boolean isDir(CloudFileDirectory root, String itemName)throws URISyntaxException, StorageException {
    CloudFileDirectory dir = root.getDirectoryReference(itemName);
    boolean flag = true;
    try {
        dir.listFilesAndDirectoriesSegmented();
    } catch (StorageException e) {
        flag = false;
    }
    return flag;
}

public static void maincode(String connectionString) {

    try {
        CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
        CloudFileClient fileClient = account.createCloudFileClient();
        CloudFileShare share = fileClient.getShareReference(fileShareName);
        CloudFileDirectory rootDir = share.getRootDirectoryReference();
        CloudFileShare backupShare = fileClient.getShareReference(backupFileShareName);
        backupShare.createIfNotExists();
        CloudFileDirectory backupRootDir = backupShare.getRootDirectoryReference();
        download(rootDir, backupRootDir);
    } catch (Exception e) {
        e.printStackTrace();
        //System.out.println(e.getMessage());
    }
}

标签: javaazure

解决方案


听起来您想将 Azure 文件共享中的所有文件下载到本地目录并将它们备份到另一个 Azure 文件共享。

这是我使用适用于 Java 的 Azure Storage SDK v8 的示例代码(我看到您使用了相同的 SDK 版本)以满足您的需求。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.ResultSegment;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.file.CloudFile;
import com.microsoft.azure.storage.file.CloudFileClient;
import com.microsoft.azure.storage.file.CloudFileDirectory;
import com.microsoft.azure.storage.file.CloudFileShare;
import com.microsoft.azure.storage.file.ListFileItem;

public class DownloadFilesFromFileShare {

    private static final String connectionString = "DefaultEndpointsProtocol=https;AccountName=<your account name>;AccountKey=<your account key>;EndpointSuffix=core.windows.net;";

    private static final String fileShareName = "<source file share>";
    private static final String localRootDirPath = "<local directory like D:/backup or /home/user/backup>";

    private static final String backupFileShareName = "<backup file share>";

    public static boolean isDir(CloudFileDirectory root, String itemName) throws URISyntaxException, StorageException {
        CloudFileDirectory dir = root.getDirectoryReference(itemName);
        boolean flag = true;
        try {
            dir.listFilesAndDirectoriesSegmented();
        } catch (StorageException e) {
            flag = false;
        }
        return flag;
    }

    public static void download(CloudFileDirectory root, CloudFileDirectory backup) throws StorageException, URISyntaxException, FileNotFoundException {
        System.out.println("=>\t"+root.getName());
        ResultSegment<ListFileItem> list = root.listFilesAndDirectoriesSegmented();
        for (ListFileItem item : list.getResults()) {
            URI uri = item.getUri();
            String path = uri.getPath();
            String localPath = localRootDirPath + path;
            String itemName = new File(path).getName();
            boolean flag = isDir(root, itemName);
            System.out.println(item.getUri() + "\t" + path +"\t"+itemName + "\t" + flag);
            if(flag) {
                // Create local directory
                new File(localPath).mkdirs();
                CloudFileDirectory next = root.getDirectoryReference(itemName);
                // Create cloud directory for backup
                CloudFileDirectory backupNext = backup.getDirectoryReference(itemName);
                backupNext.createIfNotExists();
                // Recursion
                download(next, backupNext);
            } else {
                // Download file to local
                FileOutputStream fos = new FileOutputStream(localPath);
                CloudFile file = root.getFileReference(itemName);
                file.download(fos);
                // Start Copy to cloud directory for backup without upload again
                CloudFile backupFile = backup.getFileReference(itemName);
                backupFile.startCopy(file);
                System.out.println("Downloaded " + path);
            }
        }
    }

    public static void main(String[] args) throws InvalidKeyException, URISyntaxException, StorageException, FileNotFoundException {
        CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
        CloudFileClient fileClient = account.createCloudFileClient();
        CloudFileShare share = fileClient.getShareReference(fileShareName);
        CloudFileDirectory rootDir = share.getRootDirectoryReference();
        CloudFileShare backupShare = fileClient.getShareReference(backupFileShareName);
        backupShare.createIfNotExists();
        CloudFileDirectory backupRootDir = backupShare.getRootDirectoryReference();
        download(rootDir, backupRootDir);
    }

}

我已经在我的本地环境中对其进行了测试。

希望能帮助到你。


更新:

对于资源名称使用无效字符的问题,请参考共享、目录、文件和元数据的命名和引用了解,并通过编码修复,例如使用url-encoding for /.


推荐阅读