Skip to content

Commit 458ac64

Browse files
[AWS S3] Simple cache (#6299)
1 parent c662744 commit 458ac64

File tree

8 files changed

+455
-514
lines changed

8 files changed

+455
-514
lines changed

app/code/Magento/AwsS3/Driver/AwsS3.php

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\AwsS3\Driver;
99

1010
use Exception;
11-
use League\Flysystem\AwsS3v3\AwsS3Adapter;
11+
use League\Flysystem\AdapterInterface;
1212
use League\Flysystem\Config;
1313
use Magento\Framework\Exception\FileSystemException;
1414
use Magento\Framework\Filesystem\DriverInterface;
@@ -32,30 +32,38 @@ class AwsS3 implements RemoteDriverInterface
3232
private const CONFIG = ['ACL' => 'private'];
3333

3434
/**
35-
* @var AwsS3Adapter
35+
* @var AdapterInterface
3636
*/
3737
private $adapter;
3838

39+
/**
40+
* @var LoggerInterface
41+
*/
42+
private $logger;
43+
3944
/**
4045
* @var array
4146
*/
4247
private $streams = [];
4348

4449
/**
45-
* @var LoggerInterface
50+
* @var string
4651
*/
47-
private $logger;
52+
private $objectUrl;
4853

4954
/**
50-
* @param AwsS3Adapter $adapter
55+
* @param AdapterInterface $adapter
5156
* @param LoggerInterface $logger
57+
* @param string $objectUrl
5258
*/
5359
public function __construct(
54-
AwsS3Adapter $adapter,
55-
LoggerInterface $logger
60+
AdapterInterface $adapter,
61+
LoggerInterface $logger,
62+
string $objectUrl
5663
) {
5764
$this->adapter = $adapter;
5865
$this->logger = $logger;
66+
$this->objectUrl = $objectUrl;
5967
}
6068

6169
/**
@@ -282,26 +290,27 @@ public function getAbsolutePath($basePath, $path, $scheme = null)
282290
* @param string $path Relative path
283291
* @return string Absolute path
284292
*/
285-
private function normalizeAbsolutePath(string $path = '.'): string
293+
private function normalizeAbsolutePath(string $path = '/'): string
286294
{
287295
$path = ltrim($path, '/');
288-
$path = str_replace(
289-
$this->adapter->getClient()->getObjectUrl(
290-
$this->adapter->getBucket(),
291-
$this->adapter->applyPathPrefix('.')
292-
),
293-
'',
294-
$path
295-
);
296+
$path = str_replace($this->getObjectUrl(''), '', $path);
296297

297298
if (!$path) {
298-
$path = '.';
299+
$path = '/';
299300
}
300301

301-
return $this->adapter->getClient()->getObjectUrl(
302-
$this->adapter->getBucket(),
303-
$this->adapter->applyPathPrefix($path)
304-
);
302+
return $this->getObjectUrl($path);
303+
}
304+
305+
/**
306+
* Retrieves object URL from cache.
307+
*
308+
* @param string $path
309+
* @return string
310+
*/
311+
private function getObjectUrl(string $path): string
312+
{
313+
return $this->objectUrl . ltrim($path, '/');
305314
}
306315

307316
/**
@@ -312,11 +321,7 @@ private function normalizeAbsolutePath(string $path = '.'): string
312321
*/
313322
private function normalizeRelativePath(string $path): string
314323
{
315-
return str_replace(
316-
$this->normalizeAbsolutePath(),
317-
'',
318-
$path
319-
);
324+
return str_replace($this->normalizeAbsolutePath(), '', $path);
320325
}
321326

322327
/**

app/code/Magento/AwsS3/Driver/AwsS3Factory.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Aws\S3\S3Client;
1111
use League\Flysystem\AwsS3v3\AwsS3Adapter;
1212
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\RemoteStorage\Driver\DriverException;
1314
use Magento\RemoteStorage\Driver\DriverFactoryInterface;
1415
use Magento\RemoteStorage\Driver\RemoteDriverInterface;
1516

@@ -32,11 +33,7 @@ public function __construct(ObjectManagerInterface $objectManager)
3233
}
3334

3435
/**
35-
* Creates an instance of AWS S3 driver.
36-
*
37-
* @param array $config
38-
* @param string $prefix
39-
* @return RemoteDriverInterface
36+
* @inheritDoc
4037
*/
4138
public function create(array $config, string $prefix): RemoteDriverInterface
4239
{
@@ -46,17 +43,18 @@ public function create(array $config, string $prefix): RemoteDriverInterface
4643
unset($config['credentials']);
4744
}
4845

46+
if (empty($config['bucket']) || empty($config['region'])) {
47+
throw new DriverException(__('Bucket and region are required values'));
48+
}
49+
50+
$client = new S3Client($config);
51+
$adapter = new AwsS3Adapter($client, $config['bucket'], $prefix);
52+
4953
return $this->objectManager->create(
5054
AwsS3::class,
5155
[
52-
'adapter' => $this->objectManager->create(
53-
AwsS3Adapter::class,
54-
[
55-
'client' => $this->objectManager->create(S3Client::class, ['args' => $config]),
56-
'bucket' => $config['bucket'],
57-
'prefix' => $prefix
58-
]
59-
)
56+
'adapter' => $adapter,
57+
'objectUrl' => $client->getObjectUrl($adapter->getBucket(), $adapter->applyPathPrefix('.'))
6058
]
6159
);
6260
}

