C# UWP/WinRT AWS S3: Delete File from an S3 Bucket

Back to Index

Demonstrates how to delete a file from an S3 bucket. This example deletes the file /chilkat.ocean/starfishCopy.jpg. A response status code equal to 204 is returned for success (with an empty response body).

Documentation: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectDELETE.html

CURL Command

curl -X DELETE https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg

C# UWP/WinRT Example

Chilkat.Rest rest = new Chilkat.Rest();
bool success;

Chilkat.AuthAws authAws = new Chilkat.AuthAws();
authAws.AccessKey = "AWS_ACCESS_KEY";
authAws.SecretKey = "AWS_SECRET_KEY";
authAws.Region = "us-west-2";
authAws.ServiceName = "s3";
rest.SetAuthAws(authAws);

//  URL: https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg
bool bTls = true;
int port = 443;
bool bAutoReconnect = true;
success = await rest.ConnectAsync("chilkat.ocean.s3.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);
if (success != true) {
    Debug.WriteLine("ConnectFailReason: " + Convert.ToString(rest.ConnectFailReason));
    Debug.WriteLine(rest.LastErrorText);
    return;
}

Chilkat.StringBuilder sbResponseBody = new Chilkat.StringBuilder();
success = await rest.FullRequestNoBodySbAsync("DELETE","/starfishCopy.jpg",sbResponseBody);
if (success != true) {
    Debug.WriteLine(rest.LastErrorText);
    return;
}

int respStatusCode = rest.ResponseStatusCode;
if (respStatusCode >= 400) {
    Debug.WriteLine("Response Status Code = " + Convert.ToString(respStatusCode));
    Debug.WriteLine("Response Header:");
    Debug.WriteLine(rest.ResponseHeader);
    Debug.WriteLine("Response Body:");
    Debug.WriteLine(sbResponseBody.GetAsString());
    return;
}