Laravel 9 google cloud Uploading image file
In this post we will give you information about Laravel 9 google cloud Uploading image file . Hear we will give you detail about Laravel 9 google cloud Uploading image file And how to use it also give you demo for it if it is necessary.
Google Cloud Storage for Laravel 9 google cloud Uploading image file
GCS (Google Cloud Storage) is an object storage in the cloud.
With GCS, I don’t need to care about how much space left in the server and images not available when GAE (Google App Engine) scales up the instances.
Prerequisites for Laravel 9 google cloud Uploading image file
Before you getting started, you need to prepare
- Create a project in the Google Cloud Platform Console.
- Install the Google Cloud SDK.
- Install Postman (this software is used for testing, optional to install)
Free GCS bucket for GAE
GCS provides a free bucket (allow to store up to 5GB) for each GAE application. You can a bucket created with your application domain name under GCS browser.
Laravel 9 google cloud Uploading image fileInstall GCS in Laravel
Next, install GCS as a custom file system in your Laravel 9 application. Superbalist released a handy package as a GCS adapter for flysystem.
The first step, install the package.
// Terminal
$ composer require superbalist/laravel-google-cloud-storage
Register the service provider.
// config/app.php
'providers' => [
...
/*
* Package Service Providers...
*/
Superbalist\LaravelGoogleCloudStorage\GoogleCloudStorageServiceProvider::class,
...
],
Next step, add a new disk to the filesystem.
// config/filesystems.php
'disks' => [
...
'gcs' => [
'driver' => 'gcs',
'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket
'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
],
],
There are 5 environment variables you can set:
GOOGLE_CLOUD_PROJECT_ID
– your GCP project IDGOOGLE_CLOUD_KEY_FILE
– your service account, you don’t need to set this now because GAE has built-in credential ready. Check out more from hereGOOGLE_CLOUD_STORAGE_BUCKET
– your bucket name, in my case, it is running-laravel-on-gcp.appspot.comGOOGLE_CLOUD_STORAGE_PATH_PREFIX
– your image path/folder in the bucketGOOGLE_CLOUD_STORAGE_API_URI
– your custom domain, you don’t need to set this now. Check out more from here
The last step in the installation, set the variables in app.yaml
.
// app.yaml
# Put production environment variables here.
env_variables:
...
FILESYSTEM_DRIVER: gcs
GOOGLE_CLOUD_PROJECT_ID: running-laravel-on-gcp
GOOGLE_CLOUD_STORAGE_BUCKET: running-laravel-on-gcp.appspot.com
Once the installation is done, you create an endpoint for image uploading.
Create an endpoint for image upload
Create a new controller named UserAvatarController
.
// Terminal
$ php artisan make:controller UserAvatarController
In UserAvatarController
, create a method named update
which stores the uploaded image file to GCS bucket into the path/folder named avatars
.
// app/Http/Controllers/UserAvatarController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserAvatarController extends Controller
{
/**
* Update the avatar for the user.
*
* @param Request $request
* @return Response
*/
public function update(Request $request)
{
$path = $request->file('avatar')->store('avatars');
return $path;
}
}
Don’t forget to enable the endpoint to the route file.
// routes/web.php
Route::post('/updateAvatar', 'UserAvatarController@update’);
In this case, you aren’t going to build a form to test the upload. If you are, you can skip this step.
The upload test will be running via Postman which cannot generate the CSRF token automatically. Therefore, you exclude the /updateAvatar
from the VerifyCsrfToken
Middleware.
// app/Http/Middleware/VerifyCsrfToken.php
class VerifyCsrfToken extends Middleware
{
...
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'updateAvatar'
];
}
Deploy
Deploy the application to the GAE.
// Terminal
$ gcloud app deploy
Test forLaravel 9 google cloud Uploading image file
Once the deployment is completed, run the test now.
You can use Postman or another method to test the application. Now, you are going to use Postman.
From the Postman,
- Create a new POST request
- Set the domain and the endpoint
- Name a field data as
avatar
and set the type asFile
- Select an image
- Click Send
After clicking the send button, the endpoint should return the file path and the uploaded image file name.
Tada!
Laravel 9 google cloud Uploading image fileYou can find the uploaded image file through GCP console.
Laravel 9 google cloud Uploading image fileConclusion for Laravel 9 google cloud Uploading image file
Hope this code and post will helped you for implement Laravel 9 Yajra Datatables Example Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us.