How to read data for example files ( blob ) from FTP via Spring Boot Rest API and upload into Azure Blob storage ?
1 Answers
0
votes
Please refer to my code, this solution saves the ftp file as a local temporary file and then uploads it to Azure storage, maybe you need to delete it after uploading.
// 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 {
...
}