Skip to content

Commit f9feb2c

Browse files
authored
fix(gcs): Catch errors from Google API properly (#5301)
1 parent 276bc37 commit f9feb2c

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

pkg/gcs/gcsClient.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ func (cl *gcsClient) UploadFile(ctx context.Context, bucketID string, sourcePath
6363
defer sourceFile.Close()
6464

6565
target := cl.gcs.Bucket(bucketID).Object(targetPath).NewWriter(ctx)
66-
defer target.Close()
67-
6866
task := func(ctx context.Context) error {
6967
if _, err = io.Copy(target, sourceFile); err != nil {
7068
return fmt.Errorf("upload failed: %w", err)
7169
}
72-
return nil
70+
// Google API errors (like 401, 403 and etc) are returned by Close() method, for some reason.
71+
return target.Close()
7372
}
7473

7574
return retryWithLogging(ctx, log.Entry(), task, initialBackoff, maxRetries, retryMultiplier)
@@ -90,13 +89,14 @@ func (cl *gcsClient) DownloadFile(ctx context.Context, bucketID, sourcePath, tar
9089
if err != nil {
9190
return fmt.Errorf("failed to open source file: %w", err)
9291
}
93-
defer source.Close()
9492

9593
task := func(ctx context.Context) error {
9694
if _, err = io.Copy(target, source); err != nil {
9795
return fmt.Errorf("download failed: %w", err)
9896
}
99-
return nil
97+
98+
// Google API errors (like 401, 403 and etc) are returned by Close() method, for some reason.
99+
return source.Close()
100100
}
101101

102102
return retryWithLogging(ctx, log.Entry(), task, initialBackoff, maxRetries, retryMultiplier)

pkg/gcs/retry.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
const (
1010
maxRetries = 5
1111
initialBackoff = 5 * time.Second
12-
maxRetryPeriod = 22 * time.Second
1312
retryMultiplier = 2
1413
)
1514

0 commit comments

Comments
 (0)