Syncfusion AI Assistant

How can I help you?

Save PDF files to Google Cloud Storage

28 Feb 20264 minutes to read

Follow the steps below to save a PDF file to Google Cloud Storage using the ASP.NET Core PDF Viewer with a server-backed web service.

Step 1: Create a service account

Open the Google Cloud Console and navigate to IAM & Admin > Service accounts. Click Create Service Account, enter a name, assign roles (for example, Storage Object Admin), and create a key in JSON format. Download the key file securely and use it in your application for authentication and access to the Google Cloud Storage bucket. For more details, see the official documentation

Step 2: Create an ASP.NET Core PDF Viewer sample

Follow the instructions in this Getting Started guide to create a simple PDF Viewer sample in ASP.NET Core.

Step 3: Modify the Index.cshtml.cs file in the project

  1. Import the required namespaces at the top of the file:
using System.IO;
using Google.Cloud.Storage.V1;
using Google.Apis.Auth.OAuth2;
  1. Add the following private fields and constructor parameters to the Index.cshtml.cs class. In the constructor, assign the values from the configuration to the corresponding fields.
// Private readonly object _storageClient
private readonly StorageClient _storageClient;

private IConfiguration _configuration;

public readonly string _bucketName;

public IndexModel(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment, IMemoryCache cache, IConfiguration configuration)
{
    _hostingEnvironment = hostingEnvironment;
    _cache = cache;
    // The key file is used to authenticate with Google Cloud Storage.
    string keyFilePath = @"path/to/service-account-key.json";

    // Load the service account credentials from the key file.
    var credentials = GoogleCredential.FromFile(keyFilePath);

    // Create a storage client with Application Default Credentials
    _storageClient = StorageClient.Create(credentials);

    _configuration = configuration;

    _bucketName = _configuration.GetValue<string>("BucketName");
}
  1. Modify the OnPostDownload() method to save the downloaded PDF files to the Google Cloud Storage bucket.
public IActionResult OnPostDownload([FromBody] jsonObjects responseData)
{
    PdfRenderer pdfviewer = new PdfRenderer(_cache);
    var jsonObject = JsonConverterstring(responseData);
    string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);
    string bucketName = _bucketName;
    string fileName = jsonObject["documentId"];

    // Convert the base64 string back to bytes
    string result = Path.GetFileNameWithoutExtension(fileName);
    byte[] documentBytes = Convert.FromBase64String(documentBase.Split(",")[1]);

    // Upload the document to Google Cloud Storage
    using (var memoryStream = new MemoryStream(documentBytes))
    {
        _storageClient.UploadObject(bucketName, result + "_downloaded.pdf", null, memoryStream);
    }
    return Content(documentBase);
}
  1. Open the appsettings.json file in the project and add the following lines below the existing “AllowedHosts” configuration.
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "BucketName": "Your Bucket name from Google Cloud Storage"
}

NOTE

Replace the placeholder with the actual Google Cloud Storage bucket name.

NOTE

Replace path/to/service-account-key.json with the actual file path to the service account key JSON file.

Step 5: Set the PDF Viewer properties in the ASP.NET Core PDF Viewer component

Set the documentPath property of the PDF Viewer component to the desired PDF file name that you wish to load from Google Cloud Storage. Ensure that the document exists in the target bucket.

@page "{handler?}"
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <ejs-pdfviewer id="pdfviewer" style="height:600px" serviceUrl="/Index" documentPath="PDF_Succinctly.pdf">
    </ejs-pdfviewer>
</div>

NOTE

Install the Google.Cloud.Storage.V1 NuGet package in the application to use the previous code example.

View sample in GitHub