Using ASW javascript SDK v2 (cannot use v3 for dependency reasons related to my project)
I am facing an error when trying to upload a file to an S3 bucket with the following JS code.
FYI :
- I am able to do so in VScode command line through aws CLI
- My profile is configured and I can list buckets with code
Here is my code :
const fs = require('fs');
const AWS = require('aws-sdk');
let s3 = new AWS.S3({apiVersion: '2006-03-01'});
const fileName = '/Users/*****/****/*****-ui/**/***/****/BE-Commands/cmd.json';
const uploadFile = () => {
fs.readFile(fileName, (err, data) => {
if (err) throw err;
const params = {
Bucket: 'bucket-test', // pass your bucket name
Key: 'contacts.csv', // file will be saved as testBucket/contacts.csv
Body: JSON.stringify(data, null, 2)
};
s3.upload(params, function(s3Err, data) {
if (s3Err) throw s3Err
console.log(`File uploaded successfully at ${data.Location}`)
});
});
};
uploadFile();
Getting this error :
Error: connect EHOSTDOWN 169.254.169.254:80 - Local (***.1***.1.182:*****)
at internalConnect (node:net:1084:16)
at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)
at node:net:1290:9
at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
errno: -64,
code: 'CredentialsError',
syscall: 'connect',
address: '1****.254.169.254',
port: 80,
time: 2023-08-10T18:15:50.727Z,
originalError: {
message: 'Could not load credentials from any providers',
errno: -64,
code: 'CredentialsError',
syscall: 'connect',
address: '**********',
port: 80,
time: 2023-08-10T18:15:50.722Z,
originalError: {
message: 'EC2 Metadata roleName request returned error',
errno: -64,
code: 'EHOSTDOWN',
syscall: 'connect',
address: '***.254.***.***',
port: 80,
time: 2023-08-10T18:15:50.722Z,
originalError: {
errno: -64,
code: 'EHOSTDOWN',
syscall: 'connect',
address: '***.***.***.***',
port: 80,
message: 'connect EHOSTDOWN 169.254.169.254:80 - Local (***.***.1.***:49426)'
}
}
}
2
Answers
It turned out to be related to the fact that I am running it in a browser so the code did not work. I have simply added the code in an external JS code and it worked
Well, the SDK for AWS is detecting your environment. So the reason that you’re not able to upload your file is because your configurations are not correct/not being detected. Ensure, that you are passing your configuration credentials correctly.
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html
EDIT: Updated Documentation for SDK v2 since OP is using it