1

I am trying to initialize the AWS constant in my LWC component but always it is undefined.

import { LightningElement, track } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import AWS_SDK from '@salesforce/resourceUrl/awssdk';

export default class AwsFileExplorer extends LightningElement {    
    albumBucketName = BUCKET__NAME;
    accessKeyId = ACCESS__KEY;
    secretAccessKey = SECRET__KEY;
    region = 'us-east-2';
    s3;    


    renderedCallback() {
        if (this.awsSdkInitialized) {
            return;
        }
        this.awsSdkInitialized = true;

        Promise.all([
            loadScript(this, AWS_SDK),            
        ])        
            .then(() => {
                this.awsSdkInitialized();
            })
            .catch(error => {
                this.error = error;
            });
    }

    awsSdkInitialized() {        
        const AWS = AWS;
        AWS.config.update({
            accessKeyId : this.accessKeyId,
            secretAccessKey : this.secretAccessKey
        });

        AWS.config.region = this.region;

        this.s3 = new AWS.S3({
            apiVersion: '2006-03-01',
            params: {Bucket: this.albumBucketName}
        });

        console.log(this.accessKeyId);
        console.log(this.s3);

    }

    listS3Objects() {
        this.s3.listObjects(function(err, data) {
            if (err) {
              console.log("Error", err);
            } else {
              console.log("Success", data);
            }
          });
    }    
}
1

Note that a third party library function gets attached to a window object in salesforce when referenced from static resource.

The key is notice how i use window variable.

const AWS = window.AWS;

The below should work

import { LightningElement, track } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import AWS_SDK from '@salesforce/resourceUrl/awssdk';

export default class ParentCmp extends LightningElement {

 albumBucketName = 'BUCKET__NAME';
 accessKeyId = 'ACCESS__KEY';
 secretAccessKey = 'SECRET__KEY';
 region = 'us-east-2';
 s3;

 awsSdkInitialized = false;

 renderedCallback() {
    if (this.awsSdkInitialized) {
        return;
    }
    this.awsSdkInitialized = true;

    Promise.all([
        loadScript(this, AWS_SDK),
    ])
        .then(() => {
            this.awsSdkInitialize();
        })
        .catch(error => {
            console.log(error);
        });
}

awsSdkInitialize() {
    const AWS = window.AWS;
    console.log(AWS);
    AWS.config.update({
        accessKeyId : this.accessKeyId,
        secretAccessKey : this.secretAccessKey
    });

    AWS.config.region = this.region;

    this.s3 = new AWS.S3({
        apiVersion: '2006-03-01',
        params: {Bucket: this.albumBucketName}
    });

  }
}
  • I tried this way earlier as well, but still it is undefined for me. The same way if I try with some other 3rd party library then it works, but in this situation it doesn't works. – Prateek Kumar Sep 15 at 2:40
  • It was not undefined for me .It did print all the functions when I did window.AWS – Mohith Shrivastava Sep 15 at 2:49
  • Also what is coming undefined ? – Mohith Shrivastava Sep 15 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.