如何通过Node.js Express从Cloudinary中删除图像? [英] How to remove images from Cloudinary by Node.js Express?

查看:76
本文介绍了如何通过Node.js Express从Cloudinary中删除图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上载和设置默认控制器功能正常运行.但是,我们也尝试从Cloudinary实现删除映像.如何做呢?在文档中令人困惑.这是代码:

Upload and set default controller functions are working perfectly. However, we are trying to implement delete image from Cloudinary as well. How can it be done? In the documentation it was confusing. Here is the code:

 const cloudinary = require('cloudinary');
    const HttpStatus = require('http-status-codes');

    const User = require('../models/userModels');

    cloudinary.config({
      cloud_name: 'name',
      api_key: 'key',
      api_secret: 'secret'
    });

    module.exports = {
      UploadImage(req, res) {
        cloudinary.uploader.upload(req.body.image, async result => {
          await User.update(
            {
              _id: req.user._id
            },
            {
              $push: {
                images: {
                  imgId: result.public_id,
                  imgVersion: result.version
                }
              }
            }
          )
            .then(() =>
              res
                .status(HttpStatus.OK)
                .json({ message: 'Image uploaded successfully' })
            )
            .catch(err =>
              res
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .json({ message: 'Error uploading image' })
            );
        });
      },

      DeleteImage(req, res) {
    cloudinary.uploader.destroy(req.params.image, async result => {
      await User.update(
        {
          _id: req.user._id
        },
        {
          $pull: {
            images: {
              imgId: result.public_id,
              imgVersion: result.version
            }
          }
        }
      )
        .then(() =>
          res
            .status(HttpStatus.OK)
            .json({ message: 'Image deleted successfully' })
        )
        .catch(err =>
          res
            .status(HttpStatus.INTERNAL_SERVER_ERROR)
            .json({ message: 'Error deleting image' })
        );
    });
  },

      async SetDefaultImage(req, res) {
        const { imgId, imgVersion } = req.params;

        await User.update(
          {
            _id: req.user._id
          },
          {
            picId: imgId,
            picVersion: imgVersion
          }
        )
          .then(() =>
            res.status(HttpStatus.OK).json({ message: 'Default image set' })
          )
          .catch(err =>
            res
              .status(HttpStatus.INTERNAL_SERVER_ERROR)
              .json({ message: 'Error occured' })
          );
      }
    };

我们正在将Node.js Express与Mongoose结合使用.我们如何在此处包括将删除图像的额外功能?

We are using Node.js Express with Mongoose. How can we include extra function here that will remove images?

推荐答案

有两种方法可以从cloudinary中删除图像:

There are two options to delete an image from cloudinary:

  1. 通过使用管理API.例如在Node中:

<代码>cloudinary.v2.api.delete_resources(['image1','image2'],函数(错误,结果){console.log(result);});

  1. 使用我们的上传API:

<代码>cloudinary.v2.uploader.destroy('sample',function(error,result){console.log(result,error)});

请注意,使用我们的管理API是受速率限制的,您可能要使用第二个选项.

Please note that using our admin API is rate limited and you might want to use the second option.

这篇关于如何通过Node.js Express从Cloudinary中删除图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