Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    • How to Confidently Manage Transactions in Salesforce Apex
    • Building a Dynamic Tree Grid in Lightning Web Component
    • 10 Salesforce Chrome Extensions to Boost Your Productivity
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Tuesday, July 29
    • Home
    • Salesforce Platform
      • Architecture
      • Apex
      • Lightning Web Components
      • Integration
      • Flows & Automation
      • Best Practices
      • Questions
      • News
      • Books Testimonial
    • Industries
      • Artificial Intelligence
    • Hire Me
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Downloads
      • Salesforce Release Notes
      • Apex Coding Guidelines
    • About Us
      • Privacy Policy
    • Contact Us
    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 Sahni6 Mins Read

    How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI

    July 28, 2025
    By Dhanik Lal Sahni7 Mins Read

    Top Mistakes Developers Make in Salesforce Apex Triggers

    July 25, 2025
    By Dhanik Lal Sahni14 Mins Read

    The Ultimate Guide to Apex Order of Execution for Developers

    July 20, 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
    SFBenTopDeveloper
    Ranked #4 Salesforce Developer Blog by ApexHours.com
    ApexHoursTopDevelopers
    Categories
    Archives
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (149) salesforce apex (52) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (5) 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

    MailChimp

    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.