1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gaufrette\Extras\Resolvable\Resolver; |
4
|
|
|
|
5
|
|
|
use Aws\S3\S3Client; |
6
|
|
|
use Gaufrette\Extras\Resolvable\ResolverInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Resolves object paths into time-limited URLs, namely presigned URLs. |
10
|
|
|
* |
11
|
|
|
* @author Albin Kerouanton <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @see http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURL.html |
14
|
|
|
*/ |
15
|
|
|
class AwsS3PresignedUrlResolver implements ResolverInterface |
16
|
|
|
{ |
17
|
|
|
/** @var S3Client */ |
18
|
|
|
private $service; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $bucket; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $baseDir; |
25
|
|
|
|
26
|
|
|
/** @var \DateTimeInterface */ |
27
|
|
|
private $expiresAt; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param S3Client $service Could be the same as the one given to the adapter or any other S3 client. |
31
|
|
|
* @param string $bucket Same as the one given to adapter. |
32
|
|
|
* @param string $baseDir Same as the one given to adapter. |
33
|
|
|
* @param \DateTimeInterface $expiresAt Presigned links are valid for a certain amount time of time only. |
34
|
|
|
*/ |
35
|
|
|
public function __construct(S3Client $service, $bucket, $baseDir, \DateTimeInterface $expiresAt) |
36
|
|
|
{ |
37
|
|
|
$this->service = $service; |
38
|
|
|
$this->bucket = $bucket; |
39
|
|
|
$this->baseDir = trim($baseDir, '/'); |
40
|
|
|
$this->expiresAt = $expiresAt; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Resolves given object path into presigned request URI. |
45
|
|
|
* |
46
|
|
|
* @param string $path |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function resolve($path) |
51
|
|
|
{ |
52
|
|
|
// For AWS SDK v2 |
53
|
|
|
if ($this->service instanceof \Aws\Common\Client\AbstractClient) { |
54
|
|
|
return $this->service->getObjectUrl( |
55
|
|
|
$this->bucket, |
56
|
|
|
$this->computePath($path), |
57
|
|
|
$this->expiresAt |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// For AWS SDK v3 |
62
|
|
|
$command = $this->service->getCommand('GetObject', [ |
63
|
|
|
'Bucket' => $this->bucket, |
64
|
|
|
'Key' => $this->computePath($path), |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
return (string) $this->service->createPresignedRequest($command, $this->expiresAt)->getUri(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Appends baseDir to $key. |
72
|
|
|
* |
73
|
|
|
* @param string $key |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
private function computePath($key) |
78
|
|
|
{ |
79
|
|
|
return ltrim($this->baseDir . '/' . ltrim($key, '/'), '/'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: