【问题标题】:Spring Boot application to read data from FTP server and store in Azure blob storageSpring Boot 应用程序从 FTP 服务器读取数据并存储在 Azure Blob 存储中
【发布时间】:2021-05-25 17:10:57
【问题描述】:

如何通过 Spring Boot Rest API 从 FTP 读取示例文件 (blob) 的数据并上传到 Azure Blob 存储?

【问题讨论】:

    标签: azure spring-boot azure-blob-storage


    【解决方案1】:

    请参考我的代码,此方案将ftp文件保存为本地临时文件,然后上传到Azure存储,上传后可能需要删除。

            // Create a BlobServiceClient object which will be used to create a container client
            BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
            // Create the container and return a container client object
            BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
            // Get a reference to a blob
            BlobClient blobClient = containerClient.getBlobClient(fileName);
    
            FTPClient ftpClient = new FTPClient();
            try {
                ftpClient.connect(server);
                ftpClient.login(username, password);
    
                File file = new File(ftpFileName);
                if(file.exists()) {
                    file.delete();
                }
                ftpClient.changeWorkingDirectory(directory);
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                OutputStream outputStream = null;
                boolean success = false;
                outputStream = new BufferedOutputStream(new FileOutputStream(ftpFileName));
                success = ftpClient.retrieveFile(ftpFileName, outputStream);
    
                blobClient.uploadFromFile(ftpFileName);
                System.out.println("Done");
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                ...
            }
    

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2016-08-23
      • 2017-02-20
      • 2020-12-31
      • 2018-05-27
      • 2021-02-22
      • 2020-10-21
      • 2012-06-16
      • 2019-07-29
      相关资源
      最近更新 更多