node.js code below
created a bucket in 'ap-south-1' Region bucket link in public u can view on :- https://iamgroot007.s3.ap-south-1.amazonaws.com/deadpool.png
const AWS = require("aws-sdk");
const fs = require("fs");
const BUCKET = process.env.BUCKET;
const REGION = process.env.REGION;
const ACCESS_KEY = process.env.ACCESS_KEY_ID;
const SECRET_KEY = process.env.SECRET_ACCESS_KEY;
const localImage = "./ap.png";
const imageRemoteName = `catImage_${new Date().getTime()}.png`;
router.post("/image-upload", (req, res) => {
AWS.config.update({
accessKeyId: ACCESS_KEY,
secretAccessKey: SECRET_KEY,
region: REGION,
});
const s3 = new AWS.S3();
s3.putObject({
Bucket: BUCKET,
Body: fs.readFileSync(localImage),
Key: imageRemoteName,
})
.promise()
.then((response) => {
console.log(`done! - `, response);
console.log(
`The URL is ${s3.getSignedUrl("getObject", {
Bucket: BUCKET,
Key: imageRemoteName,
})}`
);
})
.catch((err) => {
console.log("failed:", err);
});
Getting Error :-
message: 'Inaccessible host:
s3.ap-south-1\'. This service may not be available in theap-south-1,' region.', code: 'UnknownEndpoint', region: 'ap-south-1,', hostname: 's3.ap-south-1', retryable: true, originalError: { Error: getaddrinfo ENOTFOUND s3.ap-south-1 s3.ap-south-1:443 at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26) message: 'getaddrinfo ENOTFOUND s3.ap-south-1 s3.ap-south-1:443', errno: 'ENOTFOUND', code: 'NetworkingError', syscall: 'getaddrinfo', hostname: 's3.ap-south-1', host: 's3.ap-south-1', port: 443, region: 'ap-south-1,', retryable: true, time: 2020-09-11T19:08:30.062Z }, time: 2020-09-11T19:08:30.062Z }
imageRemoteNameis wrong. You should move it into the function handler. The way you have written it, multiple uploads may be written to the same cat file if the Lambda function is warm when invoked (in which case the initialization code outside of the function handler is not re-run). - jarmodimageRemoteNamein the upload route handler. Can you run the awscli on the same system? Maybe test the awscli uploading an object to that same ap-south-1 bucket using the same BUCKET and REGION environment variables in the command line. - jarmod