@@ -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 )
0 commit comments