fs

Download file from url and upload it to AWS S3 without saving - node.js

老子叫甜甜 提交于 2019-11-30 11:01:00
问题 I'm writing an application which downloads images from a url and then uploads it to an S3 bucket using the aws-sdk. Perviously I was just downloading images and saving them to disk like this. request.head(url, function(err, res, body){ request(url).pipe(fs.createWriteStream(image_path)); }); And then uploading the images to AWS S3 like this fs.readFile(image_path, function(err, data){ s3.client.putObject({ Bucket: 'myBucket', Key: image_path, Body: data ACL:'public-read' }, function(err, resp

Uncaught TypeError: URL is not a constructor using WHATWG URL object support for electron

て烟熏妆下的殇ゞ 提交于 2019-11-30 10:57:55
I am trying to read a file using WHATWG URL object support here and I am getting this error: Uncaught TypeError: URL is not a constructor here is my code: var fs = require("fs"); const { URL } = require('url'); var dbPath = 'file://192.168.5.2/db/db.sqlite'; const fileUrl = new URL(dbPath); I faced the same issue, then I looked into the url module and found a solution For Node V6 use, const URL = require('url').Url; or const { Url } = require('url'); If you look into the module, it exports 5 methods one of which is Url, so if you need to access Url, you can use either of the two methods Are

Node - Can't seek audio stream

假装没事ソ 提交于 2019-11-30 09:24:52
I have created a simple server that uses the fs module to stream an mp3 file to a browser which plays it in an html5 audio element. As it is, the audio streams perfectly fine, however, I can't seek through the stream, even if the part I seek to has already been buffered. var express = require('express'); var app = express(); var fs = require('fs'); app.get('/', function (req, res) { var filePath = 'music.mp3'; var stat = fs.statSync(filePath); res.writeHead(200, { 'Content-Type': 'audio/mpeg', 'Content-Length': stat.size, }); var readStream = fs.createReadStream(filePath); readStream.pipe(res)

Node.js from fs.readFileSync() to fs.readFile()

99封情书 提交于 2019-11-30 01:14:09
I'm trying to get my head around synchronous vs asynchronous in Node.js, in particular for reading an html file. In a request handler, the synchronous version that i'm using, which works is the following: var fs = require("fs"); var filename = "./index.html"; var buf = fs.readFileSync(filename, "utf8"); function start(resp) { resp.writeHead(200, {"Content-type":"text/html"}); resp.write(buf); resp.end(); } exports.start=start; What would be the version using readFile() ?? I understand that readFile is asynchronous so theoretically I should wait that the entire file is read before rendering it,

Download file from url and upload it to AWS S3 without saving - node.js

↘锁芯ラ 提交于 2019-11-29 23:03:53
I'm writing an application which downloads images from a url and then uploads it to an S3 bucket using the aws-sdk . Perviously I was just downloading images and saving them to disk like this. request.head(url, function(err, res, body){ request(url).pipe(fs.createWriteStream(image_path)); }); And then uploading the images to AWS S3 like this fs.readFile(image_path, function(err, data){ s3.client.putObject({ Bucket: 'myBucket', Key: image_path, Body: data ACL:'public-read' }, function(err, resp) { if(err){ console.log("error in s3 put object cb"); } else { console.log(resp); console.log(

read file from aws s3 bucket using node fs

纵然是瞬间 提交于 2019-11-29 20:50:25
I am attempting to read a file that is in a aws s3 bucket using fs.readFile(file, function (err, contents) { var myLines = contents.Body.toString().split('\n') }) I've been able to download and upload a file using the node aws-sdk, but I am at a loss as to how to simply read it and parse the contents. Here is an example of how I am reading the file from s3: var s3 = new AWS.S3(); var params = {Bucket: 'myBucket', Key: 'myKey.csv'} var s3file = s3.getObject(params) You have a couple options. You can include a callback as a second argument, which will be invoked with any error message and the

What are the pros and cons of fs.createReadStream vs fs.readFile in node.js?

空扰寡人 提交于 2019-11-29 18:51:23
I'm mucking about with node.js and have discovered two ways of reading a file and sending it down the wire, once I've established that it exists and have sent the proper MIME type with writeHead: // read the entire file into memory and then spit it out fs.readFile(filename, function(err, data){ if (err) throw err; response.write(data, 'utf8'); response.end(); }); // read and pass the file as a stream of chunks fs.createReadStream(filename, { 'flags': 'r', 'encoding': 'binary', 'mode': 0666, 'bufferSize': 4 * 1024 }).addListener( "data", function(chunk) { response.write(chunk, 'binary'); })

Saving an image stored on s3 using node.js?

大憨熊 提交于 2019-11-29 18:44:39
问题 I'm trying to write an image server that uses node.js to store images on s3. Uploading the image works fine, and I can download and view it correctly using an s3 browser client (I'm using dragondisk, specifically, but I've successfully downloaded it with other ones too), but when I download it with node and try to write it to disk, I'm unable to open the file (it says it may be damaged or use a file format that Preview does not recognize). I'm using the amazon sdk for node and fs to write the

Uncaught TypeError: URL is not a constructor using WHATWG URL object support for electron

一笑奈何 提交于 2019-11-29 16:51:50
问题 I am trying to read a file using WHATWG URL object support here and I am getting this error: Uncaught TypeError: URL is not a constructor here is my code: var fs = require("fs"); const { URL } = require('url'); var dbPath = 'file://192.168.5.2/db/db.sqlite'; const fileUrl = new URL(dbPath); 回答1: I faced the same issue, then I looked into the url module and found a solution For Node V6 use, const URL = require('url').Url; or const { Url } = require('url'); If you look into the module, it

Writing multiple files a loop in Nodejs

淺唱寂寞╮ 提交于 2019-11-29 12:21:37
Hi I'm using a for each loop to in nodejs script to write multiple files to a local location.For the courseTitleArray I'm using "Biology 101,ruby" and I can write one file successfully but not the both .Please help me out. Here is my code so far for (var value in CourseTitleArray) { console.log( "Course Title " + CourseTitleArray[value]); var newImageLocation = path.join(__dirname, 'app/img/courseImages', CourseTitleArray[value] + ".png"); fs.readFile(image.path, function(err, data) { fs.writeFile(newImageLocation, data, function(err) { console.log(CourseTitleArray[value] + " was created