Achieve resumable uploads effortlessly with google-cloud-node

While this code works well for regular uploads, I am curious about how the resumable upload feature functions when a user loses connection during a large upload. Does simply setting 'resumable' to true in the writeStream options make it work effectively?

I have gone through the documentation on performing resumable uploads, and it seems that the functionality should be encapsulated within the createWriteStream function.

To test this, I turned off my wifi midway through an upload, but upon resuming, the time taken to complete the upload was the same as if it were uninterrupted. This has left me unsure if the resumable upload actually worked as intended.

If anyone can provide assistance or clarification on this matter, please feel free to let me know.

stream = remoteFile.createWriteStream({gzip: true, resumable: true, metadata: {contentType: file.mimetype} });

stream.on('error', (err: any) => {
    next(err);
    res.status(400).send('err');
});

stream.on('finish', () => {
    res.status(200).send('Success!');
});

stream.end(file.buffer);

Answer №1

I was hesitant to rely solely on the resumable metadata option for enabling upload resumption, so I decided to follow the guidelines provided here. Below is the code snippet that I used specifically for a single request resumable upload, not multiple chunk uploads.

Firstly, a POST request is sent to the bucket's upload URL in order to obtain the resumable session URI

let url = 'https://www.googleapis.com/upload/storage/v1/b/bucketname/o?uploadType=resumable&name=testing';

let objBody = JSON.stringify({'metadata': {'someDataPoint': req.body.someDataPoint}});

let contentLength: number = Buffer.byteLength(objBody, 'utf-8');

let options = {
  url: url,
  method: 'POST',
  body: objBody,
  headers: {
    'Content-Length': contentLength,
    'X-Upload-Content-Type': req.file.mimetype,
    'X-Upload-Content-Length': req.file.size,
    'Content-Type': 'application/json; charset=UTF-8'
  }
};

request(options, (error: any, response: any, body: any, stat: any) => {

    if (response && response.statusCode === 200) {
        // send response.headers.location to next function for put request

    } else {
        console.log('err!', error);
        res.status(400).send(error);
    }

});

Following that, a PUT request is made to the obtained session URI with the file data

let url = resumableUri;

let options = {
    url: url,
    method: 'PUT',
    body: req.file.buffer,
    headers: {
        'Content-Length': req.file.size,
        'Content-Type': req.file.mimetype,
    }
};

request(options, (error: any, response: any, body: any, stat: any) => {

  if (response && response.statusCode === 200) {
      res.status(200).send('ok');
  } else if (response && response.statusCode >= 500) {
      // send request to resume upload
  } else {
      // try upload again
  }

});

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

Create and export a React component with dual properties

Currently, I am utilizing MaterialUI in my project and exporting components in the following manner: import withStyles, { WithStyles } from "@material-ui/core/styles/withStyles"; ... export default withStyles(styles)(Users); Recently, I have integrated ...

How can we limit the files served from an Express static directory to only .js files?

I'm curious to know if it's doable to exclusively serve one specific type of file (based on its extension) from an Express.js static directory. Imagine having the following Static directory: Static FileOne.js FileTwo.less FileThree. ...

Exploring the best practices for rendering nested array elements in React.js

Is there a way to display only one URL from a nested array in React using CardMedia component? https://i.sstatic.net/K6JDz.png {props.currentTodos.map((currentTodo) => ( <CardMedia className={cl ...

Having trouble with Array.filter functionality

Despite the multitude of discussions on this topic, I have not been successful in using the Array.filter method to remove an item from a string-based Array. Below is an example of the filter method being used in the context of mutating a Vuex store. UPDAT ...

Creating default selection in angular 6 using formControlName in a dropdown menu

I am currently learning MEAN stack with Angular 6 and I have a question regarding selecting the default value in a dropdown using formControlName. When updating a form, I need to have a default value in my dropdown list but I'm only getting a blank op ...

Guide to displaying a task within a table cell after a user submits the form

