Google Apps script error: Incorrect file size during resumable upload with Drive API V3 (missing 2 bytes)

I've created a Google apps script web application that is able to download the data associated with any Youtube channel's videos and save the result in a .CSV file on the user's Google Drive. The code follows these main steps:

  1. First, a GAS function (referred to as "X") is invoked when the user clicks a button in their browser (using google.script.run). This function queries the Youtube API in a loop that stops either when the nextPageToken becomes null or after approximately 330 seconds of execution time (due to the maximum allowed runtime for functions being 6 minutes in GAS). For each video, a CSV string is generated and added to an array ("mainArr").
  2. Once X completes successfully, it returns mainArr and triggers another function called "Y." The purpose of Y is to begin uploading "Utilities.newBlob(mainArr.join('\r\n'))" to the user's Drive in chunks of 256kb each (based on the documentation).

The issue arises when X is executed for shorter durations multiple times, causing Y to be called repeatedly to obtain all the videos. In such cases, the resulting CSV is missing 2 bytes per iteration, leading to discrepancies in the total file size. This discrepancy is problematic as it indicates missing videos within the .CSV file.

To troubleshoot, I have restricted the downloaded fields to only include the videos' IDs to rule out data update issues, such as increasing view counts affecting the file size. Additionally, I have confirmed that the function responsible for creating chunks from mainArr remains intact without corrupting the data.

In terms of the process for calling X and Y multiple times: upon X's completion, Y is triggered to upload mainArr chunk by chunk, excluding the last chunk named "lastChunk." After completing its task, Y clears mainArr (mainArr = []) and returns both mainArr and the base64-encoded lastChunk. Subsequently, X is invoked again, populating mainArr with new data and repeating the cycle of passing mainArr along with the encoded lastChunk for Y to handle.

Here's a snippet from Y outlining part of the described procedure:

var csv = Utilities.newBlob(mainArr.join('\r\n'));
if (lastChunk) {
  csv = Utilities.newBlob((Utilities.base64DecodeWebSafe(lastChunk)).concat(csv.getBytes()));
}
var chunkSize = (256*1024); //following drive documentation,
var fileBytes = csv.getBytes();
var fileSize = fileBytes.length;
var chunksCount = Math.ceil(fileSize / chunkSize);
for (var i = 0; i < chunksCount; i++) {
  var chunk = csv.setBytes(fileBytes.splice(0, chunkSize));
}

Answer №1

During the process, I generated a CSV string for every video and added it to an array. However, I found success by pushing the bytes of the CSV string instead of the actual string itself.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The functionality of aggregation is currently experiencing issues when used in conjunction with the $geoNear

I encountered a strange issue while using the aggregate function with this pipeline: [ { $geoNear: { near: [6.0950994999999999, 49.8914114000000026], distanceField: 'distance', }, }, ] When I call the aggregate function, ...

Combining disparate arrays with serialized name/value pairs

Is there a way to merge an unassociated array with serialized name/value pairs without manually iterating over them? //First, I select certain values from mytable var data = $('#mytable input:checked'); console.log(data); //Object[input attribu ...

Challenges in using HAML5 Canvas for mathematical applications

Hey there! I'm currently working on utilizing the canvas element to form various shapes, but I've encountered a few challenges when it comes to the mathematical aspect. Issue 1: I need to calculate an angle that is relative to the preceding line ...

jQuery is not active on responsive designs

I have implemented a script in my JavaScript code that changes the color of the navigation bar when scrolling. The navigation bar transitions to a white color as you scroll down. However, I am facing an issue with responsiveness and would like to deactivat ...

Guide to retrieve an array retrieved from MySQL using PHP

I am attempting to retrieve an array from a public function stored in a PHP file. The array I want to extract is in JSON format. I have the following code for accessing the array (which has worked in other instances), but it currently only outputs "Array" ...

What is the best way to extract every nth digit from a number using a given reference point?

The subject of the title may cause confusion, so let me clarify my goal. With values of n = 51 and m = 24, I aim to achieve this result: [ {start:0, end:24}, {start:24, end:48}, {start:48, end:51} ] Currently, I have made progress on this ...

"An Introduction to Handling NullPointerException and Array Input/Output

Hey there, I'm new to Java and currently working on a project where I've created a class to store an array and implement bubble sort algorithm for sorting. import java.io.*; public class Array { private int array[]; private int n; pr ...

Facing difficulty observing Content-Disposition header in create-react-app project

I am using a create-react-app that is being served by express. Express is acting as a proxy for the app and all the application logic resides in CRA. My issue lies in calling an API to download a file. The API sends back a "Content-Disposition" header wit ...

Enable JavaScript in Webdriver Selenium to enhance the functionality of your web

I am facing an issue with my Java program and Selenium WebDriver. The script I have does not detect the button "Open device access" because its style is set to "display: none". Usually, clicking on "Device Access" triggers JavaScript to display the "Open ...

Troubleshooting a jQuery filter function selector issue

Here's a function I've created: $.fn.filterByClass = function(cls) { var o = $(this); return o.filter(function() { if ($(this).attr("class") == cls) { return $(this); } }); }; Let's say we have multiple fo ...

"Omitting the parameter value when passing it to a directive in Angular

I recently developed a Directive in Angular7, but I encountered an issue when trying to pass a string value from the HTML to the directive. When using the following code snippet in my HTML: <ng-template [appValidatePermission]='CreateRole'&g ...

"Deleting a specific element from an array schema in a Node.js application

I need to update my database by setting ProjectSubmit.pending = true in order to remove accepted proposals. However, I also added these proposals to an array schema when they were accepted, and now I need to remove that index from the array. Here is my sc ...

Find a specific string in an HTML document while ignoring any HTML tags within the body

If my webpage contains the following HTML structure: <body> <b><span>jQuery</span> is designed to change the way that you write <span><i>JavaScript</i></span>.</b> </body> ...

The Navbar in my React Material UI app is being covered by the Drawer component. Can someone guide me on how to fix

I am facing an issue where the drawer is overlaying my navbar instead of disappearing behind it when opened. I tried adjusting the z-index in my styles but it doesn't seem to be working as expected (see screenshot). The z-index for the navbar is set h ...

Vue JS: Easily Calculate the Total Sum of All Columns

An example of a query in the backend controller public function show($id) { $structural = DB::table('attendance')->where('payroll_daily_id',$id) ->where('assignment','STRUCTURAL') -&g ...

Updating a section of a component using another component

I need to update the Header.vue component from the ConfirmCode Component when the confirm method is called When a user logs in with axios ajax, I want to refresh the li element of the header component Appointment.vue: <send-sms-modal@clickClose="setS ...

Unknown provider in Angular when using factory inside anonymous function wrapper

I encountered an issue with an unknown provider error when using a factory and declaring it with an anonymous function: (function () { 'use strict'; angular.module('app').factory('errorCodeFactory', errorCodeFactory) ...

Guide on transforming UTC time from the server to the local time of users during a GET request

I am currently facing a challenge where I need to verify if the date of the last time an element was clicked matches the current date. Due to my server generating the current date which is 5 hours ahead of my local time, there is a discrepancy causing the ...

Concealing Anchor Tags linked to a particular URL

I am working on implementing a Webview in my Android application. I am looking to hide specific tags with a particular href attribute. How can I achieve this? For example, I want to hide Link 2 and Link 4, which have a tracklist in the href attribute with ...

JavaScript will not modify the content of the page

Initially, everything was working fine with this code when I was using a JavaScript window prompt to input the length. However, I decided to switch it up and use an if statement along with a dropdown window in the HTML, and now it doesn't seem to be w ...