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

VB.NET Example

Dim rest As New Chilkat.Rest
Dim success As Boolean

Dim authAws As 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
Dim bTls As Boolean = True
Dim port As Integer = 443
Dim bAutoReconnect As Boolean = True
success = rest.Connect("chilkat.ocean.s3.us-west-2.amazonaws.com",port,bTls,bAutoReconnect)
If (success <> True) Then
    Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
    Debug.WriteLine(rest.LastErrorText)
    Exit Sub
End If



Dim sbResponseBody As New Chilkat.StringBuilder
success = rest.FullRequestNoBodySb("DELETE","/starfishCopy.jpg",sbResponseBody)
If (success <> True) Then
    Debug.WriteLine(rest.LastErrorText)
    Exit Sub
End If

Dim respStatusCode As Integer = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    Debug.WriteLine("Response Status Code = " & respStatusCode)
    Debug.WriteLine("Response Header:")
    Debug.WriteLine(rest.ResponseHeader)
    Debug.WriteLine("Response Body:")
    Debug.WriteLine(sbResponseBody.GetAsString())
    Exit Sub
End If