app/code/Magento/AwsS3/Test/Unit/Driver/AwsS3Test.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
namespace Magento\AwsS3\Test\Unit\Driver;
99

10-
use Aws\S3\S3ClientInterface;
1110
use League\Flysystem\AwsS3v3\AwsS3Adapter;
11+
use League\Flysystem\Cached\CachedAdapter;
1212
use Magento\AwsS3\Driver\AwsS3;
1313
use Magento\Framework\Exception\FileSystemException;
1414
use PHPUnit\Framework\MockObject\MockObject;
@@ -32,41 +32,15 @@ class AwsS3Test extends TestCase
3232
*/
3333
private $adapterMock;
3434

35-
/**
36-
* @var S3ClientInterface|MockObject
37-
*/
38-
private $clientMock;
39-
40-
/**
41-
* @var LoggerInterface
42-
*/
43-
private $logger;
44-
4535
/**
4636
* @inheritDoc
4737
*/
4838
protected function setUp(): void
4939
{
50-
$this->adapterMock = $this->createMock(AwsS3Adapter::class);
51-
$this->clientMock = $this->getMockForAbstractClass(S3ClientInterface::class);
52-
$this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
53-
54-
$this->adapterMock->method('applyPathPrefix')
55-
->willReturnArgument(0);
56-
$this->adapterMock->method('getBucket')
57-
->willReturn('test');
58-
$this->adapterMock->method('getClient')
59-
->willReturn($this->clientMock);
60-
$this->clientMock->method('getObjectUrl')
61-
->willReturnCallback(function (string $bucket, string $path) {
62-
if ($path === '.') {
63-
$path = '';
64-
}
65-
66-
return self::URL . $path;
67-
});
40+
$this->adapterMock = $this->createMock(CachedAdapter::class);
41+
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
6842

69-
$this->driver = new AwsS3($this->adapterMock, $this->logger);
43+
$this->driver = new AwsS3($this->adapterMock, $loggerMock, self::URL);
7044
}
7145

7246
/**

app/code/Magento/AwsS3/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"magento/framework": "^100.0.2",
1010
"magento/module-remote-storage": "*",
1111
"league/flysystem": "^1.0",
12-
"league/flysystem-aws-s3-v3": "^1.0"
12+
"league/flysystem-aws-s3-v3": "^1.0",
13+
"league/flysystem-cached-adapter": "^1.0"
1314
},
1415
"type": "magento2-module",
1516
"license": [

app/code/Magento/RemoteStorage/Driver/DriverFactoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ interface DriverFactoryInterface
1818
* @param array $config
1919
* @param string $prefix
2020
* @return RemoteDriverInterface
21+
*
22+
* @throws DriverException
2123
*/
2224
public function create(array $config, string $prefix): RemoteDriverInterface;
2325
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@
8282
"webonyx/graphql-php": "^0.13.8",
8383
"wikimedia/less.php": "~1.8.0",
8484
"league/flysystem": "^1.0",
85-
"league/flysystem-aws-s3-v3": "^1.0"
85+
"league/flysystem-aws-s3-v3": "^1.0",
86+
"league/flysystem-cached-adapter": "^1.0"
8687
},
8788
"require-dev": {
8889
"allure-framework/allure-phpunit": "~1.2.0",

0 commit comments

Comments
 (0)