digitalnodecom / node-red-contrib-generic-s3

Generic S3 nodes for use in Node-RED
https://www.npmjs.com/package/@digitalnodecom/node-red-contrib-generic-s3
Apache License 2.0
3 stars 7 forks source link

Using configured client in function node #9

Closed trajche closed 1 year ago

trajche commented 1 year ago

It would be great to get some instructions on how to use a configured client in a function node (if even possible)?

That way we are not limited only to the nodes that are created, but can also experiment directly without having to define a new client or instance of AWS S3.

rristov60 commented 1 year ago

I am glad to inform you that this is possible due to nodes' only external dependency being @aws-sdk/client-s3. This installs aws-sdk. This can be used for node-red versions that support this functionality. This functionality has been added in Node-RED 1.3.0 release, but it had to be manually enabled by setting functionExternalModules: true in the settings.js file up until 2.0.0. After 2.0.0 release this has been enabled by default. This can be done following the steps instructions given below:

  1. Step 1 - Go in the desired function node, in the Setup tab
  2. Step 2 - Click on the add button at the left bottom corner
  3. Step 3 - Enter the module name that you want to import. Must be exactly like shown in the image @aws-sdk/client-s3 for importing the aws S3 module !!!
  4. Step 4 (optional) - define under what name the module will be imported. Node-RED automatically fills in this field, but you can change as desired
Screenshot 2023-01-16 at 11 49 52

Congrats, the module has been imported 🥳

Next step would be instructions on how to use it. The way you can used functions from the imported module within the function node is with the following code

const S3 = awsSdkClientS3.S3;

var client = new S3({
    endpoint: 'S3 Endpoint',
    region: 'S3 region',
    credentials: {
        accessKeyId: 'Access key id',
        secretAccessKey: 'Super sercret access key'
    }
});

// Example function
client.listBuckets({}, (err, data) => {
    node.warn(data);
})

That's it. You can use other functions that this node module offers, but that is out of scope of this issue and this repository for now. Feel free to explore the module here: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/index.html