Integrate Laravel with Google Cloud Storage for file upload, listing, downloading, and deleting


To integrate Laravel with Google Cloud Storage for file upload, listing, downloading, and deleting, you can follow these steps:

1. Install Required Packages

First, install the Google Cloud Storage package for Laravel. You can use google/cloud-storage and laravel/filesystem to handle cloud storage integration.

Run the following command in your Laravel project:

composer require google/cloud-storage

Then, install the league/flysystem-google-cloud-storage package, which provides an adapter for Laravel’s filesystem:

composer require league/flysystem-google-cloud-storage

2. Configure Google Cloud Storage

Next, configure your Google Cloud Storage credentials.

  1. Create a Google Cloud Storage Bucket if you don’t have one already.
  2. Generate a Service Account Key from Google Cloud Console:
    • Go to the Google Cloud Console.
    • Navigate to IAM & Admin > Service Accounts.
    • Create a new service account and assign it the Storage Object Admin role.
    • Download the generated JSON key file.
  3. Store the credentials in your Laravel project:
    • Put the JSON key file in a secure directory (e.g., storage/app/google).

3. Configure Filesystem in Laravel

Open config/filesystems.php and add a new disk for Google Cloud Storage under the disks array:

'disks' => [
    'google' => [
        'driver' => 'gcs',
        'project_id' => env('GOOGLE_CLOUD_PROJECT_ID'),
        'key_file' => env('GOOGLE_CLOUD_KEY_FILE'), // path to the service account key
        'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'),
        'path_prefix' => env('GOOGLE_CLOUD_PATH_PREFIX', null), // optional
        'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // optional
    ],
],

In your .env file, add the following configuration:

GOOGLE_CLOUD_PROJECT_ID=your-project-id
GOOGLE_CLOUD_KEY_FILE=/path/to/your/service-account-key.json
GOOGLE_CLOUD_STORAGE_BUCKET=your-bucket-name

4. Using Google Cloud Storage in Your Laravel Application

You can now use the Storage facade to interact with Google Cloud Storage.

Uploading a File

use Illuminate\Support\Facades\Storage;

$file = $request->file('file');  // Get the file from the request
$path = $file->store('uploads', 'google');  // Upload the file to Google Cloud Storage

return response()->json(['path' => $path]);

Listing Files

You can list all files in a specific directory:

$files = Storage::disk('google')->files('uploads');

return response()->json(['files' => $files]);

Downloading a File

You can download a file from Google Cloud Storage:

$path = 'uploads/filename.txt';  // The path of the file on Google Cloud Storage
$content = Storage::disk('google')->get($path);

// Optionally return as a download response
return response($content, 200)
    ->header('Content-Type', 'application/octet-stream')
    ->header('Content-Disposition', 'attachment; filename="filename.txt"');

Deleting a File

To delete a file from Google Cloud Storage:

$path = 'uploads/filename.txt';  // The path of the file to delete
Storage::disk('google')->delete($path);

return response()->json(['message' => 'File deleted successfully']);

5. Testing Your Integration

Once everything is set up, test the functionality by uploading, listing, downloading, and deleting files using Laravel's storage facade.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete