webalternatif / flysystem-openstack-swift
Flysystem v3 adapter for OpenStack Swift
Installs: 2 342
Dependents: 3
Suggesters: 1
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: 8.1.* || 8.2.* || 8.3.* || 8.4.*
- league/flysystem: ^3.0
- php-opencloud/openstack: ^3.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- league/flysystem-adapter-test-utilities: ^3.0
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.19.0
- vimeo/psalm: ^6.5
README
A Flysystem v3 adapter for OpenStack Swift, using
php-opencloud/openstack
.
If you're looking for a Flysystem v1 adapter, see
chrisnharvey/flysystem-openstack-swift
.
Installation
$ composer require webalternatif/flysystem-openstack-swift
Usage
use League\Flysystem\Filesystem; use OpenStack\OpenStack; use Webf\Flysystem\OpenStackSwift\OpenStackSwiftAdapter; $openstack = new OpenStack([ 'authUrl' => '{authUrl}', 'region' => '{region}', 'user' => [ 'id' => '{userId}', 'password' => '{password}', ], 'scope' => ['project' => ['id' => '{projectId}']], ]); $adapter = new OpenStackSwiftAdapter($openstack, '{containerName}'); $flysystem = new Filesystem($adapter);
Uploading large objects
To use the createLargeObject
method of the underlying OpenStack library to
upload large objects (which is mandatory for files over 5 GB), you must use
the writeStream
method and define the segment_size
config option.
The segment_container
option is also available if you want to upload segments
in another container.
Example
use Webf\Flysystem\OpenStackSwift\Config; $flysystem->writeStream($path, $content, ([ Config::OPTION_SEGMENT_SIZE => 52428800, // 50 MiB Config::OPTION_SEGMENT_CONTAINER => 'test_segments', ]);
Generating temporary URLs
This adapter supports generating temporary URLs as described in Flysystem's documentation.
To do so, you must :
- set a secret key at the account or container level of your OpenStack Swift instance (see details in the OpenStack documentation),
- provide this secret key as third argument (
$tempUrlKey
) when creating the adapter.
Available options
When calling Filesystem::temporaryUrl()
, you can pass the following options as
third argument ($config
):
Option key | Description | Type | Default value |
---|---|---|---|
digest |
The digest algorithm to use for the HMAC cryptographic signature (given as first parameter of hash_hmac). | string |
'sha256' |
file_name |
A string to override the default file name (which is based on the object name) when the file is downloaded. | string |
null |
prefix |
If true , a prefix-based temporary URL will be generated. |
bool |
false |
Those option keys are available as public constants in the
Webf\Flysystem\OpenStackSwift\Config
class.
More information about those options can be found in the OpenStack documentation.
Example
use League\Flysystem\Filesystem; use Webf\Flysystem\OpenStackSwift\OpenStackSwiftAdapter; // ... (see above) $adapter = new OpenStackSwiftAdapter($openstack, '{containerName}', '{tempUrlKey}'); $flysystem = new Filesystem($adapter); $flysystem->temporaryUrl($path, new DateTime('+1 hour'), [ // options... ]);
Tests
This library uses the FilesystemAdapterTestCase
provided by
league/flysystem-adapter-test-utilities
, so it performs integration tests
that need a real OpenStack Swift container.
To run tests, duplicate the phpunit.xml.dist
file into phpunit.xml
and fill
all the environment variables, then run:
$ composer test
This will run Psalm and PHPUnit, but you can run them individually like this:
$ composer psalm $ composer phpunit