首页 > 解决方案 > 使用 Node.js 将照片从 Google Drive 上传到 AWS S3

问题描述

要求是将照片从公共谷歌驱动器文件夹上传到 s3。

从谷歌驱动器获取图像列表的代码

module.exports.fetchImageDetails = async (fileId) => new Promise(async (res, rej) => 
{
  const token = await googleUtil();
  const drive = google.drive('v2');
  drive.files.get(
    {
      auth: token,
      fileId: `${fileId}`,
    },
    (err, response) => {
      if (err) {
        console.log(`The API returned an error: ${err}`);
        rej(err);
      }
      res(response.data);
    },
  );
});

迭代图像

for (file of fetchedFilesInFolder.files) {
    uploadStatus = await s3Helper.uploadImage(
         process.env.MEDIA_S3_BUCKET,
         url,
         fileDetails.webContentLink
    );
}

上传到 S3: const options = { uri: url, encoding: null, }; 常量主体 = 等待请求(选项);

const uploadResult = await s3
  .upload({
    Bucket: bucket,
    Key: path,
    Body: body,
  })
  .promise();

此代码有时有效,有时无效。很多时候,上传到 S3 上的图像不是有效的图像。

我们真的不确定,发生了什么。

请注意:图像大小约为 6 MB。此外,当我们访问 URL ( https://drive.google.com/uc?id= <file_id>&export=download )时,默认情况下,谷歌驱动器webContentLink 会下载图像。这可能是问题的根本原因吗?此外,从链接中删除&export=download并不奇怪,因为https://drive.google.com/uc?id= <file_id>被重定向到另一个链接。const body = await request(options);

我们已经为此花费了足够的时间,但无法修复它。

标签: node.jsamazon-s3google-drive-api

解决方案


推荐阅读