Download File avatar

Download File

Deprecated
View all Actors
This Actor is deprecated

This Actor is unavailable because the developer has decided to deprecate it. Would you like to try a similar Actor instead?

See alternative Actors
Download File

Download File

jan-uhlar/download-file

GET image/video and store locally

Dockerfile

1# Dockerfile contains instructions how to build a Docker image that
2# will contain all the code and configuration needed to run your actor.
3# For a full Dockerfile reference,
4# see https://docs.docker.com/engine/reference/builder/
5
6# First, specify the base Docker image. Apify provides the following
7# base images for your convenience:
8#  apify/actor-node-basic (Node.js on Alpine Linux, small and fast)
9#  apify/actor-node-chrome (Node.js + Chrome on Debian)
10#  apify/actor-node-chrome-xvfb (Node.js + Chrome + Xvfb on Debian)
11# For more information, see https://docs.apify.com/actor/build#base-images
12# Note that you can use any other image from Docker Hub.
13FROM apify/actor-node-chrome
14
15# Second, copy just package.json since it should be the only file
16# that affects "npm install" in the next step, to speed up the build
17COPY package.json ./
18
19# Install NPM packages, skip optional and development dependencies to
20# keep the image small. Avoid logging too much and print the dependency
21# tree for debugging
22RUN npm --quiet set progress=false \
23 && npm install --only=prod --no-optional \
24 && echo "Installed NPM packages:" \
25 && npm list || true \
26 && echo "Node.js version:" \
27 && node --version \
28 && echo "NPM version:" \
29 && npm --version
30
31# Next, copy the remaining files and directories with the source code.
32# Since we do this after NPM install, quick build will be really fast
33# for most source file changes.
34COPY . ./
35
36# Optionally, specify how to launch the source code of your actor.
37# By default, Apify's base Docker images define the CMD instruction
38# that runs the Node.js source code using the command specified
39# in the "scripts.start" section of the package.json file.
40# In short, the instruction looks something like this:
41#
42# CMD npm start

main.js

1const Apify = require('apify');
2const rp = require('request-promise');
3const path = require('path');
4const url = require('url');
5
6Apify.main(async () => {
7  const input = await Apify.getInput();
8  console.log('Input:');
9  console.dir(input);
10
11  const {fileUrl} = input;
12
13  // encoding: null means that we download raw binary data
14  // which is what we want for an image/video
15  // resolveWithFullResponse: true for parsing content-type
16  const proxyUrl = Apify.getApifyProxyUrl();
17  console.log('Using proxy:')
18  console.log(proxyUrl)
19  const res = await rp({url: fileUrl, encoding: null, resolveWithFullResponse: true, proxy: proxyUrl});
20  const buffer = Buffer.from(res.body, 'utf8');
21  const fileName = path.parse(url.parse(fileUrl).pathname).base;
22  const contentType = res.headers['content-type'];
23
24  console.log('File downloaded:');
25  console.log({name: fileName, contentType: contentType});
26
27  await Apify.setValue(fileName, buffer, {contentType: contentType});
28
29  const storeId = Apify.getEnv().defaultKeyValueStoreId;
30  const resultUrl = `https://api.apify.com/v2/key-value-stores/${storeId}/records/${fileName}?disableRedirect=true`;
31
32  console.log('File is saved at:');
33  console.log(resultUrl);
34
35  await Apify.setValue('OUTPUT', {url: resultUrl});
36});

package.json

1{
2    "name": "my-actor",
3    "version": "0.0.1",
4    "dependencies": {
5        "apify": "^0.20.4",
6        "request-promise": "^4.2.5"
7
8    },
9    "scripts": {
10        "start": "node main.js"
11    },
12    "author": "Me!"
13}
Developer
Maintained by Community
Categories