Skip to content

Instantly share code, notes, and snippets.

@joonvena
Created November 5, 2022 03:57
Show Gist options
  • Save joonvena/c510ba8deb99c584467f38affa0fccc3 to your computer and use it in GitHub Desktop.
Save joonvena/c510ba8deb99c584467f38affa0fccc3 to your computer and use it in GitHub Desktop.
func PostArticle(c *gin.Context) {
var input Article
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
decodedImage, err := base64.StdEncoding.DecodeString(input.ImageB64)
if err != nil {
panic(err)
}
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
fmt.Println(err.Error())
}
defer client.Close()
buf := bytes.NewBuffer(decodedImage)
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()
wc := client.Bucket("original-images-4646").Object(input.Title).NewWriter(ctx)
if _, err = io.Copy(wc, buf); err != nil {
fmt.Println(err.Error())
}
// Data can continue to be added to the file until the writer is closed.
if err := wc.Close(); err != nil {
fmt.Println(err.Error())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment