[NodeJS] Upload file to FTP

He SEO·2022년 3월 10일
0

Upload to FTP 🎃

Ftp 서버에 특정 파일을 업로드 하는 코드를 작성해봅니다.
입력 파일이 아닌 JSON string을 받아 원격지에 파일로 저장할 예정입니다.

여러 라이브러리가 있지만 그 중에서 basic-ftp를 사용합니다.

npm install --save basic-ftp

Sample

//basic-ftp 라이브러리를 불러온다
import * as ftp from 'basic-ftp'

// inputText : {"name":"Gildong Hong", "age":20}
async function upload(inputText: string) {
	const client = new ftp.Client()
    //client.ftp.verbose = true;
    
    // 접속 정보 세팅
    await client.access({
      host: "target.upload.com",
      user: "username",
      password: "password",
      port: 21,
    });

    const source = new Readable;
    source.push(inputText);
    source.push(null); //end file
  
    await client.uploadFrom(source, "/home/test/doc/text.txt") 
        .then((result) => {
          console.log("Upload success");
        })
        .catch((e) => {
          console.error(e);
        });
  	client.close();
}
profile
BACKEND 개발 기록 중. 감사합니다 😘

0개의 댓글