We use S3 server to store Salesforce files. Once it is uploaded there, if we need to download S3 files, we have to use AWS Signature Version 4 now onwards. We have another post (Use Named Credential to Upload File in S3) which will help in uploading files using AWS Signature Version 4. In this post will go for downloading file from AWS S3 server.
Below are steps for downloading S3 files
- Create named credential for AWS
- Create apex class to download file content
- Create LWC component to download on client side
- Call LWC component on record action
1. Create named credential for AWS
Follow steps mentioned in section Create a named credential for AWS Credential of from our other blog.
2. Create apex class to download file content
Once we created a named credential named S3Server from above steps. Let us create class which will download file content from AWS S3.
FileStore is object which hold S3 related information for files. It has S3 url, file extension and file name.
3. Create LWC component to download on client side
Create a LWC component which will get file data from apex and show that in different browser for saving/downloading it.
It will download file as soon as Download File action clicked on file store record.
4. Call LWC component on record action
Once LWC component is ready, create a action for LWC component on file store record object.
Add that action on page layout. It will add download file button on record page.