0

我试图研究这个,但我似乎找不到答案。在 .NET (VB.NET) 中,我有以下代码来删除 Amazon S3 中的对象:

client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey, RegionEndpoint.USEast1)
    Dim request As DeleteObjectRequest = New DeleteObjectRequest
    With request
        .BucketName = bucketName
        .Key = keyToDelete
    End With
    Try
        Dim resp As DeleteObjectResponse = client.DeleteObject(request)
        Dim headers As WebHeaderCollection = resp.Headers
        For Each key As String In headers.Keys
            Return "Response Header: " + key + "Value: " + headers.Get(key)
        Next
    Catch ex As AmazonS3Exception
        Return "Not Deleted"
    End Try
    Return "Deleted"

我想不通的是我怎么知道该对象已成功删除?我以为它会抛出异常,但事实并非如此。我可以传递我想要的任何密钥,它不存在,它仍然可以正常工作。

4

1 回答 1

1

根据这个线程,https://forums.aws.amazon.com/thread.jspa?threadID=60081,除非函数抛出异常,否则对象被成功删除。但是,文档http://docs.amazonwebservices.com/sdkfornet/latest/apidocs/html/M_Amazon_S3_AmazonS3_DeleteObject.htm确实确认了您看到的行为,如果对象不存在,它将成功返回。我猜这个理论是删除了一个不存在的对象。

在尝试删除之前,您可能必须检查对象是否存在。该线程https://forums.aws.amazon.com/message.jspa?messageID=219046提供了几种方法。如果对象确实存在,则删除失败应引发异常。

于 2012-11-28T19:39:27.740 回答