首页 > 解决方案 > 从图像 url 将图像上传到谷歌驱动器

问题描述

嗨,我正在用节点制作刮刀

我不想在服务器中保存任何文件,所以我正在尝试从 url 上传图像

我正在使用 axios 来获取图像并使用 googleapis 来上传它。

这是我的代码:

const oauth2client = new google.auth.OAuth2(
    config.CLIENT_ID, 
    config.CLIENT_SECRET,
    config.REDIRECT_URI
)
oauth2client.setCredentials({'refresh_token': config.REFRESH_TOKEN})
const drive = google.drive({
    version: 'v3',
    auth: oauth2client
})
async function UploadImage(){
    var image = await axios
    .get('https://www.somepage.com/image.jpg', {
      responseType: 'stream'
    }).then(response => response.data)

     var response = await drive.files.create({
         requestBody: {
             name: 'image.jpg',
             mimeType: 'image/jpeg',
         },
         media: {
             mimeType: 'image/jpeg',
             body: image
         }
    });
}

无论如何要从 axios 流响应创建 readerStrem 吗?

标签: node.jsaxiosgoogle-drive-api

解决方案


推荐阅读