Open PDF file from Google Cloud storage

24 Nov 20231 minute to read

To Open a PDF file from Google cloud storage, you can follow the steps below

Step 1: Create a simple console application

Project configuration window

Step 3: Install the Google.Cloud.Storage.V1 NuGet package as a reference to your project from the

NuGet package installation

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

using Google.Cloud.Storage.V1;
        using Google.Apis.Auth.OAuth2;

Step 5: Add the below code example to create a simple PDF and save in Google cloud storage.

// Create a byte array
        byte[] pdfBytes;
        // Load the credentials file
        GoogleCredential credential = GoogleCredential.FromFile("credentials.json");
        // Create a storage client
        StorageClient storage = StorageClient.Create(credential);
        // Download the PDF from Google Cloud Storage
        using (MemoryStream stream = new MemoryStream())
            storage.DownloadObject("bucket50247", "Sample.pdf", stream);
            pdfBytes = stream.ToArray();

        string filePath = "Sample.pdf";

        // Write the byte array to a PDF file
        File.WriteAllBytes(filePath, pdfBytes);

You can download a complete working sample from GitHub.