ocanal's solution to "Node - Download a file from box ftp site and upload to google cloud bucket"

Hey, I wrote my solution and tested, it works like a charm, <\/p>\n\n

I used jsftp<\/a> to read file from ftp, and @google-cloud/storage<\/a> to upload bucket.<\/p>\n\n

Here is my working solution for file.txt<\/code><\/p>\n

const<\/span> <\/span>jsftp<\/span> <\/span>=<\/span> <\/span>require<\/span>(<\/span>&<\/span>quot<\/span>;<\/span>jsftp&quot<\/span>;);<\/span>\n\nconst<\/span> <\/span>ftp<\/span> <\/span>=<\/span> <\/span>new<\/span> <\/span>jsftp<\/span>({<\/span>\n  <\/span>host:<\/span> <\/span>&<\/span>quot<\/span>;<\/span>ftp_host&quot<\/span>;,<\/span> <\/span>\n  <\/span>port:<\/span> <\/span>21<\/span>,<\/span> <\/span>\n  <\/span>user:<\/span> <\/span>&<\/span>quot<\/span>;<\/span>ftp_user&quot<\/span>;,<\/span> <\/span>\n  <\/span>pass:<\/span> <\/span>&<\/span>quot<\/span>;<\/span>ftp_pass&quot<\/span>;<\/span> <\/span>\n});<\/span>\n\nconst<\/span> <\/span>{<\/span>Storage<\/span>}<\/span> <\/span>=<\/span> <\/span>require<\/span>(<\/span>'@google-cloud/storage');<\/span>\n\nconst<\/span> <\/span>GOOGLE_CLOUD_PROJECT_ID<\/span> <\/span>=<\/span> <\/span>'your-project-id'; // Replace with your project ID<\/span>\nconst<\/span> <\/span>GOOGLE_CLOUD_KEYFILE<\/span> <\/span>=<\/span> <\/span>'cloud-private-key.json'; // Replace with the path to the downloaded private key<\/span>\n\nconst<\/span> <\/span>storage<\/span> <\/span>=<\/span> <\/span>new<\/span> <\/span>Storage<\/span>({<\/span>\n    <\/span>projectId:<\/span> <\/span>GOOGLE_CLOUD_PROJECT_ID<\/span>,<\/span>\n    <\/span>keyFilename:<\/span> <\/span>GOOGLE_CLOUD_KEYFILE<\/span>,<\/span>\n});<\/span>\n\njsftp<\/span>.<\/span>prototype<\/span>.<\/span>getBuffer<\/span> <\/span>=<\/span> <\/span>function<\/span>(<\/span>remotePath<\/span>,<\/span> <\/span>callback<\/span>)<\/span> <\/span>{<\/span>\n  <\/span>var<\/span> <\/span>buffer<\/span> <\/span>=<\/span> <\/span>new<\/span> <\/span>Buffer<\/span>(<\/span>0<\/span>);<\/span>\n\n  <\/span>this<\/span>.<\/span>get<\/span>(<\/span>remotePath<\/span>,<\/span> <\/span>function<\/span>(<\/span>err<\/span>,<\/span> <\/span>socket<\/span>)<\/span> <\/span>{<\/span>\n    <\/span>if<\/span> <\/span>(<\/span>err<\/span>)<\/span> <\/span>{<\/span>\n      <\/span>return<\/span> <\/span>callback<\/span>(<\/span>err<\/span>);<\/span>\n    <\/span>}<\/span>\n\n    <\/span>socket<\/span>.<\/span>on<\/span>(<\/span>'data', function(data) {<\/span>\n      <\/span>buffer<\/span> <\/span>=<\/span> <\/span>Buffer<\/span>.<\/span>concat<\/span>([<\/span>buffer<\/span>,<\/span> <\/span>data<\/span>]);<\/span>\n    <\/span>});<\/span>\n\n    <\/span>socket<\/span>.<\/span>on<\/span>(<\/span>'close', function() {<\/span>\n      <\/span>if<\/span> <\/span>(<\/span>err<\/span>)<\/span> <\/span>{<\/span>\n        <\/span>return<\/span> <\/span>callback<\/span>(<\/span>err<\/span>);<\/span>\n      <\/span>}<\/span>\n\n      <\/span>callback<\/span>(<\/span>null<\/span>,<\/span> <\/span>buffer<\/span>);<\/span>\n    <\/span>});<\/span>\n\n    <\/span>socket<\/span>.<\/span>resume<\/span>();<\/span>\n  <\/span>});<\/span>\n};<\/span>\n\nftp<\/span>.<\/span>getBuffer<\/span>(<\/span>'file.txt', function(hadErr, data) {<\/span>\n    <\/span>if<\/span> <\/span>(<\/span>hadErr<\/span>)<\/span>\n      <\/span>console<\/span>.<\/span>error<\/span>(<\/span>hadErr<\/span>);<\/span>\n    <\/span>else<\/span> <\/span>{<\/span>\n        <\/span>console<\/span>.<\/span>log<\/span>(<\/span>data<\/span>.<\/span>toString<\/span>());<\/span>\n        <\/span>var<\/span> <\/span>fs<\/span> <\/span>=<\/span> <\/span>require<\/span>(<\/span>'fs');<\/span>\n        <\/span>var<\/span> <\/span>myBucket<\/span> <\/span>=<\/span> <\/span>storage<\/span>.<\/span>bucket<\/span>(<\/span>'your-bucket-name'); //replace your bucket name<\/span>\n\n        <\/span>var<\/span> <\/span>file<\/span> <\/span>=<\/span> <\/span>myBucket<\/span>.<\/span>file<\/span>(<\/span>'file.txt');<\/span>\n\n        <\/span>file<\/span>.<\/span>createWriteStream<\/span>()<\/span>\n          <\/span>.<\/span>on<\/span>(<\/span>'error', function(err) {<\/span>\n            <\/span>console<\/span>.<\/span>log<\/span>(<\/span>err<\/span>);<\/span>\n          <\/span>})<\/span>\n          <\/span>.<\/span>on<\/span>(<\/span>'finish', function() {<\/span>\n            <\/span>console<\/span>.<\/span>log<\/span>(<\/span>'file is uploaded');<\/span>\n          <\/span>})<\/span>\n          <\/span>.<\/span>end<\/span>(<\/span>data<\/span>);<\/span>\n    <\/span>}<\/span>\n\n  <\/span>});<\/span>\n<\/pre><\/div>
Hey, I wrote my solution and tested, it works like a charm, I used [jsftp](https://www.npmjs.com/package/jsftp) to read file from ftp, and [@google-cloud/storage](https://www.npmjs.com/package/@google-cloud/storage) to upload bucket. Before starting, - You have to create and download json `service account key` to your project folder, which is named `cloud-private-key.json` in my code. - install jsftp with `npm i jsftp` - install @google-cloud/storage with `npm i @google-cloud/storage` - don't forget to replace your `ftp settings`, `your-project-id` and `your-bucket-name` Here is my working solution for `file.txt` const jsftp = require("jsftp"); const ftp = new jsftp({ host: "ftp_host", port: 21, user: "ftp_user", pass: "ftp_pass" }); const {Storage} = require('@google-cloud/storage'); const GOOGLE_CLOUD_PROJECT_ID = 'your-project-id'; // Replace with your project ID const GOOGLE_CLOUD_KEYFILE = 'cloud-private-key.json'; // Replace with the path to the downloaded private key const storage = new Storage({ projectId: GOOGLE_CLOUD_PROJECT_ID, keyFilename: GOOGLE_CLOUD_KEYFILE, }); jsftp.prototype.getBuffer = function(remotePath, callback) { var buffer = new Buffer(0); this.get(remotePath, function(err, socket) { if (err) { return callback(err); } socket.on('data', function(data) { buffer = Buffer.concat([buffer, data]); }); socket.on('close', function() { if (err) { return callback(err); } callback(null, buffer); }); socket.resume(); }); }; ftp.getBuffer('file.txt', function(hadErr, data) { if (hadErr) console.error(hadErr); else { console.log(data.toString()); var fs = require('fs'); var myBucket = storage.bucket('your-bucket-name'); //replace your bucket name var file = myBucket.file('file.txt'); file.createWriteStream() .on('error', function(err) { console.log(err); }) .on('finish', function() { console.log('file is uploaded'); }) .end(data); } });

User: ocanal

Question: Node - Download a file from box ftp site and upload to google cloud bucket

Back to question