Custom Laravel FileSystem Provider for Oracle Object Storage
We will create Custom Laravel Provider for Oracle Object Storage FileSystem.
Overview
What is Laravel?
Laravel is a web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
What is Oracle Object Storage?
The Oracle Cloud Infrastructure Object Storage service is an internet-scale, high-performance storage platform that offers reliable and cost-efficient data durability. The Object Storage service can store an unlimited amount of unstructured data of any content type, including analytic data and rich content, like images and videos.
Prerequisite
Make sure that you have the following before continuing the post
- Basic Laravel knowledge
- Composer installed.
- Oracle Cloud Infrastructure account (they offer free account).
# First step (Setup)
Create new Laravel project
If you don’t have Laravel project already created. Please create a one by running the following command.
composer create-project — prefer-dist laravel/laravel oci_os
Get Oracle Object Storage credentials
We will go and get the credentials to be used later in order to connect our Laravel app with Object Storage.
Go to your account page, from the top right menu.
Then, go to “Customer Secret Keys”.
Click on “Generate Secret Key” button, then enter any name to identify your secrets.
Copy the “Generated Key” that provided, it will not be shown again after you close the tap.
Then close the tap and copy the associate “Access Key”.
Save both the “Generated Key”, and “Access Key” in a file and keep them safe.
# Step Two (Environment values)
We will need to Install AWS S3 driver. As Oracle Object Storage is compatible with AWS S3 API.
Simply run the following command in your Laravel project.
composer require league/flysystem-aws-s3-v3 ~1.0
Update your .env file add the following keys, and fill them with your values
FILESYSTEM_DRIVER=oci
OCI_ACCESS_KEY_ID=
OCI_SECRET_ACCESS_KEY=
OCI_DEFAULT_REGION=
OCI_BUCKET=
OCI_URL=https://{{Namespace}}.compat.objectstorage.{{region}}.oraclecloud.com
Add in OCI_ACCESS_KEY_ID, OCI_SECRET_ACCESS_KEY the values we got from step one
Get the Namespace from your bucket details page as bellow
Please don’t forget FILESYSTEM_DRIVER key. as it is important to customize the driver at run time.
# Step Three (Custom Provider)
First update config/filesystems.php, add the following values
'disks' => [
...
'oci' => [
'driver' => 's3',
'key' => env('OCI_ACCESS_KEY_ID'),
'secret' => env('OCI_SECRET_ACCESS_KEY'),
'region' => env('OCI_DEFAULT_REGION'),
'bucket' => env('OCI_BUCKET'),
'url' => env('OCI_URL') . '/'. env('OCI_BUCKET'),
],
]
Then run the following command in your project to create a provider, and name it OciObjectStorageServiceProvider
php artisan make:provider OciObjectStorageServiceProvider
Now update the created file as follow
use Aws\S3\S3Client;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
use Storage;
...
public function boot()
{
if (config('filesystems.default') != 'oci') {
return;
}
Storage::extend('s3', function($app, $config) {
$client = new S3Client([
'credentials' => [
'key' => $config['key'],
'secret' => $config['secret'],
],
'region' => $config['region'],
'version' => '2006-03-01',
'bucket_endpoint' => true,
'endpoint' => $config['url']
]);
return new Filesystem(new AwsS3Adapter($client, $config['bucket'], $config['bucket']));
});
}
And Then, add the newly created provider in config/app.php
'providers' => [
...
App\Providers\OciObjectStorageServiceProvider::class,
...
],
Now you can use Oracle Object Storage in your project.
Make sure that you set the right values in you environments values, and that FILESYSTEM_DRIVER is set to oci
I hope you enjoyed it.