-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathupload-image.js
More file actions
38 lines (33 loc) · 1.3 KB
/
Copy pathupload-image.js
File metadata and controls
38 lines (33 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Upload an image and print its public ID and an optimized delivery URL.
*
* Prerequisites: set CLOUDINARY_URL in your environment (Console > Settings > API Keys).
* In your own project, import the published package instead:
* const cloudinary = require('cloudinary').v2;
*/
const cloudinary = require('../cloudinary').v2;
async function main(imageSource = 'https://res.cloudinary.com/demo/image/upload/sample.jpg') {
const result = await cloudinary.uploader.upload(imageSource, {
// A stable public_id makes the asset addressable; omit it to get a random one.
public_id: 'examples/uploaded-sample',
overwrite: true
});
const optimizedUrl = cloudinary.url(result.public_id, {
fetch_format: 'auto',
quality: 'auto',
secure: true
});
console.log(`Stored reference: ${result.asset_id}`);
console.log(`Uploaded: ${result.public_id} (${result.bytes} bytes, ${result.width}x${result.height})`);
console.log(`Optimized delivery URL: ${optimizedUrl}`);
return result;
}
if (require.main === module) {
main().catch((error) => {
const { message } = error.error || error;
console.error(`Upload failed: ${message}`);
console.error('Check that CLOUDINARY_URL is set and the source file or URL is reachable.');
process.exitCode = 1;
});
}
module.exports = { main };