Node.js POST file for the server

I am trying to write an application that will allow my users to upload files to my Google Cloud Storage account. To prevent overwriting and do some custom processing and logging on my side, I use the Node.js server as an intermediary for the download. Thus, the process:

  • User uploads file to Node.js server
  • Node.js server analyzes the file, checks the file type, saves some data in the database
  • Node.js server uploads file to GCS
  • Node.js server response to a user request with a note about skipping / failure

I get a little lost in step 3, exactly about how to send this file to GCS. This question gives some useful insights as well as a good example, but I'm still confused.

I understand that I can open a ReadStreamtemporary download file and pass it to an object http.request(). I am confused since I can indicate in my POST request that the data in the channels is a variable file. According to the GCS API Docs , there must be a variable file, and it must be the last.

So, how do I specify the name of the POST variable for data transmitted over the channels?

Bonus points, if you can tell me how to directly link it to my user load, and not store it in a temporary file

+5
source share
1 answer

, POST, Content-Type: multipart/form-data;boundary=myboundary. write() - (linebreaks \r\n):

--myboundary
Content-Disposition: form-data; name="field_name"

field_value

write() - :

--myboundary
Content-Disposition: form-data; name="file"; filename="urlencoded_filename.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

binary_file_data

binary_file_data pipe():

var fileStream = fs.createReadStream("path/to/my/file.jpg");
fileStream.pipe(requestToGoogle, {end: false});
fileStream.on('end, function() {
    req.end("--myboundary--\r\n\r\n");
});

{end: false} pipe(), , . -- .

, Google content-length ( ). , POST POST Google, , content-length, .

content-length . - Buffer.byteLength(body) , , , . :

var body_before_file = "..."; // string fields + boundary and metadata for the file
var body_after_file = "--myboundary--\r\n\r\n";
var fs = require('fs');
fs.stat(local_path_to_file, function(err, file_info) {
    var content_length = Buffer.byteLength(body_before_file) + 
            file_info.size + 
            Buffer.byteLength(body_after_file);
    // create request to google, write content-length and other headers
    // write() the body_before_file part, 
    // and then pipe the file and end the request like we did above

, - Google, , .

... , , PUT . https://developers.google.com/storage/docs/reference-methods#putobject transfer-encoding: chunked, . , , pipe() , . https://github.com/felixge/node-formidable , - :

incomingForm.onPart = function(part) {
    if (part.filename) {
        var req = ... // create a PUT request to google and set the headers
        part.pipe(req);
    } else {
        // let formidable handle all non-file parts
        incomingForm.handlePart(part);
    }
}
+4

All Articles