s3.deleteObject(testBucketName, testFileKey) not working

I’m using the https://jslib.k6.io/aws/0.7.1/aws.js library and wanted to delete the s3 object. However, to my surprise there is no error and the object is not deleted from the s3.

Can someone help me to troubleshoot it?

Here is the snippet:

const bucketName = 'bucket1';
    const awsConfig = new AWSConfig({
        region: 'us-east-1',
        accessKeyId: 'abcd',
        secretAccessKey: 'efgh'
    });
    const s3 = new S3Client(awsConfig);
    Key = `folder1/folder2/${test_id}`;
    s3.deleteObject(bucketName, Key);

Hi @suyashnatural

The delete example in the docs works for me. I’ve also tried a slightly modified example based on yours and it seems to work as well:

import exec from 'k6/execution';
import { AWSConfig, S3Client } from 'https://jslib.k6.io/aws/0.7.1/s3.js';
const bucketName = 'test-jslib-aws';
const awsConfig = new AWSConfig({
    region: 'us-east-1',
    accessKeyId: 'abcd',
    secretAccessKey: 'efgh'
});
export default function () {
    const s3 = new S3Client(awsConfig);
    const key = `folder1/folder2/bonjour.txt`;
    s3.deleteObject(bucketName, key);
   //Make sure it was indeed deleted
   const objects = s3.listObjects();
   if (objects.filter((o) => o.name === testBucketName).length != 0) {
     exec.test.abort();
   }
}

If the test does not abort for you (add the //Make sure it was indeed deleted part always), it hints at that the file is there, in the bucket and subfolder, but maybe the accessKey does not have permissions to delete.

You can always test a full cycle, create a file and delete, to check the permissions (maybe you can’t create either). E.g.

const testBucketName = 'test-jslib-aws'
const testFileKey = 'folder1/folder2/bonjour.txt'
const testFile = open('./bonjour.txt', 'r')
export default function () {
    // List the buckets the AWS authentication configuration
    // gives us access to.
    const buckets = s3.listBuckets()

    // If our test bucket does not exist, abort the execution.
    if (buckets.filter((b) => b.name === testBucketName).length == 0) {
        exec.test.abort()
    }

    // Let's upload our test file to the bucket
    s3.putObject(testBucketName, testFileKey, testFile);

    // Let's list the test bucket objects
    const objects = s3.listObjects(testBucketName)

    // And verify it does contain our test object
    if (objects.filter((o) => o.key === testFileKey).length == 0) {
        exec.test.abort()
    }

    // Let's redownload it verify it's correct, and delete it
    const obj = s3.getObject(testBucketName, testFileKey)
    s3.deleteObject(testBucketName, testFileKey)
}

If you want to just use the delete option, check the different parts: does the bucket exist, does the file exist? You can run the s3 getObject example and see if the file is there, before attempting to delete. If it’s not there from the script (but it is in your s3 bucket), it can definitely point out at permissions, or the region is not correct.

I hope this helps you troubleshoot.

Cheers!