这应该在文档中,但我在任何地方都找不到它。
我正在使用 Go 版本 2 的 aws sdk。当我执行
s3Client.GetObject(...)
并且它返回一个错误时,我需要区分密钥何时不存在和其他错误。错误是 404,但它是 smithyOperation 错误,并且无法检查状态代码。
如何区分 S3 存储桶中不存在密钥的情况和其他错误。这是代码:
out, err := client.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: bucketName,
Key: key,
})
if err != nil {
// Here is where I need to distinguish when the key does not exist from other errors
...
}
应该是服务客户端错误,参见 https://aws.github.io/aws-sdk-go-v2/docs/handling-errors/
您阅读它们时:
import "log"
import "github.com/aws/smithy-go"
// ...
if err != nil {
var oe *smithy.OperationError
if errors.As(err, &oe) {
log.Printf("failed to call service: %s, operation: %s, error: %v", oe.Service(), oe.Operation(), oe.Unwrap())
}
return
}
这应该包含 https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html#API_GetObject_Errors
中定义的 nosuchkey 错误