Tag:

AWS

    Download Files From S3 Server using Apex

    How to Download Files from S3 Server using Apex and Lightning Component

    Downloading or reading files from S3 server is required most of time in our Salesforce projects.  This post will explain how we can download files from AWS S3 Server and show using Lightning component.

    We can download files using two ways.

    1. Using S3 API in Apex and Lighting Component
    2. Using AWS Java Script SDK in Lightning Component

    1. Using S3 API in Apex and Lighting Component

    To download file using Apex, we have to use HTTP Callout with uploaded file URL.  As in our last post  How to Upload Files to S3 Server using Apex , we have uploaded file in S3 server and stored S3 file information like file’s S3 url in custom object File Store.

    GetObject method of S3 Server API will be used to get file. Below are important information about GetObject

    • To download file, we must have access to S3 object.
    • This will return latest version of file.

    Apex Code:

    Let us download uploaded file using Apex and show that file using lightning.

    1. Get S3 Object Url from Salesforce Custom Object (FileStore)
    2. Connect S3 API using HTTPCallout and use Get Method to retrieve file
    3. Provide complete authentication detail like Bukect, credential etc
    4. After successful authentication, file will be retrieved as blob
    5. Encode this blob to show in lightning

    BaseException class  is custom exception class to identify error detail.

    Call above service class in controller class. Controller class will be called from Lightning component.

    Lightning Component:

    2. Using Java Script SDK in Lightning Component

    In this approach file will be downloaded using AWS Javascript SDK. We have to download that SDK library and add that  into static resource.

    This library will be loaded in lightning component to do operation related to file upload/download files.

    <ltng:require scripts="/resource/AWSSDK" afterScriptsLoaded="{!c.doInit}"/>

    Apex Code :

    This apex class will be retrieve file url to which we need to download.

    Complete Code:

    Lightning Component Code:

    SDK’s GetObject function will be used to get file. We have to pass complete AWS credential to get handshake with AWS.

    AWS.config.update({
    accessKeyId: "***************", // Set Access Key
    secretAccessKey: "****************", // Set Secret Key
    region_config: "eu-west-1" // Set your bucket region
    });

    Below code will create object of S3 libray class. getSignedUrl method of object will get pre signed url of file.

    var s3 = new AWS.S3();
    s3.getSignedUrl('getObject', {Bucket: strBucket, Key: strKey}, function (err, url) {
    });

    After file url is retrieved, it is downloaded using XMLHttpRequest.

    Lightning Component Code:

    Reference:

    https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html

    January 24, 2020 7 comments
    1 Facebook Twitter Google + Pinterest