简体   繁体   中英

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#.

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. 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.

I have very little experience with Amazon S3 so I don't know what could be going wrong. 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?

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. 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. If you have actual requests sending to Amazon, you can compare them with documentation . Please check AccessKeyId for your deleteRequest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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