AwsS3V3Adapter.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Illuminate\Filesystem;
  3. use Aws\S3\S3Client;
  4. use Illuminate\Support\Traits\Conditionable;
  5. use League\Flysystem\FilesystemAdapter as FlysystemAdapter;
  6. use League\Flysystem\FilesystemOperator;
  7. class AwsS3V3Adapter extends FilesystemAdapter
  8. {
  9. use Conditionable;
  10. /**
  11. * The AWS S3 client.
  12. *
  13. * @var \Aws\S3\S3Client
  14. */
  15. protected $client;
  16. /**
  17. * Create a new AwsS3V3FilesystemAdapter instance.
  18. *
  19. * @param \League\Flysystem\FilesystemOperator $driver
  20. * @param \League\Flysystem\FilesystemAdapter $adapter
  21. * @param array $config
  22. * @param \Aws\S3\S3Client $client
  23. */
  24. public function __construct(FilesystemOperator $driver, FlysystemAdapter $adapter, array $config, S3Client $client)
  25. {
  26. $config['directory_separator'] = '/';
  27. parent::__construct($driver, $adapter, $config);
  28. $this->client = $client;
  29. }
  30. /**
  31. * Get the URL for the file at the given path.
  32. *
  33. * @param string $path
  34. * @return string
  35. *
  36. * @throws \RuntimeException
  37. */
  38. public function url($path)
  39. {
  40. // If an explicit base URL has been set on the disk configuration then we will use
  41. // it as the base URL instead of the default path. This allows the developer to
  42. // have full control over the base path for this filesystem's generated URLs.
  43. if (isset($this->config['url'])) {
  44. return $this->concatPathToUrl($this->config['url'], $this->prefixer->prefixPath($path));
  45. }
  46. return $this->client->getObjectUrl(
  47. $this->config['bucket'], $this->prefixer->prefixPath($path)
  48. );
  49. }
  50. /**
  51. * Determine if temporary URLs can be generated.
  52. *
  53. * @return bool
  54. */
  55. public function providesTemporaryUrls()
  56. {
  57. return true;
  58. }
  59. /**
  60. * Get a temporary URL for the file at the given path.
  61. *
  62. * @param string $path
  63. * @param \DateTimeInterface $expiration
  64. * @param array $options
  65. * @return string
  66. */
  67. public function temporaryUrl($path, $expiration, array $options = [])
  68. {
  69. $command = $this->client->getCommand('GetObject', array_merge([
  70. 'Bucket' => $this->config['bucket'],
  71. 'Key' => $this->prefixer->prefixPath($path),
  72. ], $options));
  73. $uri = $this->client->createPresignedRequest(
  74. $command, $expiration, $options
  75. )->getUri();
  76. // If an explicit base URL has been set on the disk configuration then we will use
  77. // it as the base URL instead of the default path. This allows the developer to
  78. // have full control over the base path for this filesystem's generated URLs.
  79. if (isset($this->config['temporary_url'])) {
  80. $uri = $this->replaceBaseUrl($uri, $this->config['temporary_url']);
  81. }
  82. return (string) $uri;
  83. }
  84. /**
  85. * Get a temporary upload URL for the file at the given path.
  86. *
  87. * @param string $path
  88. * @param \DateTimeInterface $expiration
  89. * @param array $options
  90. * @return array
  91. */
  92. public function temporaryUploadUrl($path, $expiration, array $options = [])
  93. {
  94. $command = $this->client->getCommand('PutObject', array_merge([
  95. 'Bucket' => $this->config['bucket'],
  96. 'Key' => $this->prefixer->prefixPath($path),
  97. ], $options));
  98. $signedRequest = $this->client->createPresignedRequest(
  99. $command, $expiration, $options
  100. );
  101. $uri = $signedRequest->getUri();
  102. // If an explicit base URL has been set on the disk configuration then we will use
  103. // it as the base URL instead of the default path. This allows the developer to
  104. // have full control over the base path for this filesystem's generated URLs.
  105. if (isset($this->config['temporary_url'])) {
  106. $uri = $this->replaceBaseUrl($uri, $this->config['temporary_url']);
  107. }
  108. return [
  109. 'url' => (string) $uri,
  110. 'headers' => $signedRequest->getHeaders(),
  111. ];
  112. }
  113. /**
  114. * Get the underlying S3 client.
  115. *
  116. * @return \Aws\S3\S3Client
  117. */
  118. public function getClient()
  119. {
  120. return $this->client;
  121. }
  122. }