使用 aws-sdk/client-s3 从 digitalocean 空间删除多个文件

问题描述 投票:0回答:1

我想从 digitaloceans 空间中删除多个文件,但不断收到错误。环境变量全部设置完毕 我当前的代码:

const { S3Client, DeleteObjectsCommand } = require("@aws-sdk/client-s3");
require("dotenv").config();

const s3 = new S3Client({
  region: "sfo3", 
  credentials: {
    accessKeyId: process.env.DO_SPACES_KEY, 
    secretAccessKey: process.env.DO_SPACES_SECRET, 
  },
  endpoint: process.env.DO_SPACES_ENDPOINT,
  forcePathStyle: true, // Required for some S3-compatible services
});

const deleteFile = async (keys) => {
  try {
    const keys = [
      "1702039720121672541410-file_example_JPG_2500kB.jpg",
      "1702034408592871178538-file_example_PNG_2100kB.png",
      "1702034501011992084344-file_example_PNG_3MB.png",
    ];
    const keysArray = [];
    keys.map((key) => keysArray.push({ Key: key }));
    console.log(keysArray);
    const deleteParams = {
      Bucket: process.env.DO_SPACES_NAME, 
      Delete: {
        Objects: keysArray,
        // Quiet: false, 
      },
    };

    const command = new DeleteObjectsCommand(deleteParams);
    const data = await s3.send(command).catch((err) => {
      console.log(err);
    });

    console.log(data);
    return data;
  } catch (error) {
    console.error("Error deleting file:", error);
    throw error;
  }
};

module.exports = deleteFile;

出现此错误

NotImplemented: UnknownError
    at throwDefaultError (C:\Users\username\Desktop\skrink-image\backend\node_modules\@smithy\smithy-client\dist-cjs\default-error-handler.js:8:22)
{
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 501,
    requestId: 'tx0000016e999caa939aed5-00657a18e7-3c6e5030-sfo3a',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Code: 'NotImplemented',
  RequestId: 'tx0000016e999caa939aed5-00657a18e7-3c6e5030-sfo3a',
  HostId: '3c6e5030-sfo3a-sfo3-zg01'
}

如有任何帮助,我们将不胜感激 尝试通过评论

forcePathStyle: false
但仍然无法删除文件 我也尝试使用 aws-sdk 来做到这一点,但没有运气

我的 package.json 看起来像这样

{
  "name": "backend",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon server.js",
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@aws-sdk/client-s3": "^3.472.0",
    "@img/sharp-win32-x64": "^0.33.0",
    "aws-sdk": "^2.1515.0",
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.3.1",
    "express": "^4.18.2",
    "fs-extra": "^11.2.0",
    "joi": "^17.11.0",
    "multer": "^1.4.5-lts.1",
    "mysql2": "^3.6.5",
    "node-cron": "^3.0.3",
    "nodemon": "^3.0.2",
    "sequelize": "^6.35.1",
    "sharp": "^0.33.0"
  },
  "devDependencies": {
    "sequelize-cli": "^6.6.2"
  }
}
node.js aws-sdk digital-ocean aws-sdk-js digital-ocean-spaces
1个回答
0
投票

我刚刚解决了这个问题。问题不在于代码,而在于端点。对于上传,我使用的是 Digitalocean 默认提供的端点,例如

https://[spacename].nyc3.digitaloceanspaces.com

它适用于上传文件和单个删除文件。但不适用于删除多个对象。我必须使用此端点来删除

https://nyc3.digitaloceanspaces.com
,并且在文件名之前我提供了一个空间名称,如
[spacename]/image1.png
。所以我的代码看起来像这样

const { S3Client, DeleteObjectsCommand } = require("@aws-sdk/client-s3");
require("dotenv").config();

const s3 = new S3Client({
  region: "sfo3", 
  credentials: {
    accessKeyId: process.env.DO_SPACES_KEY, 
    secretAccessKey: process.env.DO_SPACES_SECRET, 
  },
  endpoint: process.env.DO_SPACES_ENDPOINT, // similar to this https://nyc3.digitaloceanspaces.com
  forcePathStyle: true, // Required for some S3-compatible services
});

const deleteFile = async (keys) => {
  try {
    const keys = [
      "[spacename]/1702039720121672541410-file_example_JPG_2500kB.jpg",
      "[spacename]/1702034408592871178538-file_example_PNG_2100kB.png",
      "[spacename]/1702034501011992084344-file_example_PNG_3MB.png",
    ];
    const keysArray = [];
    keys.map((key) => keysArray.push({ Key: key }));
    console.log(keysArray);
    const deleteParams = {
      Bucket: process.env.DO_SPACES_NAME, 
      Delete: {
        Objects: keysArray,
        // Quiet: false, 
      },
    };

    const command = new DeleteObjectsCommand(deleteParams);
    const data = await s3.send(command).catch((err) => {
      console.log(err);
    });

    console.log(data);
    return data;
  } catch (error) {
    console.error("Error deleting file:", error);
    throw error;
  }
};

module.exports = deleteFile;
© www.soinside.com 2019 - 2024. All rights reserved.