Reputation: 212
2
I have inherited a python lambda which I need to convert to node js. I am a bit stuck with the code that handles authentication and so I am wondering what the JS equivalent would be for the python code below:
import boto3
import json
import requests
from requests_aws4auth import AWS4Auth
region = "eu-west-1"
service = "es"
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
r = requests.get(url, auth=awsauth)
or this one
def get_credentials(session):
session_credentials = session.get_credentials()
if not session_credentials:
return None
read_only_credentials = session_credentials.get_frozen_credentials()
expiration = None
if hasattr(session_credentials, '_expiry_time') and session_credentials._expiry_time:
if isinstance(session_credentials._expiry_time, datetime):
expiration = session_credentials._expiry_time
else:
LOGGER.debug("Expiration in session credentials is of type {}, not datetime".format(type(expiration)))
credentials = convert_creds(read_only_credentials, expiration)
return credentials
Here is my work in progress at what seem to be relevant calls using the javascript aws-sdk:
import AWS from "aws-sdk"
const region = "eu-west-1"
const service = "es"
const credentials = AWS.Credentials().get()
const response = await fetch(url, {
headers: { Authorization: credentials },
credentials: "include"
})
I know the python code works, so I just need to convert it to JS. As you can see I'm not really sure what to do with credentials once I have it or what the equivalent of auth=awsauth would be within the fetch call. Any help would be much appreciated. Thank you!
Upvotes: 0
Views: 861
Reputation: 212
below code
import { Credentials } from "aws-sdk";
const credentials = new Credentials();
console.log(credentials);
gives below output
Credentials2 {
expired: false,
expireTime: null,
refreshCallbacks: [],
accessKeyId: undefined,
sessionToken: undefined
}
Upvotes: 0