This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
AzureStorageServiceProvider.php
135 lines (122 loc) · 4.25 KB
/
AzureStorageServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Matthewbdaly\LaravelAzureStorage;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Cache;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Cached\CacheInterface;
use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\Cached\Storage\Memory as MemoryStore;
use League\Flysystem\Filesystem;
use Matthewbdaly\LaravelAzureStorage\Exceptions\CacheAdapterNotInstalled;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\Middlewares\RetryMiddleware;
use MicrosoftAzure\Storage\Common\Middlewares\RetryMiddlewareFactory;
/**
* Service provider for Azure Blob Storage
*/
final class AzureStorageServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
Storage::extend('azure', function (Application $app, array $config) {
$client = $app->make(BlobRestProxy::class, $config);
$adapter = new AzureBlobStorageAdapter(
$client,
$config['container'],
$config['key'] ?? null,
$config['url'] ?? null,
$config['prefix'] ?? null
);
if ($cache = Arr::pull($config, 'cache')) {
if (!class_exists(CachedAdapter::class)) {
throw new CacheAdapterNotInstalled("Caching requires the league/flysystem-cached-adapter to be installed.");
}
$adapter = new CachedAdapter($adapter, $this->createCacheStore($cache));
}
return new Filesystem($adapter, $config);
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind(BlobRestProxy::class, function (Application $app, array $config) {
$config = empty($config) ? $app->make('config')->get('filesystems.disks.azure') : $config;
if (array_key_exists('sasToken', $config)) {
$endpoint = sprintf(
'BlobEndpoint=%s;SharedAccessSignature=%s;',
$config['endpoint'],
$config['sasToken']
);
} else {
$endpoint = sprintf(
'DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;',
$config['name'],
$config['key']
);
if (isset($config['endpoint'])) {
$endpoint .= sprintf("BlobEndpoint=%s;", $config['endpoint']);
}
}
$blobOptions = [];
$retry = data_get($config, 'retry');
if (isset($retry)) {
$blobOptions = [
'middlewares' => [
$this->createRetryMiddleware($retry)
]
];
}
return BlobRestProxy::createBlobService($endpoint, $blobOptions);
});
}
/**
* Create a cache store instance.
*
* @param mixed $config
*
* @return \League\Flysystem\Cached\CacheInterface
*
* @throws \InvalidArgumentException
*/
protected function createCacheStore($config): CacheInterface
{
if ($config === true) {
return new MemoryStore();
}
return new Cache(
$this->app['cache']->store($config['store']),
$config['prefix'] ?? 'flysystem',
$config['expire'] ?? null
);
}
/**
* Create retry middleware instance.
*
* @param array $config
*
* @return RetryMiddleware
*/
protected function createRetryMiddleware(array $config): RetryMiddleware
{
return RetryMiddlewareFactory::create(
RetryMiddlewareFactory::GENERAL_RETRY_TYPE,
$config['tries'] ?? 3,
$config['interval'] ?? 1000,
$config['increase'] === 'exponential' ?
RetryMiddlewareFactory::EXPONENTIAL_INTERVAL_ACCUMULATION :
RetryMiddlewareFactory::LINEAR_INTERVAL_ACCUMULATION,
true // Whether to retry connection failures too, default false
);
}
}