首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除Wasabi CDN桶204错误未删除

删除Wasabi CDN桶204错误未删除
EN

Stack Overflow用户
提问于 2022-05-04 21:58:32
回答 2查看 217关注 0票数 2

因此,我试图以编程方式从我的一个桶中删除Wasabi CDN对象。我的请求是发回204并显示成功,但是没有任何东西被移动/删除。我使用节点/javascript来完成这个任务。

这是我的函数,应该删除桶。

代码语言:javascript
复制
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请求发送到{{dev}api/admin/deleteuserbucket时,它会给我一个204 ok的响应,这就是响应。

代码语言:javascript
复制
{
    "message": "Successfully deleted user bucket"
}

然后我去我的Wasabi CDN桶检查它是否被删除,在本例中它是goodstock,它仍然在那里。感觉好像我错过了一些愚蠢的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-05-08 02:09:51

因此,要删除根桶中的内容,需要将其指向完整的对象。尽管如此,我在最初的邮政编码中设置它的方式是返回204(“这是预期的Wasabi API"),并且没有删除任何东西,因为我没有将它指向完整的对象路径。我还发现,如果您想要进行批删除而不是删除一个文件,您可以一个一个地使用aws节点包对对象执行get请求,然后使用该响应遍历对象并删除所需的内容。下面是一个例子。希望这能在不久的将来对某人有所帮助。

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2022-05-05 09:56:55

在S3中,删除桶API调用返回204No内容,并在成功删除时返回一个空响应体。

使用该URL,您将对对象(而不是桶)发出删除请求:

代码语言:javascript
复制
URL: `https://s3.wasabisys.com/truthcasting/${username}?force_delete=true`

在这个URL中传递的用户名将被解释为一个键,S3将在桶的根中查找一个对象。

另外,为什么不使用AWS来删除桶,而不是重新实现所有这些。检查AWS博士是否有此问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72119894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档