How can I get uploaded file content using multiparty in node.js? I don't need temp file, I need to redirect all stream into Google Cloud Storage in order to save the file content, but I can't find the way to get this content with events.
Found the answer. We need to use part stream a subscribe to standard streams events, like, data and end in order to receive file's data.
part.on("data", chunk => {
writeStream.write(chunk);
});
part.on("end", chunk => {
writeStream.end(chunk);
});
writeStream - is a another stream where you want to put your data. In my case that was Google Cloud Storage file PUT request via signedUrl.
part - is a part object of a form part event
Related
I have a client in React that sends a form data with a file. When that file arrives to the server, the body is parsed by body parser and its result is a buffer. The idea is that the file keep saved in some place of my server, because I want to use it later from my client. So I'd like to know how should I handle this problem.
I tried to write directly this buffer as a file with fs, but the file created has an error of format, so I can't access it.
You can do stuff like this
var fs = require('fs');
fs.writeFile('newImage', req.files.image, function (err) {
if (err) throw err;
console.log("It's saved");
});
correct parameter order of fs.writeFile is the filename first then the content.
If you're using express.bodyParser() you'll have the uploaded files in the req.files field.
And of course you should write the callback:
POV: Your image file should be in req.files not the body.
I think you need a package for storing a file on your backend service. I had used morgan package for that and I was satisfied with using it. I have just searched other packages for storing a file, i found express-fileupload package. Maybe you want to look at how to use those. If you want to store a file, using the third package would be better for you and for your effort.
this.state.videoBlob is a blob object. I used URL.createObjectURL to generate a blob URL, and passed it to fs.createReadStream, like below:
fs.createReadStream(URL.createObjectURL(this.state.videoBlob))
This blob url looks like:
blobURL: blob:http://localhost:3000/dabe5cdd-00cc-408a-9f3d-b0ba5f2b10b3
But I got an error saying:
TypeError: fs.createReadStream is not a function
The problem won't exist if I passed some online video URL. So how can I read blob from fs.createReadStream? Thanks!
When I look at the code behind fs.createReadStream(), it calls new ReadStream() and passes the path/url to that. On this line of code, it appears that the only type of url that is supported is a file URL. The doc is silent on that topic which is why I went and looked at the code. So, it does not appear to me that fs.createReadStream() supports that type of pseudo-URL.
Since you just want a readstream from that URL and you have the actual URL of the remote resource, I would suggest you just use either http.get() or request() or something similar as those will all contact the remote host and return to you a readStream. Since your objective was to get a readStream, this is one way to achieve that.
http.get('http://localhost:3000/dabe5cdd-00cc-408a-9f3d-b0ba5f2b10b3', (res) => {
// res is a readstream here
}).on('error', (err) => {
// error on the request here
});
FYI, you may find this answer on Blob URLs to be useful. I don't see any evidence that fs.createReadStream() supports blob URLs. In the browser, they are something created only by the internals of the browser and are only useful within that specific web page context (they refer indirectly to some internal storage) and can't be passed outside the web page or even preserved from one web page to the next. If you wanted your server to have access to the actual data from a blob URL created in the browser, you'd have to upload the actual data to your server. Your server can't access a blob URL created in the browser.
I'm trying to stream files to the client side without downloading the file via GetObject method and pipe this response back. Is it possible?
When I do this:
s3.getObject(params).on('httpHeaders', function (statusCode, headers) {
res.set('Accept-Ranges', headers['accept-ranges'])
res.set('Content-Length', headers['content-length']);
res.set('Content-Type', headers['content-type']);
})
.createReadStream().pipe(res);
I'm storing the s3 object in the server memory and piping back to the client.
Is there any way to do this without store the file in the server-side memory?
Maybe you can just send the s3 path(If its a public bucket) to your client and download the file using File Saver
I'm want to upload mp4 file to my azure storage without md5 content.
var uploadOptions = {};
uploadOptions.storeBlobContentMD5 = false;
blobSvc.createBlockBlobFromLocalFile('kovach', fileName, files.file.path,uploadOptions, function(error, result, response) {
if(!error){
// file uploaded
console.log(result);
res.end(fileName);
}
});
but in the response I got value in content md5.
When you upload a local file larger than 32MB, the blobSvc.createBlockBlobFromLocalFile method actually invokes several REST calls:
Create a new block blob.
Put blocks.
Commit the block list.
From the Put Block List REST, the Content-MD5 header is returned so that the client can check for message content integrity. This header refers to the content of the request, meaning, in this case, the list of blocks, and not the content of the blob itself.
I think you try blobsvc.getBlobProperties, you won't see content-md5 set on this blob.
I'm implementing a webserver using Koa. One of my request handlers needs to receive and store a large zip file. The file is NOT uploaded from a web page, but sent from another NodeJs application. Therefore it's not multipart encoded, but just applcation/octet-stream. How can I read such a file from the request?
I've noticed there is a request.socket object but I could not find any documentation on how to use it.
In other words I need the opposite to
this.body = fs.createReadStream(path);