【问题标题】:node js Get Zip From a URL and upload to Google drivenode js 从 URL 获取 Zip 并上传到 Google 驱动器
【发布时间】:2019-08-05 20:55:39
【问题描述】:

我正在尝试从 url 获取 zip 文件,以便在下一步将其上传到 Google 驱动器。但是我的代码不起作用。

// The method to get the zip File from the url

function getFile(){

var file = request({
  method : "GET",
  url : "https://start.spring.io/starter.zip",
  encoding: null // <- this one is important !
}, function (error, response, body) {
  if(error ||  response.statusCode !== 200) {
    // handle error
    return;
  }
  JSZip.loadAsync(body).then(function (zip) {
    return zip.file("content.txt").async("string");
  }).then(function () {
    console.log(text);
  });
});
}

// method to upload zip to drive 

function uploadFile(auth) {
    const drive = google.drive({ version: 'v3', auth });
    var fileMetadata = {
        'name': 'demo.zip'
    };
    var media = {
        mimeType: 'application/zip',
        body: fs.createReadStream(getFile())
    };
    drive.files.create({
        resource: fileMetadata,
        media: media,
        fields: 'id'
    }, function (err, res) {
        if (err) {
            // Handle error
            console.log(err);
        } else {
            console.log('File Id: ', res.data.id);
        }
    });
}

我想从上面的 url 中获取 zip 文件,但是它抛出了一个异常:

new ERR_INVALID_ARG_TYPE(propName, ['string', 'Buffer', 'URL'], path);

当我将正文:fs.createReadStream(getFile()) 更改为正文:fs.createReadStream("https://start.spring.io/starter.zip")

the Exception is: no such file or directory, open 'https://start.spring.io/starter.zip

【问题讨论】:

    标签: node.js url request google-drive-api


    【解决方案1】:
    • 您想从 URL 下载一个 zip 文件并将其上传到您的 Google 云端硬盘。
    • 您希望使用带有 Node.js 的 googleapis 来实现此目的。
    • 您已经能够使用 Drive API 获取和放置文件。

    如果我的理解是正确的,那么这个修改呢?

    修改点:

    • request 从 URL 检索到的值可用于上传。
    • 从上传的 URL 中检索到的值是缓冲区。所以请将其转换为 readstream 类型。

    当以上几点反映到你的脚本中时,它变成如下。

    修改脚本:

    function getFile() {
      return new Promise(function(resolve, reject) {
        request(
          {
            method: "GET",
            url: "https://start.spring.io/starter.zip",
            encoding: null // <- this one is important !
          },
          function(error, response, body) {
            if (error && response.statusCode != 200) {
              reject(error);
              return;
            }
            resolve(body);
          }
        );
      });
    }
    
    function uploadFile(auth) {
      const stream = require("stream"); // In this script, use this module.
    
      getFile().then(body => {
        const bs = new stream.PassThrough();
        bs.end(body);
        const drive = google.drive({ version: "v3", auth });
        var fileMetadata = {
          name: "demo.zip"
        };
        var media = {
          mimeType: "application/zip",
          body: bs // Modified
        };
        drive.files.create(
          {
            resource: fileMetadata,
            media: media,
            fields: "id"
          },
          function(err, res) {
            if (err) {
              // Handle error
              console.log(err);
            } else {
              console.log("File Id: ", res.data.id);
            }
          }
        );
      });
    }
    

    其他模式:

    当然uploadFile()也可以修改如下。

    async function uploadFile(auth) {
      const stream = require("stream"); // In this script, use this module.
    
      const buffer = await getFile();
      const bs = new stream.PassThrough();
      bs.end(buffer);
    
      const drive = google.drive({ version: "v3", auth });
      var fileMetadata = {
        name: "demo.zip"
      };
      var media = {
        mimeType: "application/zip",
        body: bs // Modified
      };
      drive.files.create(
        {
          resource: fileMetadata,
          media: media,
          fields: "id"
        },
        function(err, res) {
          if (err) {
            // Handle error
            console.log(err);
          } else {
            console.log("File Id: ", res.data.id);
          }
        }
      );
    }
    

    参考资料:

    在我的环境中,我可以确认这个修改后的脚本有效。但是,如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

    【讨论】:

    • 它正在工作!非常感谢您的详细回复!
    • @Klaus Kledan 感谢您的回复。很高兴您的问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 2014-06-25
    • 2021-12-26
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多