Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Thursday, May 29
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»Use Named Credential to Upload File in S3

    Use Named Credential to Upload File in S3

    Dhanik Lal SahniBy Dhanik Lal SahniJuly 21, 2021Updated:January 15, 202515 Comments2 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Use Named Credential to Upload File in S3
    Share
    Facebook Twitter LinkedIn Pinterest Email

    We mostly have requirements to upload files in file storage servers like AWS S3 Server, Dropbox, Google Drive, etc. This post will help in uploading files to S3 Server. We will use named credential to upload file in S3 server.

    Steps for uploading file in S3 Server using named credential

    1. Create a named credential for AWS Credential
    2. Create an apex class to upload file
    3. Test functionality

    1. Create a named credential for AWS Credential

    Create a named credential with Authentication Protocol as AWS Signature Version 4.

    AWS Signature Version 4 (SigV4) is the process to add authentication information to AWS API requests sent by HTTP.  No need to set any header in this case. All information is shared using this named credential itself.

    Named Credential Detail

    NameS3Upload
    Urlhttps://s3.amazonaws.com/salesforcecodex
    or
    https://{bucket}.s3.{region}.amazonaws.com
    Identity TypeNamed Principle
    Authentication ProtocolAWS Signature Version 4
    AWS Access Key Id{Your access key}
    AWS Secret Access Key{Your secret access key}
    AWS Region{your s3 bucket region}
    AWS Services3

    2 Create an apex class to upload file

    Let us create an apex class that will upload files to the AWS S3 server. For this post, I have created one function which will take LinkedEntityId which is a record where the file is attached.

    We need blob data of attachment to store in S3 Server. So we have used file content VersionData which is in blob format.

    Apart from callout, we need not be required to put any header information for API requests. Named credential will take care of auth headers.

    3. Test functionality

    Let us test uploading of files. For testing, the below code can be executed in the developer console’s anonymous window to upload a document that is attached to the record id. This recordid can be of case, account, contact or any custom object.

    AWSFileService service=new AWSFileService();
    service.UploadDocuments('5002v00002qbQxWAAU');

    The above code can also be written in one method to make requests dynamic and used by many processes.

    @AuraEnabled
    public static void UploadDocToS3Server(string recordId)
    {
    AWSFileService service=new AWSFileService();
    service.UploadDocuments(recordId);
    }

    We can call method UploadDocToS3Server from lightning components.

    References:

    Signature Version 4 signing process

    Checkout previous posts for similar requirements.

    Uploading Files to S3 Server using Apex

    Download Files From S3 Server using Apex

    View S3 File in Lightning Web Component

    AWS integration integration architecture lightning named credential
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleGeneric Apex class for Calling External System
    Next Article Download S3 File in Salesforce using AWS Signature Version 4.0
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni4 Mins Read

    How to Build a Generic Modal Window in Lightning Web Component

    May 26, 2025
    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    View 15 Comments

    15 Comments

    1. N on August 3, 2021 8:06 pm

      Hi Dhanik,

      Got here from the previous post about uploading to S3. I’ve tried setting this up but I’m getting an error message :

      SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.XXXXXXAWS4-HMAC-SHA256

      The URL I set was:
      https://MYBUCKETNAME.s3.eu-west-2.amazonaws.com – is this correct?
      Also – I don’t see anywhere where we use trhe filename string and ass it on – am I missing something here?

      Thanks,
      N

      Reply
      • Dhanik Lal Sahni on August 4, 2021 8:04 am

        Hello N,
        You have to use like this https://s3.amazonaws.com/salesforcecodex. Here salesforcecodex is bucket name, so put your bucket name in place of it. I feel it will work if you give proper detail to named credential.

        Let us connect, if you face issue even after that. Connect to my linked-in profile for this.

        Thank You,
        Dhanik

        Reply
    2. Pingback: Download S3 File using AWS Signature Version 4.0

    3. Bharat on September 11, 2021 11:06 pm

      Hi Dhanik,

      Thanks for your post. It was really helpful. I tried the same for an opportunity record but getting the below error not sure why.
      System.HttpResponse[Status=Bad Request, StatusCode=400]”|

      Reply
      • Dhanik Lal Sahni on September 13, 2021 7:05 pm

        Hello Bharat,

        Have you checked complete error detail using response.getBody()? This error mostly comes when our URL is not created properly. If all good from your side and still issue, please connect on linked-in or telegram. We will connect and resolve it.

        Thank You,
        Dhanik

        Reply
    4. Wojtek on December 3, 2021 8:17 pm

      Hi,
      I’m receiving an Error: 400 The authorization header is malformed; the region ‘eu-central-1’ is wrong; expecting ‘us-east-1

      My configuration for named Credentials:
      URL: https://s3.amazonaws.com/mybucketname
      AWS Region: eu-central-1
      AWS Service: S3
      I’m wondering what’s wrong

      Reply
      • Dhanik Lal Sahni on December 7, 2021 1:10 pm

        Hey,
        Please check this post

        Thank You,
        Dhanik

        Reply
    5. Pingback: Cuttly Url Shortener in Salesforce | SalesforceCodex

    6. Geecel on March 24, 2022 2:21 pm

      Hi I got this error, The XML you provided was not well-formed or did not validate against our published schema salesforce when testing the code above.

      Reply
      • Dhanik Lal Sahni on March 25, 2022 8:58 pm

        Hey Geecel,

        Can you check the line where you are getting errors? Let me know, so that we can connect and resolve your issue.

        Thank You,
        Dhanik

        Reply
        • Geecel on March 28, 2022 1:34 pm

          I was able to resolved the issue. I updated from req.setEndpoint(‘callout:AWSSignature’); to req.setEndpoint(‘callout:AWSSignature’ +’/’+attach.Title.replaceAll(‘[^a-zA-Z0-9\\s+]’,’-‘).toLowerCase()+ ‘.’+attach.FileExtension.toLowerCase()); I hope this helps.
          Thankyou!

          Reply
          • Muthukumar on June 2, 2023 7:50 am

            This solution worked out for me, when I was facing Access denied issue, thanks.

            Reply
    7. Pingback: Difference between SOAP and REST API? - SalesforceCodex

    8. Muthukumar on June 1, 2023 5:27 pm

      I am getting ‘403’ Access denied Error. I am using the same code but still getting this error.

      Reply
      • Dhanik Lal Sahni on June 12, 2023 11:32 am

        Hello Muthu,

        Please check access to user which you are using to explore API.

        Thank You,
        Dhanik

        Reply
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.