Skip to content

Commit 1425a52

Browse files
committed
feat: add Azure OSS adapter
Fix: #178
1 parent 4587fe0 commit 1425a52

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ to interact with your storage.
104104
[AsyncAws S3](https://github.com/thephpleague/flysystem-bundle/blob/master/docs/2-cloud-storage-providers.md#asyncaws-s3),
105105
[AWS SDK S3](https://github.com/thephpleague/flysystem-bundle/blob/master/docs/2-cloud-storage-providers.md#aws-sdk-s3),
106106
[Azure](https://github.com/thephpleague/flysystem-bundle/blob/master/docs/2-cloud-storage-providers.md#azure),
107+
[Azure OSS](https://github.com/thephpleague/flysystem-bundle/blob/master/docs/2-cloud-storage-providers.md#azure-oss),
107108
[Google Cloud Storage](https://github.com/thephpleague/flysystem-bundle/blob/master/docs/2-cloud-storage-providers.md#google-cloud-storage),
108109
[DigitalOcean Spaces](https://github.com/thephpleague/flysystem-bundle/blob/master/docs/2-cloud-storage-providers.md#digitalocean-spaces),
109110
[Scaleway Object Storage](https://github.com/thephpleague/flysystem-bundle/blob/master/docs/2-cloud-storage-providers.md#scaleway-object-storage)

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"symfony/options-resolver": "^5.4 || ^6.0 || ^7.0"
3636
},
3737
"require-dev": {
38+
"azure-oss/storage-blob-flysystem": "^1.2",
3839
"doctrine/mongodb-odm": "^2.0",
3940
"league/flysystem-async-aws-s3": "^3.1",
4041
"league/flysystem-aws-s3-v3": "^3.1",

docs/2-cloud-storage-providers.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ including many cloud storage providers. This bundle provides the same level of s
55
cloud providers by providing corresponding adapters in the configuration.
66

77
* [Azure](#azure)
8+
* [Azure OSS](#azure-oss)
89
* [AsyncAws S3](#asyncaws-s3)
910
* [AWS S3](#aws-sdk-s3)
1011
* [DigitalOcean Spaces](#digitalocean-spaces)
@@ -35,6 +36,36 @@ flysystem:
3536
prefix: 'optional/path/prefix'
3637
```
3738
39+
## Azure OSS
40+
41+
### Installation
42+
43+
```
44+
composer require azure-oss/storage-blob-flysystem
45+
```
46+
47+
### Usage
48+
49+
```yaml
50+
# config/packages/flysystem.yaml
51+
52+
services:
53+
azure_blob_storage_client:
54+
class: 'AzureOss\Storage\Blob\BlobServiceClient'
55+
factory: ['AzureOss\Storage\Blob\BlobServiceClient', 'fromConnectionString']
56+
arguments:
57+
- '%env(AZURE_BLOB_STORAGE_CONNECTION_STRING)%'
58+
59+
flysystem:
60+
storages:
61+
users.storage:
62+
adapter: 'azureoss'
63+
options:
64+
client: 'azure_blob_storage_client'
65+
container: 'container_name'
66+
prefix: 'optional/path/prefix'
67+
```
68+
3869
## AsyncAws S3
3970
4071
### Installation

src/Adapter/AdapterDefinitionFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct()
3232
new Builder\AsyncAwsAdapterDefinitionBuilder(),
3333
new Builder\AwsAdapterDefinitionBuilder(),
3434
new Builder\AzureAdapterDefinitionBuilder(),
35+
new Builder\AzureOssAdapterDefinitionBuilder(),
3536
new Builder\FtpAdapterDefinitionBuilder(),
3637
new Builder\GcloudAdapterDefinitionBuilder(),
3738
new Builder\GridFSAdapterDefinitionBuilder(),
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the flysystem-bundle project.
5+
*
6+
* (c) Titouan Galopin <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace League\FlysystemBundle\Adapter\Builder;
13+
14+
use AzureOss\FlysystemAzureBlobStorage\AzureBlobStorageAdapter;
15+
use AzureOss\Storage\Blob\BlobContainerClient;
16+
use AzureOss\Storage\Blob\BlobServiceClient;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\OptionsResolver\OptionsResolver;
20+
21+
/**
22+
* @author Titouan Galopin <[email protected]>
23+
*
24+
* @internal
25+
*/
26+
final class AzureOssAdapterDefinitionBuilder extends AbstractAdapterDefinitionBuilder
27+
{
28+
public function getName(): string
29+
{
30+
return 'azureoss';
31+
}
32+
33+
protected function getRequiredPackages(): array
34+
{
35+
return [
36+
AzureBlobStorageAdapter::class => 'azure-oss/storage-blob-flysystem',
37+
];
38+
}
39+
40+
protected function configureOptions(OptionsResolver $resolver): void
41+
{
42+
$resolver->setRequired('client');
43+
$resolver->setAllowedTypes('client', 'string');
44+
45+
$resolver->setRequired('container');
46+
$resolver->setAllowedTypes('container', 'string');
47+
48+
$resolver->setDefault('prefix', '');
49+
$resolver->setAllowedTypes('prefix', 'string');
50+
}
51+
52+
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
53+
{
54+
$containerClient = new Definition(BlobContainerClient::class);
55+
$containerClient->setFactory([self::class, 'initializeBlobContainerClient']);
56+
$containerClient->setArguments([
57+
new Reference($options['client']),
58+
$options['container'],
59+
]);
60+
61+
$definition->setClass(AzureBlobStorageAdapter::class);
62+
$definition->setArgument(0, $containerClient);
63+
$definition->setArgument(1, $options['prefix']);
64+
}
65+
66+
public static function initializeBlobContainerClient(BlobServiceClient $blobServiceClient, string $container): BlobContainerClient
67+
{
68+
return $blobServiceClient->getContainerClient($container);
69+
}
70+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the flysystem-bundle project.
5+
*
6+
* (c) Titouan Galopin <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\League\FlysystemBundle\Adapter\Builder;
13+
14+
use AzureOss\FlysystemAzureBlobStorage\AzureBlobStorageAdapter;
15+
use League\FlysystemBundle\Adapter\Builder\AzureOssAdapterDefinitionBuilder;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
19+
class AzureOssAdapterDefinitionBuilderTest extends TestCase
20+
{
21+
public function createBuilder(): AzureOssAdapterDefinitionBuilder
22+
{
23+
return new AzureOssAdapterDefinitionBuilder();
24+
}
25+
26+
public function provideValidOptions(): \Generator
27+
{
28+
yield 'minimal' => [[
29+
'client' => 'my_client',
30+
'container' => 'container_name',
31+
]];
32+
33+
yield 'prefix' => [[
34+
'client' => 'my_client',
35+
'container' => 'container_name',
36+
'prefix' => 'prefix/path',
37+
]];
38+
}
39+
40+
/**
41+
* @dataProvider provideValidOptions
42+
*/
43+
public function testCreateDefinition($options): void
44+
{
45+
$this->assertSame(AzureBlobStorageAdapter::class, $this->createBuilder()->createDefinition($options, null)->getClass());
46+
}
47+
48+
public function testOptionsBehavior(): void
49+
{
50+
$definition = $this->createBuilder()->createDefinition([
51+
'client' => 'my_client',
52+
'container' => 'container_name',
53+
'prefix' => 'prefix/path',
54+
], null);
55+
56+
$this->assertSame(AzureBlobStorageAdapter::class, $definition->getClass());
57+
$this->assertInstanceOf(Definition::class, $definition->getArgument(0));
58+
$this->assertSame('prefix/path', $definition->getArgument(1));
59+
}
60+
}

0 commit comments

Comments
 (0)