Skip to content

Commit

Permalink
Improve error messages from failed S3 operations
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Aug 23, 2024
1 parent a7dacc7 commit 3e46ea8
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions archives/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,11 @@ func GetS3FileInfo(ctx context.Context, s3Client *s3x.Service, fileURL string) (
}

bucket := strings.Split(u.Host, ".")[0]
path := strings.TrimPrefix(u.Path, "/")

head, err := s3Client.Client.HeadObject(
ctx,
&s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(path),
},
)
key := strings.TrimPrefix(u.Path, "/")

head, err := s3Client.Client.HeadObject(ctx, &s3.HeadObjectInput{Bucket: aws.String(bucket), Key: aws.String(key)})
if err != nil {
return 0, "", err
return 0, "", fmt.Errorf("error looking up S3 object bucket=%s key=%s: %w", bucket, key, err)
}

if head.ContentLength == nil || head.ETag == nil {
Expand All @@ -150,19 +143,15 @@ func GetS3File(ctx context.Context, s3Client *s3x.Service, fileURL string) (io.R
}

bucket := strings.Split(u.Host, ".")[0]
path := strings.TrimPrefix(u.Path, "/")
key := strings.TrimPrefix(u.Path, "/")

output, err := s3Client.Client.GetObject(
ctx,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(path),
},
&s3.GetObjectInput{Bucket: aws.String(bucket), Key: aws.String(key)},
withAcceptEncoding("gzip"),
)

if err != nil {
return nil, err
return nil, fmt.Errorf("error fetching S3 object bucket=%s key=%s: %w", bucket, key, err)
}

return output.Body, nil
Expand Down

0 comments on commit 3e46ea8

Please sign in to comment.