How to use google cloud storage in laravel to save the document instead of saving it locally using phpdocx

345 Views Asked by At

How to use Google Cloud Storage to save the document directly into the bucket instead of saving it locally and pushing it to the GCS using phpdocx?

I currently have this code below where I am saving it locally, but I want to directly save it in GCS instead.

$newDocx->searchAndReplace($docxFromPath, public_path() . '/storage/agreements/docx' . $fileName, 'Company Name', 'Apple', $options);

P.S. I am using laravel 10

1

There are 1 best solutions below

5
Naser.Sadeghi On

First you should install GCS on Laravel using the following composer command:

composer require superbalist/laravel-google-cloud-storage

Then you should create Google Cloud Storage Credentials by creating a service account in your Google Cloud project and downloading the JSON key file. Try to store this file securely in your Laravel application and preferably outside the public folder.

After that you should configure the Laravel filesystem using a code like below by editing config/filesystems.php

'disks' => [
    //.
    //.
    //.

    'gcs' => [
        'driver' => 'gcs',
        'project_id' => env('GCS_PROJECT_ID'),
        'key_file' => env('GCS_KEY_FILE'), // path to the JSON key
        'bucket' => env('GCS_BUCKET'),
        'path_prefix' => env('GCS_PATH_PREFIX', null), // optional: prefix for all object paths
        'storage_api_uri' => env('GCS_STORAGE_API_URI', null), // optional: public URL to serve direct file uploads
    ],
],

Make sure to set the relevant environment variables in your .env file.

Finally, you can use it like below in your code:

use Storage;

// Create the new document with phpdocx
$newDocx->searchAndReplace($docxFromPath, 'temporary-file.docx', 'Company Name', 'Apple', $options);

// Read the file into a variable
$fileContent = file_get_contents('temporary-file.docx');

// Save it to Google Cloud Storage
Storage::disk('gcs')->put('agreements/docx/'.$fileName, $fileContent);

// Optionally, delete the local temporary file
unlink('temporary-file.docx');

In this approach, the document is first saved to a local temporary file in your machine and then the contents of this file are read and uploaded to Google Cloud Storage.Finally, the local file is deleted.

Edit: After your last comment, I'm providing the following code:

First try installing the following package:

composer require google/cloud-storage

In your service provider or bootstrap file, add the following code to register the stream wrapper::

use Google\Cloud\Storage\StorageClient;
$storageClient = new StorageClient([
'keyFilePath' => storage_path('app/your-gcs-keyfile.json') //     Adjust path as needed
]);
$storageClient->registerStreamWrapper();

Instead of using a local path, use a GCS stream path. This requires understanding how stream wrappers work. For Google Cloud Storage, the path usually looks like

gs://your-bucket/your-path/file.docx.

Modify your phpdocx creation and saving process like so:

use Storage;

// Define your GCS path
$gcsPath = 'gs://'.env('GCS_BUCKET').'/agreements/docx/'.$fileName;

// Create and manipulate the document with phpdocx
$newDocx->searchAndReplace($docxFromPath, $gcsPath, 'Company Name', 'Apple', $options);

This way, you will skip saving to the local server.