{"record":{"id":"17bf1b05434e5f4b","repo":"GoogleContainerTools/skaffold","slug":"error-copying-file-to-gcs-w","errorCode":null,"errorMessage":"error copying file to GCS: %w","messagePattern":"error copying file to GCS: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/gcs/client/native.go","lineNumber":346,"sourceCode":"\tdefer reader.Close()\n\n\tfile, err := os.Create(localPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create file: %v\", err)\n\t}\n\tdefer file.Close()\n\n\tif _, err := io.Copy(file, reader); err != nil {\n\t\treturn fmt.Errorf(\"failed to copy object to file: %v\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (nb nativeBucketHandler) UploadObject(ctx context.Context, objName string, content *os.File) error {\n\twc := nb.bucket.Object(objName).NewWriter(ctx)\n\tif _, err := io.Copy(wc, content); err != nil {\n\t\treturn fmt.Errorf(\"error copying file to GCS: %w\", err)\n\t}\n\tif err := wc.Close(); err != nil {\n\t\treturn fmt.Errorf(\"error closing GCS writer: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (nb nativeBucketHandler) Close() {\n\tnb.storageClient.Close()\n}\n","sourceCodeStart":328,"sourceCodeEnd":357,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/gcs/client/native.go#L328-L357","documentation":"UploadObject wraps io.Copy failures while streaming a local file into a GCS object writer. It means the upload data transfer failed — network interruption, context cancellation, or an API error during resumable upload. Note the writer's Close error is reported separately, so this is specifically the copy/streaming phase.","triggerScenarios":"Calling UploadFile (which delegates to UploadObject) when the connection to GCS drops or the context is cancelled while the file is streaming, or the underlying writer encounters an API error mid-upload.","commonSituations":"Uploading large artifacts from CI with unstable networking; context timeout shorter than upload time on slow links; GCS returning 429/5xx mid-stream under heavy load; proxy killing long-lived connections.","solutions":["Retry the upload (GCS resumable uploads are designed to be retried from scratch via a new NewWriter call)","Increase the context deadline to cover the expected upload duration","Check network/proxy stability and firewall rules for storage.googleapis.com","For large files, verify disk read speed and that the source file is not being modified during upload"],"exampleFix":"// before\nctx, cancel := context.WithTimeout(ctx, 5*time.Second)\ndefer cancel()\nif err := n.UploadFile(ctx, bigFile, \"gs://bucket/artifacts/app.tar.gz\"); err != nil { return err } // timeout mid-copy\n// after\nuctx, cancel := context.WithTimeout(ctx, 30*time.Minute)\ndefer cancel()\nerr := n.UploadFile(uctx, bigFile, \"gs://bucket/artifacts/app.tar.gz\")\nif err != nil && isRetryable(err) {\n    err = n.UploadFile(uctx, bigFile, \"gs://bucket/artifacts/app.tar.gz\")\n}","handlingStrategy":"retry","validationCode":"func hasDiskSpaceFor(src string) error {\n\tfi, err := os.Stat(src)\n\tif err != nil { return err }\n\tif fi.Size() > 5<<30 { // large files: pre-check via resumable session is done by the lib\n\t\tlog.Printf(\"uploading large file %s (%d bytes)\", src, fi.Size())\n\t}\n\treturn nil\n}\n// also ensure the context deadline comfortably exceeds expected upload time","typeGuard":null,"tryCatchPattern":"err := n.UploadFile(ctx, src, dst)\nif err != nil && strings.Contains(err.Error(), \"error copying file to GCS\") {\n\tif isRetryable(err) { // 429/5xx/network\n\t\treturn retryBackoff(3, time.Second, func() error {\n\t\t\treturn n.UploadFile(ctx, src, dst)\n\t\t})\n\t}\n\treturn err\n}","preventionTips":["Size the context timeout to the file size and link speed","Retry idempotent uploads on transient/network errors","Keep long-lived connections to storage.googleapis.com unfiltered by proxies","Do not modify the source file while it is being uploaded"],"tags":["gcs","network","io","upload"],"backgroundTag":"upload-failed","analyzedSha":"a1189de023efc32d4b8e11f395acc678aa555011","analyzedAt":"2026-09-05T12:09:27.064Z","contentChangedAt":"2026-09-05T12:09:27.064Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}