skip to Main Content

Whenever I try to delete an object with the Javascript SDK, I get an error saying access denied. However, I can upload files correctly. Can someone help me with this?

My policies for the bucket:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:DeleteObject"
            ],
            "Resource": "arn:aws:s3:::twitter-clone-image-storage-zrs/*"
        }
    ]
}

My function for deleting files:

const s3 = new S3({
  region,
  accessKeyId,
  secretAccessKey,
});

export const deleteFile = (picture_key) => {
  const params = {
    Bucket: bucketName,
    Key: picture_key,
  };

  return s3.deleteObject(params).promise();
};

2

Answers


  1. You can try below code, if npm aws module properly installed,

    const { S3 } = require('aws-sdk');
             
    // Set up AWS credentials and configure the S3 client
    const s3 = new S3({
      accessKeyId: 'your-access-key-id',
      secretAccessKey: 'your-secret-access-key',
      region: 'your-aws-region',
    });
             
    // Specify the bucket name and the key
    const bucketName = 'your-bucket';
    const objectKey = 'path/to/your/sample.file';
              
    const deleteParams = {
      Bucket: bucketName,
      Key: objectKey,
    };
                 
    s3.deleteObject(deleteParams, (err, data) => {
      if (err) {
        console.error('Error deleting object:', err);
      } else {
        console.log('Object deleted successfully:', data);
      }
    });
    
    Login or Signup to reply.
  2. Then, you can use the following code to delete an object from an S3 bucket:

    const AWS = require('aws-sdk');
    
    // Configure the region
    AWS.config.update({region: 'REGION'});
    
    // Create an S3 client
    const s3 = new AWS.S3();
    
    // Define the bucket name and object key
    const bucketName = 'YOUR_BUCKET_NAME';
    const objectKey = 'OBJECT_KEY';
    
    // Define the parameters for the deleteObject function
    const params = {
      Bucket: bucketName,
      Key: objectKey
    };
    
    // Call the deleteObject function
    s3.deleteObject(params, function(err, data) {
      if (err) {
        console.log('Error deleting object: ', err);
      } else {
        console.log('Successfully deleted object: ', data);
      }
    });
    

    Replace ‘REGION’ with the region where your S3 bucket is located, and replace ‘YOUR_BUCKET_NAME’ and ‘OBJECT_KEY’ with the name of your bucket and the key of the object you want to delete, respectively.

    Make sure that the IAM role or user associated with your AWS credentials has the necessary permissions to delete objects from the S3 bucket.

    If you want to delete all objects in a bucket, you can use the following code:

    const AWS = require('aws-sdk');
    
    // Configure the region
    AWS.config.update({region: 'REGION'});
    
    // Create an S3 client
    const s3 = new AWS.S3();
    
    // Define the bucket name
    const bucketName = 'YOUR_BUCKET_NAME';
    
    // Define the parameters for the listObjectsV2 function
    const params = {
      Bucket: bucketName
    };
    
    // Initialize a list to store the keys of all objects in the bucket
    const objectsToDelete = [];
    
    // Call the listObjectsV2 function to get a list of all objects in the bucket
    s3.listObjectsV2(params, function(err, data) {
      if (err) {
        console.log('Error listing objects: ', err);
      } else {
        // Add the keys of all objects to the objectsToDelete list
        data.Contents.forEach(function(object) {
          objectsToDelete.push({
            Key: object.Key
          });
        });
    
        // Define the parameters for the deleteObjects function
        const deleteParams = {
          Bucket: bucketName,
          Delete: {
            Objects: objectsToDelete
          }
        };
    
        // Call the deleteObjects function to delete all objects in the bucket
        s3.deleteObjects(deleteParams, function(err, data) {
          if (err) {
            console.log('Error deleting objects: ', err);
          } else {
            console.log('Successfully deleted objects: ', data);
          }
        });
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search