Download File avatar
Download File

Deprecated

Pricing

Pay per usage

Go to Store
Download File

Download File

Deprecated

Developed by

Jan Uhlář

Jan Uhlář

Maintained by Community

GET image/video and store locally

0.0 (0)

Pricing

Pay per usage

2

Total users

57

Monthly users

1

Last modified

4 years ago

Dockerfile

# Dockerfile contains instructions how to build a Docker image that
# will contain all the code and configuration needed to run your actor.
# For a full Dockerfile reference,
# see https://docs.docker.com/engine/reference/builder/
# First, specify the base Docker image. Apify provides the following
# base images for your convenience:
# apify/actor-node-basic (Node.js on Alpine Linux, small and fast)
# apify/actor-node-chrome (Node.js + Chrome on Debian)
# apify/actor-node-chrome-xvfb (Node.js + Chrome + Xvfb on Debian)
# For more information, see https://docs.apify.com/actor/build#base-images
# Note that you can use any other image from Docker Hub.
FROM apify/actor-node-chrome
# Second, copy just package.json since it should be the only file
# that affects "npm install" in the next step, to speed up the build
COPY package.json ./
# Install NPM packages, skip optional and development dependencies to
# keep the image small. Avoid logging too much and print the dependency
# tree for debugging
RUN npm --quiet set progress=false \
&& npm install --only=prod --no-optional \
&& echo "Installed NPM packages:" \
&& npm list || true \
&& echo "Node.js version:" \
&& node --version \
&& echo "NPM version:" \
&& npm --version
# Next, copy the remaining files and directories with the source code.
# Since we do this after NPM install, quick build will be really fast
# for most source file changes.
COPY . ./
# Optionally, specify how to launch the source code of your actor.
# By default, Apify's base Docker images define the CMD instruction
# that runs the Node.js source code using the command specified
# in the "scripts.start" section of the package.json file.
# In short, the instruction looks something like this:
#
# 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

{
"name": "my-actor",
"version": "0.0.1",
"dependencies": {
"apify": "^0.20.4",
"request-promise": "^4.2.5"
},
"scripts": {
"start": "node main.js"
},
"author": "Me!"
}