谷歌应用程序脚本从二进制文件中获取字节范围

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)

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

问题描述

我知道在 Google Drive API 您可以请求获取某个范围内的文件的一部分:Range: bytes=500-999,但我正在查看 Google Apps 脚本参考,我似乎在其中找不到等效函数.

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 字节).所以也请注意这一点.
      • 作为另一个示例脚本,this怎么样?
      • 部分下载
      • 类 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 字节.准确的说,当超过50MB的blob转换为字节数组时,就会出现错误.这样,当将 50MB 的字节数组添加到 50MB 的字节数组时,就会发生与最大大小有关的错误.这是我之前测量的实验结果.

      • 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.

      这篇关于谷歌应用程序脚本从二进制文件中获取字节范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:谷歌应用程序脚本从二进制文件中获取字节范围

基础教程推荐