首页 > 解决方案 > 如何使用 http api 从 javascript 中删除 AWS S3 中的密钥

问题描述

当我尝试从我的 AWS S3 中删除密钥时,我似乎无法克服该"403 (Forbidden)"错误或后续错误。<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>...</RequestId><HostId>...</HostId></Error>为了进行测试,我已经公开了我的存储桶,并为 DELETE 添加了 CORS 规则,并允许对目录中的所有文件使用 s3:DeleteObject,如下所示 "Resource": "arn:aws:s3:::...(directory)/*

到目前为止,这是我在代码中尝试过的:

function deleteS3file(bucket, path, filename) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
         if (xmlhttp.readyState == 4) {
         if (Math.floor(xmlhttp.status/100) != 2){
             alert(filename +" not deleted from S3\n"+xmlhttp.status+" "+xmlhttp.responseText);
             // http://mysourceip.com/
         } else {
            console.log(filename +" deleted from S3");
         }
         }
    }
    xmlhttp.open("DELETE", bucket, true);
    xmlhttp.send(path+'/'+filename);
}

(这只是给了我 403 错误)

function deleteS3file(bucket, path, filename) { // path without leading slash
    var fd = new FormData();
    fd.append('key', path+filename); // full path to file
    fd.append('acl', 'bucket-owner-full-control');

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
         if (xmlhttp.readyState == 4) {
         if (Math.floor(xmlhttp.status/100) != 2){
             alert(filename +" not deleted from S3\n"+xmlhttp.status+" "+xmlhttp.responseText);
             // http://mysourceip.com/
         } else {
            console.log(filename +" deleted from S3");
         }
         }
    }
    xmlhttp.open("DELETE", bucket, true);
    xmlhttp.send(fd);
}

(这给了我 403 和 AWS Access Denied 错误)

我知道我的存储桶配置是正确的,因为我可以毫无问题地上传文件。AWS 文档似乎指向我的第一个解决方案,但我什至没有收到 AWS 回复。

标签: javascriptamazon-web-servicesapiamazon-s3

解决方案


推荐阅读