I am building an environment which let users to run their nodejs code. It is pretty much like what Code Pen
or runit
does. If users need to run aws sdk code in the environment, I don't know how to handle their credentials and configs. I know aws nodejs sdk has a method config()
which I can pass all configuration in. But usually developers aws credentials and config are saved in ~/.aws/credential
and ~/.aws/config
files. If I ask users to upload these files into the environment, how can I convert them into a parameter can be read by aws sdk? Is there a easy way to do or I have to manually parse these files?
You can do it like this:
const AWS = require('aws-sdk');
// config.json
{"accessKeyId": <YOUR_ACCESS_KEY_ID>, "secretAccessKey": <YOUR_SECRET_ACCESS_KEY>, "region": "us-east-1" }
AWS.config.loadFromPath('./config.json');
You can also do it like this:
var AWS = require("aws-sdk");
AWS.config.update({
region: "us-west-2",
"accessKeyId": <YOUR_ACCESS_KEY_ID>,
"secretAccessKey": <YOUR_SECRET_ACCESS_KEY>
});
Here's an example of hard-coding credentials for the Simple Email Service (SES) in AWS SDK v3:
let { SES } = require("@aws-sdk/client-ses");
const ses = new SES({
apiVersion: "2010-12-01",
region: "us-west-1",
credentials: {
accessKeyId: ".....",
secretAccessKey: ".....",
},
});
I'm guessing it's basically the same for other AWS constructors - i.e. just add that credentials
property with the access keys in it.
(Note that hard-coding your credentials like this convenient, but can be a bad idea for professional/production applications where security is important. See the links at the bottom of this page for several other approaches that are more secure.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With