Skip to content

Instantly share code, notes, and snippets.

@rms1000watt
Created August 2, 2018 15:11
Show Gist options
  • Save rms1000watt/17b7e3eb20803ba57c55761739c89f8e to your computer and use it in GitHub Desktop.
Save rms1000watt/17b7e3eb20803ba57c55761739c89f8e to your computer and use it in GitHub Desktop.
Converting AWS Error code, checking if match
// https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/handling-errors.html
svc := s3.New(sess)
resp, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(os.Args[1]),
Key: aws.String(os.Args[2]),
})
if err != nil {
// Casting to the awserr.Error type will allow you to inspect the error
// code returned by the service in code. The error code can be used
// to switch on context specific functionality. In this case a context
// specific error message is printed to the user based on the bucket
// and key existing.
//
// For information on other S3 API error codes see:
// http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeNoSuchBucket:
exitErrorf("bucket %s does not exist", os.Args[1])
case s3.ErrCodeNoSuchKey:
exitErrorf("object with key %s does not exist in bucket %s", os.Args[2], os.Args[1])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment