Vingtoft
Vingtoft

Reputation: 14606

Angular | Partially import aws-sdk in typescript

I'm using the aws-sdk in my Angular application. The bundle size is HUGE, and I'm only using a tiny fraction of the library:

enter image description here

Question: Is there any way to only partially import aws-sdk?

Please note:

Upvotes: 3

Views: 621

Answers (2)

Tom Lima
Tom Lima

Reputation: 1056

In my application I use only S3. And I managed to reduce the size of the bundle as follows:

import AWS from 'aws-sdk/global';
import S3 from 'aws-sdk/clients/s3';

//Here is an example of code used to perform file upload.

AWS.config.update({
    accessKeyId: "YOUR-ACCESS-KEY",
    secretAccessKey: "YOUR-SECRET",
    "region": "us-east-1"  
}); 

var file = document.getElementById("input-upload").files[0];
var bucket = new S3({params: {Bucket: 'mybucket'}});
var params = {Key: 'myfolder', ContentType: file.type, Body: file};

bucket.upload(params);

enter image description here

Upvotes: 0

Michael
Michael

Reputation: 2454

From the docs, you can import individual parts of the AWS SDK like so:

// import individual service
import S3 from 'aws-sdk/clients/s3';

You'll need to consult the documentation / type definitions to see what the ones you want are called exactly.

Avoid importing the whole "aws-sdk" package as this won't be tree shaken

Upvotes: 1

Related Questions