I'm having some minor problems uploading a base64 image to Google Cloud Storage in Golang. I can upload, and it works, but the client must send the image without any extra data, like the example below:
Don't work
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAABGdBTUEAALGPC/xhBQAAAClJREFUCB1jnD59+n8GLIAFJObo6IgitX//fgYmFBEkDukSYDtAZqIDAAh8CBGQqUSHAAAAAElFTkSuQmCC
Works
iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAABGdBTUEAALGPC/xhBQAAAClJREFUCB1jnD59+n8GLIAFJObo6IgitX//fgYmFBEkDukSYDtAZqIDAAh8CBGQqUSHAAAAAElFTkSuQmCC
Is there a way to upload the whole base64 string, including the metadata? Or should I always remove the beginning of the string?
Code below:
func UploadProduct(ctx context.Context, product *Product) error {
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(product.PhotoURL))
bucketName, err := file.DefaultBucketName(ctx)
if err != nil {
return err
}
client, err := storage.NewClient(ctx)
if err != nil {
return err
}
defer client.Close()
bucket := client.Bucket(bucketName)
name := product.Name + strconv.Itoa(int(product.Volume)) + ".png"
w := bucket.Object("products/" + name).NewWriter(ctx)
w.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}}
w.CacheControl = "public, max-age=86400"
if _, err := io.Copy(w, reader); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
url := "https://storage.googleapis.com/" + bucketName + "/products/" + name
product.PhotoURL = url
return nil
}
Thanks in advance!