【问题标题】:Delete an image from Cloudinary using REST full api使用 RESTful api 从 Cloudinary 中删除图像
【发布时间】:2020-05-05 06:02:51
【问题描述】:

如何使用简单的 REST api 从我的云服务器中删除图像?虽然我能够成功上传图片,但找不到使用 api 删除图片的正确方法

【问题讨论】:

    标签: cloudinary


    【解决方案1】:

    如果您使用 Cloudinary 服务器端 SDK 之一,则可以使用 Upload API 的 destroy 方法从 Cloudinary 删除资源。

    如果您直接调用 API,那么您可以使用 cURL。例如,要从云my_cloud 中删除具有 public_id sample_image 的图像,我可以执行以下操作。

    导出所有需要的变量:

    export CLD_NAME=my_cloud
    export CLD_API_KEY=12345
    export CLD_API_SECRET=abcde
    

    生成签名:

    export TIME=$(date +%s)
    
    export SIGNATURE=$(echo -n "invalidate=true&public_id=sample_image&timestamp=$TIME$CLD_API_SECRET" | openssl sha1)
    
    curl -X POST https://api.cloudinary.com/v1_1/$CLD_NAME/image/destroy --data "invalidate=true&public_id=sample_image&timestamp=$TIME&signature=$SIGNATURE&api_key=$CLD_API_KEY"
    

    有关直接 API 调用的更多信息,请参阅文档的这一部分 - https://cloudinary.com/documentation/upload_images#generating_authentication_signatures

    【讨论】:

    • 我需要一个可以通过 Postman 客户端调用的简单 RESTful api
    【解决方案2】:

    上传图片时,public_id 应该保存在数据库中。否则,您无法使用 URL 删除图像。我尝试在 React Js 中上传图片。

    ``

    onClickHandler = () => {
            const data = new FormData()
            for(var x = 0; x<this.state.selectedFile.length; x++) {
                data.append('file', this.state.selectedFile[x])
                data.append("upload_preset","*****")
                data.append("cloud_name","*****")
                axios.post("https://api.cloudinary.com/v1_1/*****/image/upload",data)
                  .then((res) => {
                      console.log(res.data);
                    console.log(res.data);
                    const imageDetails={
                        image:res.data.secure_url,
                        public_id:res.data.public_id,
                        productName:"*******"
                    }
                    axios.post("http://localhost:4000/*********",imageDetails)
                    .then(res=>console.log(res))
                  })
            }
    

    `` 然后你可以使用 node js 删除图像。请参阅以下链接以获取节点 js 代码 Delete cloudinary images in node js

    【讨论】:

      猜你喜欢
      • 2020-03-02
      • 2020-12-18
      • 2020-11-05
      • 2015-09-30
      • 2015-08-11
      • 2019-01-30
      • 2019-05-25
      • 2017-10-25
      • 2021-11-12
      相关资源
      最近更新 更多