1

I’d like to set the S3 endpoint globally, once, rather than setting it each time I make a new AWS.S3() call. Is this possible?

This is different than global region setting. I’m looking to set a per service endpoint globally.

Justin808
  • 20,859
  • 46
  • 160
  • 265
  • Possible duplicate of [How to configure the region in the AWS js SDK?](https://stackoverflow.com/questions/45032362/how-to-configure-the-region-in-the-aws-js-sdk) – Steve E. Feb 04 '19 at 04:24
  • It a duplication. Setting the region is different than per service endpoints. – Justin808 Feb 04 '19 at 04:39
  • AWS.S3() will generate the endpoint from the Global AWS.config.region. You can override for services e.g. `AWS.config.s3 = { endpoint: 'https://my-endpoint' };` – Steve E. Feb 04 '19 at 05:21
  • @SteveE. - I'll try again but I swear `AWS.config.s3 = { endpoint: 'https://my-endpoint' };` didn't work for me. – Justin808 Feb 04 '19 at 05:28

2 Answers2

0

create a file named aws.js inside that file create all the functions you want to use for your s3 bucket i.e for uploading content or deleting content.

then just export that file and use it anywhere you want.

e.g : i'm using SQS in my case:

const AWS = require("aws-sdk");
AWS.config.update({ region: "us-east-1" });

// Create an SQS service object
const sqs = new AWS.SQS({ apiVersion: "2012-11-05", "accessKeyId": process.env.ACCESS_KEY_ID, "secretAccessKey": process.env.SECRET_ACCESS_KEY });

const QueueUrl = process.env.SQS_QUEUE_URL;

const sendPayloadToSQS = async message => {
    const params = {
        MessageBody: JSON.stringify(message),
        QueueUrl
    };

    const request = sqs.sendMessage(params);
    const result = await request.promise();

    if (result) {
        console.log(`Message queued to SQS successfully : ${result.MessageId}`);
    } else {
        console.log("Message queued failed");
    }
};

module.exports = sendPayloadToSQS;
Ankur Dubey
  • 454
  • 5
  • 15
  • This isn’t really answering the question, though it’s a possible alternative. I’m hoping for a global configuration option. I don’t really want to do this for all my aws calls. Wrapping the SDK doesn’t feel correct. – Justin808 Feb 04 '19 at 04:38
0

What is your aws-sdk version?

const AWS = require("aws-sdk");
console.log(new AWS.S3().config.endpoint); // s3.amazonaws.com

AWS.config.s3 = { endpoint: "https://my-endpoint" }; // sure this line has been call first of all

console.log(new AWS.S3().config.endpoint); // https://my-endpoint
hoangdv
  • 15,138
  • 4
  • 27
  • 48