【问题标题】:uploading a file to google cloud by converting a file buffer通过转换文件缓冲区将文件上传到谷歌云
【发布时间】:2021-11-02 01:06:35
【问题描述】:

我正在尝试通过接受缓冲区并使用save 函数将文件上传到谷歌云。但是,我的问题是我的文件没有以正确的格式上传到谷歌云。它们被上传了,但是当我尝试下载文件时,它们都是缓冲区的 .txt 文件。

我不太确定我应该在这里做什么。任何帮助将不胜感激!

上传.ts

const upload = async (fileBuffer: any, filePath: string, fileName: string) => {
    const file = bucket.file(path.join(filePath, fileName));
    file.save(fileBuffer, (err: any) => {
        if (err) {
            console.log(err);
        }
    });
}

storage.ts

imageSender: async(req: Request, res: Response) => {
    try {
        const buffer = "";

        await upload(buffer, "folder", "file");

        res.status(200).send("The file was successfully uploaded.");

    } catch (err) {
        res.status(500).send(httpError(500, "Could not upload the file."));
    }
},

【问题讨论】:

    标签: javascript typescript express google-cloud-platform


    【解决方案1】:

    原来我实际上是在接收 base64 字符串而不是缓冲区,所以我只是添加了 const buffer = Buffer.from(base64, 'base64');。之后就完美运行了!

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 2011-10-04
      • 2015-02-28
      相关资源
      最近更新 更多