<?php $servername = "localhost"; $username = "u749668864_PandaHost"; $password = "Effy1234"; $dbname = "u749668864_PandaHost"; $q = $_REQUEST["task"]; $task = $q; echo $task; $conn->close(); ?> Exploring the world of ajax has left me scratching ...

Obtain the element created by a directive within the controller

I'm currently utilizing the wrapper located at: https://github.com/Yankovsky/nouislider-angular/blob/master/nouislider.js for the nouislider plugin. Within my controller, I aim to access the element that I've created in the template: <div ya ...

Resetting input field based on callback function

In my simple script, I have two pages - script.php and function.php. The script.php page contains an input field with the ID #code, a link with the ID #submit, and a jQuery function. $('#submit').click(function () { $.ajax({ url: 'f ...

jQuery color picker plug-in for selecting and displaying a range of colors

I am currently developing a program that utilizes HTML, CSS, JavaScript, and JQuery for its user interface. One essential feature of this UI is the ability for users to choose one option from a set list of colors. It is crucial that each element in this co ...

Updating a MongoDB Atlas collection with the help of an AWS Lambda function

Our system relies on MongoDB Atlas for our cloud database and NodeJS in the backend. To automate a daily data retrieval task from a third-party API and update our DB collection, we currently use a cron job that runs at 2 AM. However, the client is reques ...

Storing tasks in the server backend for later access

Struggling to find the most efficient way to store todo list items in the backend. I've heard that storing arrays and objects directly in the backend may not be optimal. Currently working on a web app inspired by Google Keep. Here's some context ...

Is there a way to send JSON using ExpressJS in UTF-8 encoding?

Currently facing an issue with my new web app that I haven't encountered in the past. Experimenting with a simple code snippet like this: var jsonToSend = {hello: "woørld"}; app.get('/someUrl', function(req, res) { res.setHeader('Co ...

What is the best way to create an associative array using jQuery and then send it through AJAX to be parsed by PHP?

Is there a way to create an associative array in jQuery and send it via ajax to a php page for processing? Here is an example of what I am trying to achieve... // jQuery if($something == true) { data[alt] = $(this).attr('alt'); data[sr ...

Issue with prop inheritance in componentInheritance problem with passing props to

How can I pass the state function setMyChoice as a prop to the GamePlay component in a rock paper scissors Nextjs application? The goal is to produce a value for comparison. In the Gameplay component, I then pass a prop to another component called PlayButt ...

Executing multiple AJAX requests with promises in Angular based on an array of values

I have been working on implementing multiple ajax calls in sequence using Angular. Interestingly, everything seems to be working fine when I use $.ajax, but when I switch to using $http for server requests in Angular, things start to go awry. Both methods ...

Encountered an import error when using "npm run build", but it runs successfully with "npm start"

My React app is running smoothly with npm start, but I encounter an error when trying to build it using npm run build. The error message I receive is: `Creating an optimized production build... Failed to compile. Attempted import error: './parseq-lan ...

Exploring the world of third-party widgets: Comparing Angular, jQuery, and traditional JavaScript

I have a plan to develop a simple embeddable widget similar to Google ads or Facebook like box. Users will be able to easily add the widget to their website and I will extract some parameters to fetch data from my servers. Previously, I relied on jQuery f ...

Team members

Just started diving into Angular and practicing coding with it while following video tutorials. However, I've stumbled upon something in my code that has left me puzzled. I'm curious about the significance of the line "employees: Employee[]" in ...

What is the best way to create a JavaScript "input" field that only accepts vowel letters?

Is there a way to create an "input" field that only accepts vowel letters? Additionally, I want to ensure that copying and dragging text into the input field will also be filtered out. <div class="field"> <input class="fie ...

Generatively generating a new element for the react application to be mounted on

I am looking to enhance my JavaScript application using React by creating a build. Currently, the application consists of just a single file structured as follows. This file essentially creates a div element and continuously changes the color of the text & ...