Save PDF file to AWS S3

24 Jul 20262 minutes to read

To save a PDF file to AWS S3, you can follow the steps below.

Step 1: Create a simple console application.

Project configuration window

Step 2: Install the Syncfusion.Pdf.Net.Core and AWSSDK.S3 NuGet packages as a reference to your project from the NuGet.org.



NuGet package installation



NuGet package installation

Step 3: Include the following namespaces in the Program.cs file.

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Amazon.S3;
using Syncfusion.Drawing;
using Amazon.S3.Transfer;
using Amazon;

Step 4: Add the below code example to create a simple PDF and save in AWS S3.

// Create a new PDF document.
using (PdfDocument document = new PdfDocument())
{
    // Add a page to the document.
    PdfPage page = document.Pages.Add();

    // Get the PDF graphics for the page.
    PdfGraphics graphics = page.Graphics;

    // Draw text on the page.
    graphics.DrawString("Hello, Syncfusion PDF!", new PdfStandardFont(PdfFontFamily.Helvetica, 12), PdfBrushes.Black, new PointF(10, 10));

    // Save the PDF to a memory stream.
    MemoryStream stream = new MemoryStream();
    document.Save(stream);
    // The document is closed by the using block; reset the stream position before reading it.
    stream.Position = 0;

    // Set your AWS credentials and region.
    string accessKey = "YOUR_ACCESS_KEY";
    string secretKey = "YOUR_SECRET_KEY";
    // Change to your desired region.
    RegionEndpoint region = RegionEndpoint.YOUR_REGION;

    // Specify the bucket name and object key.
    string bucketName = "YOUR_BUCKET_NAME";
    string objectKey = "HelloWorld.pdf";

    // Upload the PDF to S3.
    using (var s3Client = new AmazonS3Client(accessKey, secretKey, region))
    {
        using (var transferUtility = new TransferUtility(s3Client))
        {
            transferUtility.Upload(stream, bucketName, objectKey);
        }
    }
}

You can download a complete working sample from GitHub.