Last active
August 29, 2024 20:19
-
-
Save colbyfayock/e52cc95690cd4cbfcaea25937239fcb4 to your computer and use it in GitHub Desktop.
Collect images from Unsplash, then upload them to Cloudinary
This file contains hidden or 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
// Run this 1st: | |
// - Create a file locally such as unsplash.js | |
// - Paste the contents of this file | |
// - Update your Unsplash access key from https://unsplash.com/developers | |
// - Run `node unsplash.js` | |
import { createApi } from 'unsplash-js'; | |
import { promises as fs } from 'fs'; | |
const unsplash = createApi({ accessKey: '<Your Unsplash Access Key>' }); | |
const totalImages = 1200; | |
const imagesPerPage = 30; | |
const pages = totalImages / imagesPerPage | |
const photos = []; | |
for ( let i = 0; i < pages; i++ ) { | |
const { response } = await unsplash.photos.list({ | |
perPage: imagesPerPage, | |
page: i + 1 | |
}); | |
photos.push(...response.results); | |
await fs.writeFile('./photos.json', JSON.stringify(photos)); | |
await new Promise((resolve) => { | |
setTimeout(() => resolve(), 500); | |
}) | |
} | |
console.log('Image metadata saved to photos.json'); |
This file contains hidden or 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
// Run this 2nd | |
// - Create a file locally such as cloudinary.js | |
// - Paste the contents of this file | |
// - Update your Cloudinary credentials from https://console.cloudinary.com/console/ | |
// - Update the folder value or remove the property definition if uploading to root | |
// - Run `node cloudinary.js` | |
import { v2 as cloudinary } from 'cloudinary'; | |
import photos from './photos.json' assert { type: 'json' }; | |
cloudinary.config({ | |
cloud_name: '<Your Cloudinary Cloud Name>', | |
api_key: '<Your Cloudinary API Key>', | |
api_secret: '<Your Cloudinary API Secret>', | |
}) | |
for ( let i = 0; i < photos.length; i++ ) { | |
await cloudinary.uploader.upload(photos[i].links.download, { | |
folder: '<Your Folder>' | |
}); | |
await new Promise((resolve) => { | |
setTimeout(() => resolve(), 500); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment