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

Java 将文件从Azure文件共享目录移动到unix目录和备份目录(在Azure文件共享中),java,azure,Java,Azure,需要将Azure文件共享上的目录中的所有文件移动到Unix目录。移动后,在备份目录中备份这些文件 我写了一个方法,根据文件名将文件从Azure文件共享目录移动到unix目录。但我需要更改它,以便它移动所有文件并进行备份。 源目录地址如下所示:- Z:\Business 已创建备份目录,该目录为:- Z:\Business\Backup 业务下没有子目录,只有文件和名称以Data_files_yyyymmdd开头 第二步, 需要将所有文件从目录移动到unix目录 编辑:1- 我对代码进行了一些编辑

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

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

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

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

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

static String connectionString=“DefaultEndpointsProtocol=https;AccountName=elkdemastershare;AccountKey=zdqwmyhgdbvjwy85iapp5czavk2cgzvucqyqikwhdcwbi0bge/WNkQsW+CPWWRJN1JITFkYaWm0bGqOIEJnUg=”;EndpointSuffix=core.windows.net”;
静态字符串fileShareName=“Business”;
静态字符串localRootDirPath=“/cogn_shared/TgtFiles/test_data/”;
静态字符串backupFileShareName=“业务/备份”;
公共静态void下载(CloudFileDirectory根目录、CloudFileDirectory备份)引发StorageException、URISyntaxException、FileNotFoundException{
System.out.println(“=>\t”+root.getName());
ResultSegmentlist=root.listFilesAndDirectoriesSegmented();
对于(ListFileItem项:list.getResults()){
URI=item.getUri();
//需要将Azure文件共享上的目录中的所有文件移动到Unix目录。移动后,在备份目录中备份这些文件。
//我已经写了一个方法,根据文件名将文件从Azure文件共享目录移动到unix目录。但是我需要更改它,以便它移动所有文件并进行备份。
//需要将所有文件从目录移动到unix目录。
String path=uri.getPath();
字符串localPath=localRootDirPath+path;
String itemName=新文件(路径).getName();
布尔标志=isDir(根,项名称);
System.out.println(item.getUri()+“\t”+path+“\t”+itemName+“\t”+标志);
国际单项体育联合会(旗){
//创建本地目录
新文件(localPath).mkdirs();
CloudFileDirectory next=root.getDirectoryReference(itemName);
//为备份创建云目录
CloudFileDirectory backupNext=backup.getDirectoryReference(itemName);
backupNext.createIfNotExists();
//递归
下载(下一步,备份下一步);
}否则{
//将文件下载到本地
FileOutputStream fos=新的FileOutputStream(localPath);
CloudFile file=root.getFileReference(itemName);
文件下载(fos);
//开始复制到云目录进行备份,无需再次上载
CloudFile backupFile=backup.getFileReference(itemName);
备份文件.startCopy(文件);
System.out.println(“下载”+路径);
}
}
}
公共静态布尔isDir(CloudFileDirectory根目录,字符串itemName)抛出URISyntaxException、StorageException{
CloudFileDirectory dir=root.getDirectoryReference(itemName);
布尔标志=真;
试一试{
dir.listFilesAndDirectoriesSegmented();
}捕获(存储异常){
flag=false;
}
返回标志;
}
公共静态无效主代码(字符串连接字符串){
试一试{
CloudStorageAccount=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();
下载(rootDir、backupRootDir);
}捕获(例外e){
e、 printStackTrace();
//System.out.println(e.getMessage());
}
}

听起来您想将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);
    }

}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.net.URI;
导入java.net.URISyntaxException;
导入java.security.InvalidKeyException;
导入com.microsoft.azure.storage.CloudStorageAccount;
导入com.microsoft.azure.storage.ResultSegment;
导入com.microsoft.azure.storage.StorageException;
导入com.microsoft.azure.storage.file.CloudFile;
导入com.microsoft.azure.storage.file.CloudFileClient;
导入com.microsoft.azure.storage.file.CloudFileDirectory;
导入com.microsoft.azure.storage.file.CloudFileShare;
导入com.microsoft.azure.storage.file.ListFileItem;
公共类从文件共享下载文件{
私有静态最终字符串连接String=“DefaultEndpointsProtocol=https;AccountName=;AccountKey=;EndpointSuffix=core.windows.net;”;
私有静态最终字符串fileShareName=“”;
私有静态最终字符串localRootDirPath=“”;
私有静态最终字符串备份文件共享名
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);
    }

}