먼저, FTP 서버를 만든다.
FTP 서버 간 파일 이동을 테스트하려면 FTP 서버가 두개 필요한데, 한개는 파일질라로 만들었고 한개는 팀장님께서 테스트하라고 할당해주셨다.
basic-ftp
를 사용해 구현하였다./* basic-ftp 라이브러리를 가져온다 이후에는 생략하겠음 */
const ftp = require("basic-ftp");
async function download(){
const client = new ftp.Client();
client.ftp.verbose = true; // 자세한 통신과정을 보고 싶으면 true, 생략하고 싶으면 false로 설정하면 된다.
try{
await client.access({
host: "다운로드 받을 FTP 주소",
user: "FTP에 접속하기 위해 만들었던 user 이름",
password: "FTP에 접속하기 위해 만들었던 user password", // type: string!!
});
await client.cd("/Test"); // 서버에 접속 후 서버에 만들어둔 Test 폴더로 이동
await client.downloadTo("test.txt", "test.txt"); // 첫번째 인자는 저장할 때 어떤 이름으로 저장할것인지, 두번째 인자는 가져올 파일
} catch (err){
console.log(err);
client.close();
return false;
}
client.close();
return true;
}
Test
폴더에 있는 test.txt
파일을 test.txt
파일 명으로 download 한다.test.txt
파일을 다른 FTP 서버로 upload하려 한다.async function upload() {
const client = new ftp.Client();
client.ftp.verbose = true; // 통신 상세 과정 볼거면 true, 아니면 false
try {
await client.access({
host: "업로드 FTP 주소",
user: "FTP에 접속하기 위해 만들었던 user 이름",
password: "FTP에 접속하기 위해 만들었던 user password", // type: string!!
});
await client.cd("/Server"); // 서버에 접속 후, 업로드할 폴더로 이동
await client.uploadFrom(
"어떤이름으로 업로드?",
"어떤 파일 가져올것?"
);
} catch (err) {
console.log(err);
client.close();
return false;
}
client.close();
return true;
}
const deleteFile = () => {
fs.unlinkSync(
"지울 파일의 경로"
);
};
/* 대충 이런식으로... */
function test = async() => {
const downloadingFlag = await download();
const uploadingFlag = await upload();
deleteFile();
}