Save PDF to AWS S3 in Blazor SfPdfViewer Component

17 Jul 20264 minutes to read

To save a PDF file to AWS S3, follow these steps:

Step 1: Create an AWS account and an S3 bucket

Set up an AWS account and configure Amazon S3 by following the official guide: AWS Management Console. Create an S3 bucket, configure an IAM user or role with scoped permissions for S3 access, and generate access keys. Store credentials securely using environment variables.

Step 2: Install the AWSSDK.S3 NuGet package

Install the AWSSDK.S3 NuGet package in your application to use the AWS S3 client APIs.

Step 3: Create a Simple SfPdfViewer Sample in blazor

Start by following the steps provided in this link to create a simple SfPdfViewer sample in blazor. This will give you a basic setup of the SfPdfViewer component.

Step 4: Include the following namespaces in the Index.razor file:

@using Amazon;
@using Amazon.S3;
@using Amazon.S3.Model;
@using Syncfusion.Blazor.SfPdfViewer;
@using Syncfusion.Blazor.Buttons;

Step 5: Add the below code example to save the downloaded PDF files to an AWS S3 bucket

@page "/"
@using System.IO

<SfButton @onclick="OnClick">Save file to AWS S3 bucket</SfButton>
<SfPdfViewer2 DocumentPath="@DocumentPath"
              @ref="viewer"
              Height="100%"
              Width="100%">
</SfPdfViewer2>

@code {
    private SfPdfViewer2 viewer;
    private string DocumentPath { get; set; } = "PDF_Succinctly.pdf";

    private readonly string accessKey = "Your Access Key from AWS S3";
    private readonly string secretKey = "Your Secret Key from AWS S3";
    private readonly string bucketName = "Your Bucket name from AWS S3";
    private readonly string fileName = "File Name to be loaded into Syncfusion SfPdfViewer";

    private async Task OnClick(MouseEventArgs args)
    {
        byte[] data = await viewer.GetDocumentAsync();
        string result = Path.GetFileNameWithoutExtension(fileName);
        string FileName = result + "_downloaded.pdf";
        RegionEndpoint bucketRegion = RegionEndpoint.USEast1;

        // Configure the AWS SDK with your access credentials and other settings
        var s3Client = new AmazonS3Client(accessKey, secretKey, bucketRegion);
        using (MemoryStream stream = new MemoryStream(data))
        {
            var request = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = result + "_downloaded.pdf",
                    InputStream = stream,
                    ContentType = "application/pdf",
                };
            // Upload the PDF document to AWS S3
            var response = s3Client.PutObjectAsync(request).Result;
        }
    }
}

Replace the file name with the actual document name that you want to load from the AWS S3 bucket. Make sure to pass the document name from the AWS S3 bucket to the DocumentPath property of the SfPdfViewer component.

NOTE

Replace Your Access Key from AWS S3, Your Secret Key from AWS S3, and Your Bucket name from AWS S3 with your actual AWS access key, secret key, and bucket name.

View sample in GitHub

See also