dongyao1915 2017-02-20 01:33
浏览 279

Google Cloud Storage图片上传

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!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作