所以我试图通过编程从我的一个桶中删除Wasabi CDN对象.我的请求正在发回204并显示成功,但没有任何内容被移动/删除.我正在使用node/javascript来实现这一点.

下面是我的函数,它应该删除bucket.

import expressAsyncHandler from 'express-async-handler'
import User from '../../models/User.js'
import axios from 'axios'
import aws4 from 'aws4'

/**
 * @desc:   THIS is going to be a testing function that will be added into admin delete user and all related docs.
 * @route:  DELETE
 * @access: Private - Admin Route for when deleting user will delete the CDN username bucket aswell
 * @goalHere: The goal of this function is to delete the user bucket from CDN. So that if we d
 * @comment: This is a testing function that will be added into deleteVideo.js. Unless we just await this function in deleteVideo.js.
 */

export const deleteUserBucket = expressAsyncHandler(async (req, res, next) => {
  try {
    const user = await User.findOne({ username: req.user.username })
    const username = user.username

    let request = {
      host: process.env.CDN_HOST,
      method: 'DELETE',
      url: `https://s3.wasabisys.com/truthcasting/${username}?force_delete=true`,
      path: `/truthcasting/${username}?force_delete=true`,
      headers: {
        'Content-Type': 'application/json',
      },
      service: 's3',
      region: 'us-east-1',
      maxContentLength: Infinity,
      maxBodyLength: Infinity,
    }

    let signedRequest = aws4.sign(request, {
      accessKeyId: process.env.CDN_KEY,
      secretAccessKey: process.env.CDN_SECRET,
    })

    //delete the Host and Content-Length headers
    delete signedRequest.headers.Host
    delete signedRequest.headers['Content-Length']

    const response = await axios(signedRequest)
    console.log(response.data)
    console.log('successfully deleted user bucket', response.status)

    return res.status(200).json({
      message: `Successfully deleted user bucket`,
    })
  } catch (error) {
    console.log(error)

    return res.status(500).json({
      message: `Problem with deleting user bucket`,
    })
  }
})

export default deleteUserBucket

当我在POSTMAN中将http DELETE请求发送到{{dev}api/admin/deleteuserbucket时,它会给我一个204 ok的响应,这就是响应.

{
    "message": "Successfully deleted user bucket"
}

然后,我go 我的芥末CDN桶判断它是否被删除,在这种情况下,它是goodstock,它仍然存在.感觉我错过了一些愚蠢的事情.

推荐答案

因此,要删除根bucket中的内容,需要将其指向完整的对象.也就是说,我在原始post代码中设置它的方式是返回204("这是Wasabi API的预期结果"),并且没有删除任何内容,因为我没有将它指向完整的对象路径.我还发现,如果你想批量删除而不是删除一个文件,你可以使用aws sdk node 包对你的对象执行get请求,然后使用该响应循环对象并删除你需要的内容..下面是一个例子.希望这能在不久的将来帮助别人.

import expressAsyncHandler from 'express-async-handler'
import User from '../../models/User.js'
import axios from 'axios'
import aws4 from 'aws4'
import errorHandler from '../../middleware/error.js'
import AWS from 'aws-sdk'

/**
 * @desc:   THIS is going to be a testing function that will be added into admin delete user and all related docs.
 * @route:  DELETE
 * @access: Private - Admin Route for when deleting user will delete the CDN username bucket aswell
 * @goalHere: The goal of this function is to delete the user bucket from CDN. So that if we d
 * @comment: This is a testing function that will be added into deleteVideo.js. Unless we just await this function in deleteVideo.js.
 */

export const deleteUserBucket = expressAsyncHandler(async (req, res, next) => {
  const username = req.body.username
  try {
    // Connection
    // This is how you can use the .aws credentials file to fetch the credentials
    const credentials = new AWS.SharedIniFileCredentials({
      profile: 'wasabi',
    })
    AWS.config.credentials = credentials

    // This is a configuration to directly use a profile from aws credentials file.
    AWS.config.credentials.accessKeyId = process.env.CDN_KEY
    AWS.config.credentials.secretAccessKey = process.env.CDN_SECRET

    // Set the AWS region. us-east-1 is default for IAM calls.
    AWS.config.region = 'us-east-1'

    // Set an endpoint.
    const ep = new AWS.Endpoint('s3.wasabisys.com')

    // Create an S3 client
    const s3 = new AWS.S3({ endpoint: ep })

    // The following example retrieves an object for an S3 bucket.
    // set the details for the bucket and key
    const object_get_params = {
      Bucket: 'truthcasting',
      Prefix: `${username}/`,
      //Key: `cnfishead/videos/4:45:14-PM-5-6-2022-VIDDYOZE-Logo-Drop.mp4`,
      // Key: `cnfishead/images/headshot.04f99695-photo.jpg`,
    }

    // get the object that we just uploaded.
    // get the uploaded test_file
    // s3.getObject(object_get_params, function (err, data) {
    //   if (err) console.log(err, err.stack) // an error occurred
    //   else console.log(data) // successful response
    // })

    // get the object that we just uploaded.
    // get the uploaded test_file
    await s3.listObjectsV2(object_get_params, (err, data) => {
      if (err) {
        console.log(err)
        return res.status(500).json({
          message: 'Error getting object',
          error: err,
        })
      } else {
        console.log(data)
        //TODO Change this for loop to a async for loop. Like this: for await (const file of data.Contents) { }
        for (let i = 0; i < data.Contents.length; i++) {
          const object_delete_params = {
            Bucket: 'truthcasting',
            Key: data.Contents[i].Key,
          }
          s3.deleteObject(object_delete_params, (err, data) => {
            if (err) {
              console.log(err)
              return res.status(500).json({
                message: 'Error deleting object',
                error: err,
              })
            } else {
              console.log(data)
            }
          })
        }
        if (data.IsTruncated) {
          console.log('Truncated')
          getObjectFromBucket(req, res, next)
        }
        //console.log('Not Truncated')
        res.status(200).json({
          message: `Successfully retrieved + deleted ${data.Contents.length} objects`,
          data: data,
        })
      }
    })
  } catch (error) {
    console.log(error)
    errorHandler(error, req, res)
  }
})

export default deleteUserBucket

Javascript相关问答推荐

角色 map 集/spritebook动画,用户输入不停止在键上相位器3

Msgraph用户邀请自定义邮箱模板

为什么我的列表直到下一次提交才更新值/onChange

如何通过使用vanilla JS限制字体大小增加或减少两次来改变字体大小

使用Java脚本导入gltf场景并创建边界框

从页面到应用程序(NextJS):REST.STATUS不是一个函数

如何在Angular拖放组件中同步数组?

第三方包不需要NODE_MODULES文件夹就可以工作吗?

如何在Node.js中排除导出的JS文件

try 使用PM2在AWS ubuntu服务器上运行 node 进程时出错

如何在HTMX提示符中设置默认值?

基于产品ID更新条带产品图像的JavaScript命中错误

Django导入问题,无法导入我的应用程序,但我已在设置中安装了它

自动滚动功能在当前图像左侧显示上一张图像的一部分

如何在FiRestore中的事务中使用getCountFromServer

为什么我看到的是回复,而不是我的文档?

在JavaScript中将Base64转换为JSON

如何在单击链接时设置一个JavaScript变量(以打开弹出窗口的特定部分)

我如何才能阻止我的球数以指数方式添加到我的HTML画布中?

Iterator.Next不是进行socket.end()调用后的函数