Dropbox Upload Actor
Pricing
Pay per usage
Go to Store
Dropbox Upload Actor
Streams file from URL to a dropbox account.
0.0 (0)
Pricing
Pay per usage
2
Total users
6
Monthly users
2
Last modified
3 years ago
Dockerfile
# This is a template for a Dockerfile used to run acts in Actor system.# The base image name below is set during the act build, based on user settings.# IMPORTANT: The base image must set a correct working directory, such as /usr/src/app or /home/userFROM apify/actor-node-basic:v0.21.10
# Second, copy just package.json and package-lock.json since it should be# the only file that affects "npm install" in the next step, to speed up the buildCOPY 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 debuggingRUN npm --quiet set progress=false \ && npm install --only=prod --no-optional \ && echo "Installed NPM packages:" \ && (npm list --all || true) \ && echo "Node.js version:" \ && node --version \ && echo "NPM version:" \ && npm --version
# Copy source code to container# Do this in the last step, to have fast build if only the source code changedCOPY . ./
# NOTE: The CMD is already defined by the base image.# Uncomment this for local node inspector debugging:# CMD [ "node", "--inspect=0.0.0.0:9229", "main.js" ]
package.json
{ "name": "apify-project", "version": "0.0.1", "description": "", "author": "It's not you it's me", "license": "ISC", "dependencies": { "node-fetch": "latest", "dropbox": "latest", "apify": "0.21.10" }, "scripts": { "start": "node main.js" }}
main.js
1const fetch = require('node-fetch');2var Dropbox = require('dropbox').Dropbox;3const Apify = require('apify');4
5Apify.main(async () => {6 const input = await Apify.getValue('INPUT');7 const accessToken = input.accessToken;8 const filePath = input.filePath ? input.filePath : '';9 const fileUrl = input.fileUrl;10
11 await uploadFile(fileUrl, filePath);12
13 async function uploadFile(fileUrl, filePath){ 14 if(typeof fileUrl !== 'string'){15 throw new TypeError('Parameter is not type of string.');16 }17 18 // Open Dropbox instance.19 var dbx = await new Dropbox({20 fetch: fetch,21 accessToken: accessToken22 });23 24 const UPLOAD_FILE_SIZE_LIMIT = 150 * 1024 * 1024; // 150Mb - Dropbox file size threshold 25 const maxBlob = 8 * 1000 * 1000; // 8Mb - Dropbox JavaScript API suggested max file / chunk size26 27 const buffer = await getFileBuffer(fileUrl);28 console.log(`File size: ${(buffer.byteLength/1000000).toFixed(2)}Mb`);29 30 // Determine whether you should use filesUpload or filesUploadSession.31 if(buffer.byteLength < UPLOAD_FILE_SIZE_LIMIT){32 // Upload for files less than 150Mb don't have to be chunked.33 console.log('Uploading file.');34 await dbx.filesUpload({ path: filePath, contents:buffer })35 .then(res => console.log('File successfully uploaded.'))36 .catch(err => console.log(err));37 } else {38 var chunks = [];39 var offset = 0;40 41 while(offset < buffer.byteLength){42 var chunkSize = Math.min(maxBlob, buffer.byteLength - offset);43 chunks.push(buffer.slice(offset, offset + chunkSize ));44 offset += chunkSize;45 }46 47 var index = 0;48
49 for(chunk of chunks){50 var percentToFinishUpload;51
52 if (index == 0){53 // Begin upload session.54 var {session_id} = await dbx.filesUploadSessionStart({ close: false, contents: chunk });55 console.log(`Opening session: ${session_id}`);56 index++;57
58 percentToFinishUpload = ((index * chunk.byteLength)/buffer.byteLength * 100).toFixed(2);59 console.log(`Upload percentage: ${percentToFinishUpload}%`);60 } else if (index < chunks.length - 1){61 // Append chunk to upload session.62 let cursor = { session_id: session_id, offset: index * chunk.byteLength };63 await dbx.filesUploadSessionAppendV2({ cursor: cursor, close: false, contents: chunk});64 index++;65
66 percentToFinishUpload = Math.floor(((index * chunk.byteLength)/buffer.byteLength) * 100).toFixed(2);67 console.log(`Upload percentage: ${percentToFinishUpload}%`);68 } else {69 // Append final chunk and close session.70 let cursor = { session_id: session_id, offset: buffer.byteLength - chunk.byteLength };71 let commit = { path: input.filePath, mode: 'add', autorename: true, mute: false};72 await dbx.filesUploadSessionFinish({ cursor: cursor, commit: commit, contents: chunk })73 .then(res => console.log('Success!'));74
75 percentToFinishUpload = Math.floor(((index * chunk.byteLength)/buffer.byteLength) * 100).toFixed(2);76 console.log('File upload complete.');77 }78 }79 }80 }81 82 async function getFileBuffer(fileUrl){83 console.log(`Fetching file from: ${fileUrl}`);84 85 const blob = await fetch(fileUrl)86 .then(response => response.blob())87 .catch(err => console.log(err));88 89 console.log('File successfully retrieved.');90 console.log('Creating buffer from file.');91 const buf = Buffer.from(blob[Object.getOwnPropertySymbols(blob)[1]]);92 93 console.log('Returning buffer.');94 return buf;95 }96});