Skip to content

Instantly share code, notes, and snippets.

@kkdai
Created April 13, 2022 06:21
Show Gist options
  • Save kkdai/9b8a82ccdf1294d8a7ae5f7999fd42f4 to your computer and use it in GitHub Desktop.
Save kkdai/9b8a82ccdf1294d8a7ae5f7999fd42f4 to your computer and use it in GitHub Desktop.
var cl *storage.Client
func UploadFileToGCS(file io.ReadCloser, object string) error {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()
// Upload an object with storage.Writer.
wc := cl.Bucket(c.bucketName).Object(object).NewWriter(ctx)
if _, err := io.Copy(wc, file); err != nil {
return fmt.Errorf("io.Copy: %v", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("Writer.Close: %v", err)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment