首页 > 解决方案 > 从 S3 中删除存储桶时的随机行为

问题描述

我正在编写与Aws s3交互的代码,以执行各种操作,如创建存储桶、删除存储桶、上传和下载文件等。尝试删除存储桶时出现问题;拒绝访问

目前,我正在使用Root 用户凭据来创建和删除存储桶。未启用版本控制,并且无法在 AWS 控制台中看到附加到此存储桶的任何存储桶策略。

它表现出奇怪的行为;有时在尝试删除空存储桶时会出现访问被拒绝错误,有时它会毫不费力地被删除。

我可以毫无问题地通过 AWs s3 控制台删除存储桶。它只是通过它随机行为的代码。

请有人解释一下;可能是什么原因?

这是我的代码

public string DeleteBucket(string bucketName, string S3Region)
{
    string sts = "";
   
    Chilkat.Http http = new Chilkat.Http();

    // Insert your access key here:
    http.AwsAccessKey = "AccessKey";
    http.AwsSecretKey = "SecretKey";  //root user
    http.AwsRegion = S3Region;
    

    bool success = http.S3_DeleteBucket(bucketName);
    
    if (success != true)
    {
        
        return sts = "{\"Status\":\"Failed\",\"Message\":\""http.lastErrorText"\"}";
    }
    else
    {
        return sts = "{\"Status\":\"Success\",\"Message\":\"Bucket deleted!\"}";
    }
}

标签: asp.netamazon-s3c#-4.0chilkat

解决方案


您应该检查 HTTP 响应正文以查看来自 AWS 的错误消息。例如:

http.KeepResponseBody = true;

bool success = http.S3_DeleteBucket(bucketName);

if (success != true) {
    Debug.WriteLine(http.LastErrorText);
    // Also examine the error response body from AWS:
    Debug.WriteLine(http.LastResponseBody);
}
else {
    Debug.WriteLine("Bucket created.");
}

推荐阅读