Google Apps脚本从二进制文件获取字节范围 [英] Google apps script get range of bytes from binary file

查看:131
本文介绍了Google Apps脚本从二进制文件获取字节范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 Google云端硬盘API 您可以请求在一定范围内获取文件的一部分:Range: bytes=500-999,但是我正在查看

I'm aware that in the Google Drive API you can request to get a portion of a file within a certain range: Range: bytes=500-999, but I was looking in the Google Apps Script reference and I couldn't seem to find the equivalent function in there.

有人有什么想法吗?

推荐答案

  • 您要检索文件的部分正文.
  • 您想使用Google Apps脚本实现这一目标.
  • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    为了实现部分下载,请在请求标头中使用Range: bytes=500-999.

    In order to achieve the partial download, please use Range: bytes=500-999 at the request headers.

    作为一个简单的示例脚本,下面的脚本如何?在此脚本中,使用Drive API为文件ID的文件运行了从100字节到200字节的部分下载.

    As a simple sample script, how about the following script? In this script, the partial download from 100 bytes to 200 bytes is run for a file of file ID using Drive API.

    var fileId = "###";  // Please set the file ID.
    
    var start = 100;
    var end = 199;
    var url = "https://www.googleapis.com/drive/v3/files/" + fileId + "?alt=media";
    var params = {
      method: "get",
      headers: {
        Authorization: "Bearer " + ScriptApp.getOAuthToken(),
        Range: "bytes=" + start + "-" + end,
      },
    };
    var bytes = UrlFetchApp.fetch(url, params).getContent();
    Logger.log(bytes.length)
    

    • 运行脚本时,可以在日志中看到100.
    • 重要的一点是,第一个字节的范围是0.
    • 另一个要点是,在Google Apps脚本中,blob的最大大小为50 MB(52,428,800字节).所以也请注意这一点.
      • When you run the script, 100 can be seen at the log.
      • As one important point, the range of the 1st byte is 0.
      • As another important point, at Google Apps Script, the maximum size of the blob is 50 MB (52,428,800 bytes). So also please be careful this.
        • 作为另一个示例脚本,如何?
        • Partial download
        • Class UrlFetchApp

        如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

        If I misunderstood your question and this was not the direction you want, I apologize.

        当我测试字节数组的最大大小时,我会注意到规范已更改.因此,我将其添加为更新的规范.

        When I tested the maximum size of the byte array, I could notice that the specification had been changed. So I add it as the updated specification.

        • 之前:最大Blob大小为52,428,800字节.准确地说,当将超过50 MB的Blob转换为字节数组时,就会发生错误.这样,当将50 MB的字节数组添加到50 MB的字节数组时,会出现与最大大小有关的错误.这是我之前测量过的实验结果.

        • Before: The maximum blob size is 52,428,800 bytes. Accurately, when the blob more than 50 MB is converted to the byte array, an error occurs. By this, when the byte array of 50 MB is added to the byte array of 50 MB, an error related to the maximum size occurs. This was my experimental result I measured before.

        现在:现在,当我再次测试这种情况时,尽管无法将52,428,801字节的文件作为字节数组检索(这是相同的规范.最大blob大小为52,428,800字节),我注意到52,428,800字节的字节数组必须能够添加到52,428,800字节的字节数组中.这样,我可以确认可以创建104,857,600的字节数组.

        Now: Now, when I tested this situation again, although the file of 52,428,801 bytes cannot be retrieved as the byte array (this is the same specification. The maximum blob size is 52,428,800 bytes.), I noticed that the byte array of 52,428,800 bytes got to be able to be added to the byte array of 52,428,800 bytes. By this, I could confirm that the byte array of 104,857,600 could be created.

        这篇关于Google Apps脚本从二进制文件获取字节范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