【问题标题】:Google cloud storage uploaded via node, image is broken通过节点上传的谷歌云存储,图像损坏
【发布时间】:2018-08-18 21:01:35
【问题描述】:

使用节点库上传的图像似乎已损坏,尽管它们报告了正确的大小。

报告了关于使用Java/PHP的类似问题

    let fileName: string = `public/static/${data["name"]}`;
    let file: Storage.File = this.bucket.file(fileName);

    var fileBuffer = Buffer.from(data["source"], 'base64');

    return from(file.save(fileBuffer, { contentType: data["contentType"], public: data["isPublic"] }))
        .pipe(map(() => {
            return { source: `${this.publicURLPrefix}/${fileName}` }
        }));

通过以上代码,图片文件上传成功,代码将上传内容进行base64转换,写入指定文件。但是,在浏览到公共 URL 时,图像似乎已损坏/损坏。手动上传的图片按预期工作。

【问题讨论】:

    标签: google-app-engine google-cloud-storage


    【解决方案1】:

    发布了here关于节点存储损坏图像的疲倦解决方案,并且似乎需要删除base64编码图像中的元信息。下面更新了工作代码

        let fileName: string = `${namespace}/${data["name"]}`;
        let file: Storage.File = this.bucket.file(fileName);
    
        //add acl, who has access to this file
        console.debug("creating/modifying file", fileName);
    
        let base64Source = (data["source"] as string).replace(/^data:image\/(png|gif|jpeg);base64,/,'');
        let fileBuffer = new Buffer(base64Source,'base64');
    
        return from(file.save(fileBuffer, { contentType: data["contentType"], public: data["isPublic"] }))
            .pipe(map(() => {
                return { source: `${this.publicURLPrefix}/${fileName}` }
            }));
    

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 2020-09-22
      • 2017-12-06
      • 2022-01-09
      • 2015-02-28
      • 1970-01-01
      相关资源
      最近更新 更多