要在不生成本地文件的情况下将图像上传到Cloudinary,您可以使用以下示例代码:
首先,您需要安装cloudinary
模块。您可以使用以下命令在您的项目中安装它:
npm install cloudinary
然后,您可以使用以下代码将图像上传到Cloudinary:
const cloudinary = require('cloudinary').v2;
cloudinary.config({
cloud_name: 'YOUR_CLOUD_NAME',
api_key: 'YOUR_API_KEY',
api_secret: 'YOUR_API_SECRET'
});
const imageUrl = 'https://example.com/image.jpg'; // 替换为您要上传的图像的 URL
cloudinary.uploader.upload(imageUrl, { public_id: 'uploaded_image' }, (error, result) => {
if (error) {
console.log('上传失败:', error);
} else {
console.log('上传成功:', result);
}
});
请确保将YOUR_CLOUD_NAME
、YOUR_API_KEY
和YOUR_API_SECRET
替换为您的Cloudinary帐户的正确凭据。
上述代码使用cloudinary
模块的uploader.upload
方法将指定的图像URL上传到Cloudinary。public_id
参数用于指定上传图像的公共ID,您可以根据需要进行更改。
成功上传后,将在控制台打印出上传结果对象。
请注意,这种方法不会在本地生成图像文件,而是直接从指定的URL上传图像到Cloudinary。