hedii/laravel-ovh-swift-storage

Out of the box Ovh Swift storage usage for Laravel

4.2.0 2021-04-08 07:46 UTC

This package is auto-updated.

Last update: 2024-11-11 16:01:35 UTC


README

Build Status Total Downloads License Latest Stable Version

Laravel Ovh Swift Storage

Out of the box Ovh Swift storage usage for Laravel 6.0+

Table of contents

Installation

Install via composer

composer require hedii/laravel-ovh-swift-storage

Edit config/filesystems.php to add the new ovh-swift disk

return [

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

        'ovh-swift' => [
            'driver' => 'ovh-swift',
            'authUrl' => env('OVH_SWIFT_OPENSTACK_AUTH_URL', 'https://auth.cloud.ovh.net/v3/'),
            'region' => env('OVH_SWIFT_OPENSTACK_REGION'),
            'projectId' => env('OVH_SWIFT_OPENSTACK_PROJECT_ID'),
            'containerName' => env('OVH_SWIFT_CONTAINER_NAME'),
            'prefix' => env('OVH_SWIFT_PREFIX'),
            'username' => env('OVH_SWIFT_OPENSTACK_USERNAME'),
            'password' => env('OVH_SWIFT_OPENSTACK_PASSWORD'),
            'visibility' => env('OVH_SWIFT_VISIBILITY', 'public'),
            'publicUrl' => env('OVH_SWIFT_PUBLIC_URL'),
            'urlKey' => env('OVH_SWIFT_URL_KEY'),
            'requestOptions' => [],
        ],
    
    ],

];

Edit .env to add the required environment variables

OVH_SWIFT_OPENSTACK_REGION=GRA
OVH_SWIFT_OPENSTACK_PROJECT_ID=xxxxxxxxxxxxxxxxxxx
OVH_SWIFT_CONTAINER_NAME=xxxxxxxxxxxxxxxxxxx
OVH_SWIFT_OPENSTACK_USERNAME=xxxxxxxxxxxxxxxxxxx
OVH_SWIFT_OPENSTACK_PASSWORD=xxxxxxxxxxxxxxxxxxx

Usage

Once you have modified the Laravel filesystem configuration and the environment variables, you can use the new Ovh Swift storage disk as any Laravel storage disk.

Private containers

By default, this package assumes you are using a public Object Storage container.

If you want to use a private container with temporary urls, you have to configure a temporary url key and set the visibility to private.

OVH_SWIFT_OPENSTACK_REGION=GRA
OVH_SWIFT_OPENSTACK_PROJECT_ID=xxxxxxxxxxxxxxxxxxx
OVH_SWIFT_CONTAINER_NAME=xxxxxxxxxxxxxxxxxxx
OVH_SWIFT_OPENSTACK_USERNAME=xxxxxxxxxxxxxxxxxxx
OVH_SWIFT_OPENSTACK_PASSWORD=xxxxxxxxxxxxxxxxxxx
OVH_SWIFT_VISIBILITY=private
OVH_SWIFT_URL_KEY=xxxxxxxxxxxxxxxxxxx

Be aware you will not be able to retrieve regular urls with a private container, only temporary urls.

Request options

If you want to use http request options like timeout, connect_timeout or any other valid option, put them in the driver configuration.

return [

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

        'ovh-swift' => [
            /* ... */
            'requestOptions' => [
                'timeout' => 3.14,
                'connect_timeout' => 3.14,
            ],
        ],
    
    ],

];

Example

use Illuminate\Support\Facades\Storage;

Storage::disk('ovh-swift')->put('avatars/1', $fileContents);

$url = Storage::url('avatars/1');

// if using private containers:
$temporaryUrl = Storage::temporaryUrl('avatars/1', now()->addMinute());

Testing

If you want to test the package, you must create a new Openstack user and two new Ovh Object Storage containers:

  • A public container named test
  • A private container named test-private

Once it's done, copy phpunit.xml.dist to phpunit.xml and update the environment variables.

Be aware that the test suite will delete all files in the containers after each test. Do not test against a production containers!

To start test suite, run this command:

composer test

License

laravel-ovh-swift-storage is released under the MIT Licence. See the bundled LICENSE file for details.