Created
October 2, 2023 07:54
-
-
Save optlsnd/fb998b6dd9255e2c802aad913710b334 to your computer and use it in GitHub Desktop.
Get a list of files from Uploadcare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const { stringify } = require("csv-stringify"); | |
const { | |
listOfFiles, | |
UploadcareSimpleAuthSchema, | |
paginate, | |
} = require("@uploadcare/rest-client"); | |
const filename = "output.csv"; | |
const writableStream = fs.createWriteStream(filename); | |
// define columns | |
// refer to https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/fileInfo | |
// for fileInfo structure | |
const columns = ["uuid", "original_filename", "size", "mime_type"]; | |
const stringifier = stringify({ header: true, columns }); | |
stringifier.pipe(writableStream); | |
const uploadcareSimpleAuthSchema = new UploadcareSimpleAuthSchema({ | |
publicKey: "YOUR_PUBLIC_API_KEY", | |
secretKey: "YOUR_SECRET_API_KEY", | |
}); | |
const paginatedListOfFiles = paginate(listOfFiles); | |
const pages = paginatedListOfFiles( | |
{ limit: 1000 }, | |
{ authSchema: uploadcareSimpleAuthSchema } | |
); | |
(async () => { | |
let total = 0; | |
let processed = 0; | |
for await (const page of pages) { | |
if (!total) { | |
total = page.total; | |
} | |
page.results.forEach((fileInfo) => { | |
const { uuid, originalFilename, size, mimeType } = fileInfo; | |
const row = [uuid, originalFilename, size, mimeType]; | |
stringifier.write(row); | |
}); | |
processed += page.results.length; | |
console.clear(); | |
console.log(`Processed ${processed}/${total}`); | |
} | |
console.log("Finished writing data"); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "uploadcare-get-files", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@uploadcare/rest-client": "^6.6.1", | |
"csv-stringify": "^6.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment