将 google 个云存储文件转换为 base64 字符串

Convert google cloud storage file to base64 string

我正在从 google 云存储中检索 pdf 文件。我需要将此文件转换为 base64 字符串,以便我可以传递给 api,因为 request.This 是 nodejs

const { Storage } = require("@google-cloud/storage");
const storage = new Storage(options);
const bucket = storage.bucket(bucketName);   
let remoteFile = bucket.file(fileName);

需要将此 remoteFile 对象转换为 base64 字符串。 实际上我需要将此 remoteFile 作为附件传递给 sendgrid 邮件 api.

正如你在library sample中找到的,你需要先下载文件内容,然后你可以做你想做的,如果你想用base64编码

....

remoteFile.download().then(function(data) {
  const file = data[0];
  ... convert base64 and continue here....
});