function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Suman KuchSuman Kuch 

How to get a file URL from secured amazon s3 using REST API?

Hi,
We need to show around 100 photos on a page, the photos are in secured S3 bucket. and each photo retrieved by photoid. I tried to get blog and convert into string to display on  page but the page is taking around 30 secs to load all photos. Is there a way to get photo URL rather getting blob and Ill put that on page (I use iframe to show photo)?

Here is the apex code I'm using for REST api call to get photo by id.
public void getPhotofromAWS(String photoid){
        
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        //String endpoint ='https://xxxxxx.s3.amazonaws.com/photo';
        String bucketName = 'xxxxxx';
        String fileName = photoid+'.jpg';
        String formattedDateString = Datetime.now().format('EEE,   dd MMM yyyy HH:mm:ss z');
        req.setHeader('Date', formattedDateString);
        String stringToSign = 'GET\n\n\n'+formattedDateString+'\n/'+bucketName +'/photo/'+filename;
        Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof('aaaaaaaaaAxxxxxxxxxsssssAAAAAAa'));
        String sig = EncodingUtil.base64Encode(mac);
        String auth = 'AWS' + ' ' + 'AAAAAAAAAAAAAAA' + ':' + sig;
        req.setHeader('Authorization',auth);
        String endpointBase = 'http://' + bucketName + '.s3.amazonaws.com/photo/' + filename;
        system.debug(' guid '+guid);
        req.setEndpoint(endpointBase );
        Http http = new Http();
        HTTPResponse res = http.send(req);
        //photoString = res.getBody();
        photoString='data:'+res.getHeader('Content-Type')+';base64,'+EncodingUtil.base64Encode(res.getBodyAsBlob());
 
    }
Please advice.

Thanks,
Sumant
 
NagendraNagendra (Salesforce Developers) 
Hi Suman,

Please check with below link from forums community which helps you to accelerate with above requirement. Kindly mark this as solved if it's resolved.

Regards,
Nagendra.