.NET Core accessing S3: Access Denied

Viewed 25

I am very new to AWS S3 and have no idea what went wrong in my code.

I'm wanting to develop a backend that takes in a file and uploads it to S3 bucket, but I got Amazon.S3.AmazonS3Exception: Access Denied with the following code

[HttpPost]
public async Task<IActionResult> UploadFile(IFormFile file) {
    var client = new AmazonS3Client(
        "access key id",
        "secret access key",
        "session token",
        RegionEndpoint.APSoutheast2
    );

    using (var memoryStream = new MemoryStream()) {
        file.CopyTo(memoryStream);
        var uploadRequest = new TransferUtilityUploadRequest {
            InputStream = memoryStream,
            Key = file.FileName,
            BucketName = "bucket name",
            ContentType = file.ContentType
        };
        var transferUtility = new TransferUtility(client);
        await transferUtility.UploadAsync(uploadRequest);
    }
    
    return Ok("OK");
}

The access key id, secret access key, and session token were taken from aws CLI using this command

aws sts get-session-token --duration-seconds 129600

I would be extremely grateful if someone can help me out. Thanks in advance!

0 Answers
Related