FTP file uploader avatar

FTP file uploader

Try for free

No credit card required

View all Actors
FTP file uploader

FTP file uploader

petr_cermak/ftp-upload
Try for free

No credit card required

Performs a simple upload of an array of files to an FTP.

Dockerfile

1# First, specify the base Docker image. You can read more about
2# the available images at https://sdk.apify.com/docs/guides/docker-images
3# You can also use any other image from Docker Hub.
4FROM apify/actor-node:16
5
6# Second, copy just package.json and package-lock.json since those are the only
7# files that affect "npm install" in the next step, to speed up the build.
8COPY package*.json ./
9
10# Install NPM packages, skip optional and development dependencies to
11# keep the image small. Avoid logging too much and print the dependency
12# tree for debugging
13RUN npm --quiet set progress=false \
14 && npm install --only=prod --no-optional \
15 && echo "Installed NPM packages:" \
16 && (npm list || true) \
17 && echo "Node.js version:" \
18 && node --version \
19 && echo "NPM version:" \
20 && npm --version
21
22# Next, copy the remaining files and directories with the source code.
23# Since we do this after NPM install, quick build will be really fast
24# for most source file changes.
25COPY . ./
26
27# Optionally, specify how to launch the source code of your actor.
28# By default, Apify's base Docker images define the CMD instruction
29# that runs the Node.js source code using the command specified
30# in the "scripts.start" section of the package.json file.
31# In short, the instruction looks something like this:
32#
33# CMD npm start

INPUT_SCHEMA.json

1{
2    "title": "FTP file uploader",
3    "type": "object",
4    "schemaVersion": 1,
5    "properties": {
6        "username": {
7            "title": "Username",
8            "type": "string",
9            "editor": "textfield",
10            "description": "FTP username"
11        },
12        "password": {
13            "title": "Password",
14            "type": "string",
15            "editor": "textfield",
16            "description": "FTP password"
17        },
18        "host": {
19            "title": "Host",
20            "type": "string",
21            "editor": "textfield",
22            "description": "FTP host"
23        },
24        "port": {
25            "title": "Port",
26            "type": "integer",
27            "prefill": 21,
28            "default": 21,
29            "description": "FTP port"
30        },
31        "folder": {
32            "title": "FTP folder",
33            "type": "string",
34            "editor": "textfield",
35            "prefill": "/",
36            "default": "/",
37            "description": "FTP folder"
38        },
39        "fileUrls": {
40            "title": "File URLs",
41            "type": "array",
42            "editor": "json",
43            "prefill": [],
44            "description": "File URLs"
45        }
46    },
47    "required": ["username", "password", "host", "fileUrls"]
48}

main.js

1const fs = require('fs');
2const Apify = require('apify');
3const FtpClient = require('ftp-client');
4
5const connect = (ftpClient) => new Promise((resolve, reject) => {
6    ftpClient.connect((err) => {
7        if(err){reject(err);}
8        else{resolve();}
9    });
10});
11
12const upload = (ftpClient, files, outFolder) => new Promise((resolve) => {
13    ftpClient.upload(files, outFolder || '/', {
14        baseDir: outFolder || '/',
15        overwrite: 'older'
16    }, function (result) {
17        resolve(result);
18    });
19});
20
21Apify.main(async () => {
22    const input = await Apify.getInput();
23    if(!input.host){throw new Error('Missing "host" attribute in INPUT!');}
24    if(!input.username){throw new Error('Missing "username" attribute in INPUT!');}
25    if(!input.password){throw new Error('Missing "password" attribute in INPUT!');}
26    if(!input.fileUrls){throw new Error('Missing "fileUrls" attribute in INPUT!');}
27    
28    const config = {
29        host: input.host,
30        port: input.port || 21,
31        user: input.username,
32        password: input.password
33    };
34
35    const options = {
36        overwrite: 'older',
37        logging: 'basic'
38    };
39
40    const client = new FtpClient(config, options);
41    await connect(client);
42
43    const fileNames = [];
44    for(const url of input.fileUrls){
45        const response = await Apify.utils.requestAsBrowser({url});
46        const data = response.rawBody;
47        if(data){
48            const fileName = url.slice(url.lastIndexOf('/') + 1);
49            fs.writeFileSync(fileName, data);
50            fileNames.push(fileName)
51        }
52    }
53    await upload(client, fileNames, input.folder);
54});

package.json

1{
2    "name": "ftp-upload",
3    "version": "0.0.1",
4    "dependencies": {
5        "apify": "^2.0.2",
6        "ftp-client": "^0.2.2"
7    },
8    "scripts": {
9        "start": "node main.js"
10    },
11    "author": "Petr Čermák"
12}
Developer
Maintained by Community

Actor Metrics

  • 2 monthly users

  • 0 No stars yet

  • 88% runs succeeded

  • Created in Aug 2021

  • Modified 2 years ago