I need to check the AWS s3 URL is valid or not using Nodejs, I need only the status code not all the data of the file exa:-https://test.s3.us-east-2.amazonaws.com/occupancy.csv, I applied request method but it takes all the data from the file..second method AWS s3.headObject but it only checks the bucket name exist or not tell me is there any method who give the status code that this URL has existed or not
Asked
Active
Viewed 5,629 times
-1
-
this can help: https://stackoverflow.com/questions/19051393/node-js-how-to-check-status-of-a-url-within-a-http-request – Mayra Navarro Jan 21 '20 at 06:28
-
already I used request method but it fetched all the data that I don't need – ADITYA KUMAR Jan 21 '20 at 06:32
-
but status code vary based on what happens, if you want just validate: var url = require("url"); var result = url.parse('http://drive.google.com/0/23'); https://stackoverflow.com/questions/30931079/validating-a-url-in-node-js/55585593 – Mayra Navarro Jan 21 '20 at 06:39
1 Answers
1
You can simply do an HTTP head request to check whether the url exist.
var request = require("request");
var options = {
method: 'HEAD',
url: 'https://test.s3.us-east-2.amazonaws.com/occupancy.csv',
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(response.statusCode);
});
Reference: Getting HTTP headers with node.js

Arun Kamalanathan
- 8,107
- 4
- 23
- 39
-
-
-
sorry for the late reply. updated the answer. it works, I have tested this myself – Arun Kamalanathan Jan 21 '20 at 12:09