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