Created
April 13, 2022 06:21
-
-
Save kkdai/9b8a82ccdf1294d8a7ae5f7999fd42f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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