Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript http get请求后将文件上载到google驱动器_Javascript_Node.js_Api_Google Drive Api - Fatal编程技术网

Javascript http get请求后将文件上载到google驱动器

Javascript http get请求后将文件上载到google驱动器,javascript,node.js,api,google-drive-api,Javascript,Node.js,Api,Google Drive Api,我在单独的文件中有两个函数来分割工作流 const download = function(url){ const file = fs.createWriteStream("./test.png"); const request = https.get(url, function(response) { response.pipe(file); }); } 我的fileHelper.js中的这个函数应该获取一个包含图像的URL,然后将其本地保存到test

我在单独的文件中有两个函数来分割工作流

const download = function(url){
    const file = fs.createWriteStream("./test.png");
    const request = https.get(url, function(response) {
        response.pipe(file);
    });
}
我的
fileHelper.js
中的这个函数应该获取一个包含图像的URL,然后将其本地保存到
test.png

function uploadFile(filePath) {
    fs.readFile('credentials.json', (err, content) => {
        if (err) return console.log('Error loading client secret file:', err);
        // Authorize a client with credentials, then call the Google Drive API.
        authorize(JSON.parse(content), function (auth) {
            const drive = google.drive({version: 'v3', auth});
            const fileMetadata = {
            'name': 'testphoto.png'
            };
            const media = {
            mimeType: 'image/png',
            body: fs.createReadStream(filePath)
            };
            drive.files.create({
            resource: fileMetadata,
            media: media,
            fields: 'id'
            }, (err, file) => {
            if (err) {
                // Handle error
                console.error(err);
            } else {
                console.log('File Id: ', file.id);
            }
            });
        });
    });
}
我的
googleDriveHelper.js
中的这个函数应该获取调用的文件路径,然后将该流上传到我的google驱动器中。这两个函数各自工作,但https.get似乎是异步工作的,如果我尝试在下载后调用
googleDriveHelper.uploadFile(filePath)
函数,它没有时间上传完整的文件,因此会将一个空白文件上传到我的驱动器

  • 我想找到一种方法,当调用
    fileHelper.download(url)
    时,它会自动上传到我的驱动器中
  • 我也不知道是否有办法直接从
    下载
    函数到
    上传
    函数创建readStream,这样我就不必在本地保存文件来上传它
      我相信你的目标如下

      • 您希望将从URL检索的文件上载到Google Drive
      • 从URL下载文件时,您希望在不创建文件的情况下将其上载到Google Drive
      • 您希望通过Node.js使用googleapis实现这一点
      • 您已经能够使用驱动器API上载文件
      对于这个问题,这个答案如何

      修改点:
      • download
        功能中,检索到的缓冲区转换为流类型,并返回流数据
      • uploadFile
        功能中,检索到的流数据用于上传
      • 从驱动器API的响应值检索文件ID时,请使用
        file.data.ID
        而不是
        file.ID
      通过上述修改,从URL下载的文件可以上传到Google Drive,而无需创建文件

      修改脚本: 修改脚本时,请按以下方式修改

      download()
      uploadFile()
      参考资料:

        • 我相信你的目标如下

          • 您希望将从URL检索的文件上载到Google Drive
          • 从URL下载文件时,您希望在不创建文件的情况下将其上载到Google Drive
          • 您希望通过Node.js使用googleapis实现这一点
          • 您已经能够使用驱动器API上载文件
          对于这个问题,这个答案如何

          修改点:
          • download
            功能中,检索到的缓冲区转换为流类型,并返回流数据
          • uploadFile
            功能中,检索到的流数据用于上传
          • 从驱动器API的响应值检索文件ID时,请使用
            file.data.ID
            而不是
            file.ID
          通过上述修改,从URL下载的文件可以上传到Google Drive,而无需创建文件

          修改脚本: 修改脚本时,请按以下方式修改

          download()
          uploadFile()
          参考资料:
          const download = function (url) {
            return new Promise(function (resolve, reject) {
              request(
                {
                  method: "GET",
                  url: url,
                  encoding: null,
                },
                (err, res, body) => {
                  if (err && res.statusCode != 200) {
                    reject(err);
                    return;
                  }
                  const stream = require("stream");
                  const bs = new stream.PassThrough();
                  bs.end(body);
                  resolve(bs);
                }
              );
            });
          };
          
          function uploadFile(data) { // <--- Modified
            fs.readFile("drive_credentials.json", (err, content) => {
              if (err) return console.log("Error loading client secret file:", err);
              authorize(JSON.parse(content), function (auth) {
                const drive = google.drive({ version: "v3", auth });
                const fileMetadata = {
                  name: "testphoto.png",
                };
                const media = {
                  mimeType: "image/png",
                  body: data, // <--- Modified
                };
                drive.files.create(
                  {
                    resource: fileMetadata,
                    media: media,
                    fields: "id",
                  },
                  (err, file) => {
                    if (err) {
                      console.error(err);
                    } else {
                      console.log("File Id: ", file.data.id); // <--- Modified
                    }
                  }
                );
              });
            });
          }
          
          async function run() {
            const url = "###";
            const data = await fileHelper.download(url);
            googleDriveHelper.uploadFile(data);
          }