I want to delete multiple files from digitaloceans spaces but keep getting errors. Environmental variables are all set My current code:
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;
Getting this error
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'
}
Any help would be appreciated
Tried by commenting forcePathStyle: false
but still not deleting files
I also tried to do it using aws-sdk but with no luck
My package.json looks like this
{
"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"
}
}
I just fixed the issue. The issue was not in the code but with the endpoint. For upload, I was using an endpoint which was provided by default from Digitalocean like
https://[spacename].nyc3.digitaloceanspaces.com
.
It worked for uploading file and single deleting files. But not for deleting multiple objects. I had to use this endpoint for deleting https://nyc3.digitaloceanspaces.com
and before the file name I provided a space name like [spacename]/image1.png
. So my code will look like this
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;