将文件从Azure文件共享目录移动到Unix目录和备份目录(在Azure文件共享内)

时间:2019-06-07 19:37:54

标签: java azure

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

我写了一种方法,可以根据文件名将文件从Azure文件共享目录移动到UNIX目录。但是我需要更改它,以便它移动所有文件并进行备份。 源目录地址看起来像这样: Z:\业务 备份目录已经创建: Z:\ Business \ Backup 在“业务”下没有子目录,仅文件和名称以Data_Files_yyyymmdd开头。

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

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

但我收到以下错误消息:- [错误] com.microsoft.azure.storage.StorageException:指定的资源名称包含无效字符。 我试图修复它,但无法修复。 我尝试将backupFileShareName更改为如下所示的其他名称,但两者均无法正常工作。 尝试1)静态字符串backupFileShareName =“业务/备份”; 尝试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());
    }
}

2 个答案:

答案 0 :(得分:0)

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

这是我的适用于Java的Azure存储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);
    }

}

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

希望有帮助。


更新:

对于资源名称中使用的无效字符的问题,请参考Naming and Referencing Shares, Directories, Files, and Metadata进行了解,并通过编码进行修复,例如对/使用url-encoding。

答案 1 :(得分:0)

感谢Peter..it在目录名称中进行了很小的更改。