0

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.

Does anyone have any ideas?

0

1 Answer 1

5
  • You want to retrieve the partial body of the file.
  • You want to achieve this using Google Apps Script.

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

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

Sample script:

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

Note:

  • As one more sample script, how about this?

References:

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

Updated at August 19:

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.

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

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

Sign up to request clarification or add additional context in comments.

19 Comments

Thanks, ingenius, exactly what I was looking for! ( realized afterwards can just make the http request from within the google apps script). Just a quick note about the script you referenced: I noticed it involved storing a large file in a google spreadsheet. I may be interested in doing this to make a database, I would need to be able to #1 write chunks at a particular index and #2 read chunks at a particular index, do you know if this can be done with google spreadsheets? Meaning can I write, say, 4kb of data, at chunk position #14 (for example), and read a chunk at a chunk index?
the maximum size of the blob is 50 MB (52,428,800 bytes). The maximum fetch size is 50 OR the maximum blob size is 50? Can chunks of 50MB be downloaded and joint to create a big blob(say, 100MB)?
@TheMaster In this case, 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.
@TheMaster But now, when I tested this situation, although the file of 52,428,801 bytes cannot be retrieved as the byte array, 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. It was found that the specification of Google Apps Script had been changed.
For reference, I came from this: stackoverflow.com/questions/63443643
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.