简体   繁体   English

使用.Net AWSSDK在Amazon S3中删除文件时获取访问被拒绝异常

[英]Getting Access Denied Exception when deleting a file in Amazon S3 using the .Net AWSSDK

I am trying to do some simple file IO using amazon S3 and C#. 我正在尝试使用亚马逊S3和C#做一些简单的文件IO。

So far I have been able to create files and list them. 到目前为止,我已经能够创建文件并列出它们。 I am the bucket owner and I should have full access. 我是存储桶所有者,应该具有完全访问权限。 In CloudBerry I can create and delete files in the bucket. 在CloudBerry中,我可以创建和删除存储桶中的文件。 In my code when I try to delete a file I get an access denied exception. 在我的代码中,当我尝试删除文件时,出现拒绝访问的异常。

This is my test method: 这是我的测试方法:

[Test]
public void TestThatFilesCanBeCreatedAndDeleted()
{
    const string testFile = "test.txt";

    var awsS3Helper = new AwsS3Helper();

    awsS3Helper.AddFileToBucketRoot(testFile);
    var testList = awsS3Helper.ListItemsInBucketRoot();
    Assert.True(testList.ContainsKey(testFile)); // This test passes
    awsS3Helper.DeleteFileFromBucket(testFile); // Access denied exception here
    testList = awsS3Helper.ListItemsInBucketRoot();
    Assert.False(testList.ContainsKey(testFile)); 

}

My method to add a file: 我添加文件的方法:

var request = new PutObjectRequest();
        request.WithBucketName(bucketName);
        request.WithKey(fileName);
        request.WithContentBody("");
        S3Response response = client.PutObject(request);
        response.Dispose();

My method to delete a file: 我删除文件的方法:

var request = new DeleteObjectRequest()
        {
            BucketName = bucketName,
            Key = fileKey
        };
        S3Response response = client.DeleteObject(request);
        response.Dispose();

After running the code the file is visible in CloudBerry and I can delete it from there. 运行代码后,该文件在CloudBerry中可见,我可以从那里删除它。

I have very little experience with Amazon S3 so I don't know what could be going wrong. 我对Amazon S3的经验很少,所以我不知道可能会出什么问题。 Should I be putting some kind of permissions on to any files I create or upload? 我应该对创建或上传的任何文件赋予某种权限吗? Why would I be able to delete a file while I am logged in to CloudBerry with the same credentials provided to my program? 使用我提供给程序的相同凭据登录CloudBerry时,为什么能删除文件?

I'm not sure what is the source of problem. 我不确定问题的根源是什么。 Possibly security rules, but maybe something very simple with your bucket configuration. 可能是安全规则,但对于您的存储桶配置来说可能非常简单。 You can check them using S3 Organizer Firefox plugin , using AWS management site or any other management tool. 您可以使用S3 Organizer Firefox插件 ,AWS管理站点或任何其他管理工具来检查它们。 Also I recommend request-responce logging - that helped a lot in different investigating for me. 我也建议请求响应日志记录-这对我的其他调查工作大有帮助。 AWSSDK has plenty of good exmamples with logging - so you need only copy-paste them and everything works. AWSSDK具有很多很好的日志记录示例-因此您只需要复制粘贴即可,一切正常。 If you have actual requests sending to Amazon, you can compare them with documentation . 如果您有实际的请求发送到Amazon,则可以将其与文档进行比较。 Please check AccessKeyId for your deleteRequest 请检查AccessKeyId以获取您的deleteRequest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM