有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将文件从Azure文件共享目录移动到unix目录和备份目录(在Azure文件共享中)

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

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

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

编辑:1- 我对代码进行了一些编辑,因为我在一个工具中运行它。 并将代码称为:- 主代码(AzureStorageConnectionString)

但我得到的错误是:- [错误]com。微软蔚蓝色的存储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());
    }
}

共 (1) 个答案

  1. # 1 楼答案

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

    以下是我的Azure Storage SDK v8 for Java示例代码(我看到您使用了相同的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);
        }
    
    }
    

    我已经在本地环境中测试了它

    希望能有帮助


    更新:

    有关资源名称中使用的无效字符的问题,请参考Naming and Referencing Shares, Directories, Files, and Metadata了解它,并通过编码修复它,例如使用/的url编码