StreamWrapper.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. <?php
  2. namespace Aws\S3;
  3. use Aws\CacheInterface;
  4. use Aws\LruArrayCache;
  5. use Aws\Result;
  6. use Aws\S3\Exception\S3Exception;
  7. use GuzzleHttp\Psr7;
  8. use GuzzleHttp\Psr7\Stream;
  9. use GuzzleHttp\Psr7\CachingStream;
  10. use Psr\Http\Message\StreamInterface;
  11. /**
  12. * Amazon S3 stream wrapper to use "s3://<bucket>/<key>" files with PHP
  13. * streams, supporting "r", "w", "a", "x".
  14. *
  15. * # Opening "r" (read only) streams:
  16. *
  17. * Read only streams are truly streaming by default and will not allow you to
  18. * seek. This is because data read from the stream is not kept in memory or on
  19. * the local filesystem. You can force a "r" stream to be seekable by setting
  20. * the "seekable" stream context option true. This will allow true streaming of
  21. * data from Amazon S3, but will maintain a buffer of previously read bytes in
  22. * a 'php://temp' stream to allow seeking to previously read bytes from the
  23. * stream.
  24. *
  25. * You may pass any GetObject parameters as 's3' stream context options. These
  26. * options will affect how the data is downloaded from Amazon S3.
  27. *
  28. * # Opening "w" and "x" (write only) streams:
  29. *
  30. * Because Amazon S3 requires a Content-Length header, write only streams will
  31. * maintain a 'php://temp' stream to buffer data written to the stream until
  32. * the stream is flushed (usually by closing the stream with fclose).
  33. *
  34. * You may pass any PutObject parameters as 's3' stream context options. These
  35. * options will affect how the data is uploaded to Amazon S3.
  36. *
  37. * When opening an "x" stream, the file must exist on Amazon S3 for the stream
  38. * to open successfully.
  39. *
  40. * # Opening "a" (write only append) streams:
  41. *
  42. * Similar to "w" streams, opening append streams requires that the data be
  43. * buffered in a "php://temp" stream. Append streams will attempt to download
  44. * the contents of an object in Amazon S3, seek to the end of the object, then
  45. * allow you to append to the contents of the object. The data will then be
  46. * uploaded using a PutObject operation when the stream is flushed (usually
  47. * with fclose).
  48. *
  49. * You may pass any GetObject and/or PutObject parameters as 's3' stream
  50. * context options. These options will affect how the data is downloaded and
  51. * uploaded from Amazon S3.
  52. *
  53. * Stream context options:
  54. *
  55. * - "seekable": Set to true to create a seekable "r" (read only) stream by
  56. * using a php://temp stream buffer
  57. * - For "unlink" only: Any option that can be passed to the DeleteObject
  58. * operation
  59. */
  60. class StreamWrapper
  61. {
  62. /** @var resource|null Stream context (this is set by PHP) */
  63. public $context;
  64. /** @var StreamInterface Underlying stream resource */
  65. private $body;
  66. /** @var int Size of the body that is opened */
  67. private $size;
  68. /** @var array Hash of opened stream parameters */
  69. private $params = [];
  70. /** @var string Mode in which the stream was opened */
  71. private $mode;
  72. /** @var \Iterator Iterator used with opendir() related calls */
  73. private $objectIterator;
  74. /** @var string The bucket that was opened when opendir() was called */
  75. private $openedBucket;
  76. /** @var string The prefix of the bucket that was opened with opendir() */
  77. private $openedBucketPrefix;
  78. /** @var string Opened bucket path */
  79. private $openedPath;
  80. /** @var CacheInterface Cache for object and dir lookups */
  81. private $cache;
  82. /** @var string The opened protocol (e.g., "s3") */
  83. private $protocol = 's3';
  84. /**
  85. * Register the 's3://' stream wrapper
  86. *
  87. * @param S3ClientInterface $client Client to use with the stream wrapper
  88. * @param string $protocol Protocol to register as.
  89. * @param CacheInterface $cache Default cache for the protocol.
  90. */
  91. public static function register(
  92. S3ClientInterface $client,
  93. $protocol = 's3',
  94. CacheInterface $cache = null
  95. ) {
  96. if (in_array($protocol, stream_get_wrappers())) {
  97. stream_wrapper_unregister($protocol);
  98. }
  99. // Set the client passed in as the default stream context client
  100. stream_wrapper_register($protocol, get_called_class(), STREAM_IS_URL);
  101. $default = stream_context_get_options(stream_context_get_default());
  102. $default[$protocol]['client'] = $client;
  103. if ($cache) {
  104. $default[$protocol]['cache'] = $cache;
  105. } elseif (!isset($default[$protocol]['cache'])) {
  106. // Set a default cache adapter.
  107. $default[$protocol]['cache'] = new LruArrayCache();
  108. }
  109. stream_context_set_default($default);
  110. }
  111. public function stream_close()
  112. {
  113. $this->body = $this->cache = null;
  114. }
  115. public function stream_open($path, $mode, $options, &$opened_path)
  116. {
  117. $this->initProtocol($path);
  118. $this->params = $this->getBucketKey($path);
  119. $this->mode = rtrim($mode, 'bt');
  120. if ($errors = $this->validate($path, $this->mode)) {
  121. return $this->triggerError($errors);
  122. }
  123. return $this->boolCall(function() use ($path) {
  124. switch ($this->mode) {
  125. case 'r': return $this->openReadStream($path);
  126. case 'a': return $this->openAppendStream($path);
  127. default: return $this->openWriteStream($path);
  128. }
  129. });
  130. }
  131. public function stream_eof()
  132. {
  133. return $this->body->eof();
  134. }
  135. public function stream_flush()
  136. {
  137. if ($this->mode == 'r') {
  138. return false;
  139. }
  140. if ($this->body->isSeekable()) {
  141. $this->body->seek(0);
  142. }
  143. $params = $this->getOptions(true);
  144. $params['Body'] = $this->body;
  145. // Attempt to guess the ContentType of the upload based on the
  146. // file extension of the key
  147. if (!isset($params['ContentType']) &&
  148. ($type = Psr7\mimetype_from_filename($params['Key']))
  149. ) {
  150. $params['ContentType'] = $type;
  151. }
  152. $this->clearCacheKey("s3://{$params['Bucket']}/{$params['Key']}");
  153. return $this->boolCall(function () use ($params) {
  154. return (bool) $this->getClient()->putObject($params);
  155. });
  156. }
  157. public function stream_read($count)
  158. {
  159. return $this->body->read($count);
  160. }
  161. public function stream_seek($offset, $whence = SEEK_SET)
  162. {
  163. return !$this->body->isSeekable()
  164. ? false
  165. : $this->boolCall(function () use ($offset, $whence) {
  166. $this->body->seek($offset, $whence);
  167. return true;
  168. });
  169. }
  170. public function stream_tell()
  171. {
  172. return $this->boolCall(function() { return $this->body->tell(); });
  173. }
  174. public function stream_write($data)
  175. {
  176. return $this->body->write($data);
  177. }
  178. public function unlink($path)
  179. {
  180. $this->initProtocol($path);
  181. return $this->boolCall(function () use ($path) {
  182. $this->clearCacheKey($path);
  183. $this->getClient()->deleteObject($this->withPath($path));
  184. return true;
  185. });
  186. }
  187. public function stream_stat()
  188. {
  189. $stat = $this->getStatTemplate();
  190. $stat[7] = $stat['size'] = $this->getSize();
  191. $stat[2] = $stat['mode'] = $this->mode;
  192. return $stat;
  193. }
  194. /**
  195. * Provides information for is_dir, is_file, filesize, etc. Works on
  196. * buckets, keys, and prefixes.
  197. * @link http://www.php.net/manual/en/streamwrapper.url-stat.php
  198. */
  199. public function url_stat($path, $flags)
  200. {
  201. $this->initProtocol($path);
  202. // Some paths come through as S3:// for some reason.
  203. $split = explode('://', $path);
  204. $path = strtolower($split[0]) . '://' . $split[1];
  205. // Check if this path is in the url_stat cache
  206. if ($value = $this->getCacheStorage()->get($path)) {
  207. return $value;
  208. }
  209. $stat = $this->createStat($path, $flags);
  210. if (is_array($stat)) {
  211. $this->getCacheStorage()->set($path, $stat);
  212. }
  213. return $stat;
  214. }
  215. /**
  216. * Parse the protocol out of the given path.
  217. *
  218. * @param $path
  219. */
  220. private function initProtocol($path)
  221. {
  222. $parts = explode('://', $path, 2);
  223. $this->protocol = $parts[0] ?: 's3';
  224. }
  225. private function createStat($path, $flags)
  226. {
  227. $this->initProtocol($path);
  228. $parts = $this->withPath($path);
  229. if (!$parts['Key']) {
  230. return $this->statDirectory($parts, $path, $flags);
  231. }
  232. return $this->boolCall(function () use ($parts, $path) {
  233. try {
  234. $result = $this->getClient()->headObject($parts);
  235. if (substr($parts['Key'], -1, 1) == '/' &&
  236. $result['ContentLength'] == 0
  237. ) {
  238. // Return as if it is a bucket to account for console
  239. // bucket objects (e.g., zero-byte object "foo/")
  240. return $this->formatUrlStat($path);
  241. } else {
  242. // Attempt to stat and cache regular object
  243. return $this->formatUrlStat($result->toArray());
  244. }
  245. } catch (S3Exception $e) {
  246. // Maybe this isn't an actual key, but a prefix. Do a prefix
  247. // listing of objects to determine.
  248. $result = $this->getClient()->listObjects([
  249. 'Bucket' => $parts['Bucket'],
  250. 'Prefix' => rtrim($parts['Key'], '/') . '/',
  251. 'MaxKeys' => 1
  252. ]);
  253. if (!$result['Contents'] && !$result['CommonPrefixes']) {
  254. throw new \Exception("File or directory not found: $path");
  255. }
  256. return $this->formatUrlStat($path);
  257. }
  258. }, $flags);
  259. }
  260. private function statDirectory($parts, $path, $flags)
  261. {
  262. // Stat "directories": buckets, or "s3://"
  263. if (!$parts['Bucket'] ||
  264. $this->getClient()->doesBucketExist($parts['Bucket'])
  265. ) {
  266. return $this->formatUrlStat($path);
  267. }
  268. return $this->triggerError("File or directory not found: $path", $flags);
  269. }
  270. /**
  271. * Support for mkdir().
  272. *
  273. * @param string $path Directory which should be created.
  274. * @param int $mode Permissions. 700-range permissions map to
  275. * ACL_PUBLIC. 600-range permissions map to
  276. * ACL_AUTH_READ. All other permissions map to
  277. * ACL_PRIVATE. Expects octal form.
  278. * @param int $options A bitwise mask of values, such as
  279. * STREAM_MKDIR_RECURSIVE.
  280. *
  281. * @return bool
  282. * @link http://www.php.net/manual/en/streamwrapper.mkdir.php
  283. */
  284. public function mkdir($path, $mode, $options)
  285. {
  286. $this->initProtocol($path);
  287. $params = $this->withPath($path);
  288. $this->clearCacheKey($path);
  289. if (!$params['Bucket']) {
  290. return false;
  291. }
  292. if (!isset($params['ACL'])) {
  293. $params['ACL'] = $this->determineAcl($mode);
  294. }
  295. return empty($params['Key'])
  296. ? $this->createBucket($path, $params)
  297. : $this->createSubfolder($path, $params);
  298. }
  299. public function rmdir($path, $options)
  300. {
  301. $this->initProtocol($path);
  302. $this->clearCacheKey($path);
  303. $params = $this->withPath($path);
  304. $client = $this->getClient();
  305. if (!$params['Bucket']) {
  306. return $this->triggerError('You must specify a bucket');
  307. }
  308. return $this->boolCall(function () use ($params, $path, $client) {
  309. if (!$params['Key']) {
  310. $client->deleteBucket(['Bucket' => $params['Bucket']]);
  311. return true;
  312. }
  313. return $this->deleteSubfolder($path, $params);
  314. });
  315. }
  316. /**
  317. * Support for opendir().
  318. *
  319. * The opendir() method of the Amazon S3 stream wrapper supports a stream
  320. * context option of "listFilter". listFilter must be a callable that
  321. * accepts an associative array of object data and returns true if the
  322. * object should be yielded when iterating the keys in a bucket.
  323. *
  324. * @param string $path The path to the directory
  325. * (e.g. "s3://dir[</prefix>]")
  326. * @param string $options Unused option variable
  327. *
  328. * @return bool true on success
  329. * @see http://www.php.net/manual/en/function.opendir.php
  330. */
  331. public function dir_opendir($path, $options)
  332. {
  333. $this->initProtocol($path);
  334. $this->openedPath = $path;
  335. $params = $this->withPath($path);
  336. $delimiter = $this->getOption('delimiter');
  337. /** @var callable $filterFn */
  338. $filterFn = $this->getOption('listFilter');
  339. $op = ['Bucket' => $params['Bucket']];
  340. $this->openedBucket = $params['Bucket'];
  341. if ($delimiter === null) {
  342. $delimiter = '/';
  343. }
  344. if ($delimiter) {
  345. $op['Delimiter'] = $delimiter;
  346. }
  347. if ($params['Key']) {
  348. $params['Key'] = rtrim($params['Key'], $delimiter) . $delimiter;
  349. $op['Prefix'] = $params['Key'];
  350. }
  351. $this->openedBucketPrefix = $params['Key'];
  352. // Filter our "/" keys added by the console as directories, and ensure
  353. // that if a filter function is provided that it passes the filter.
  354. $this->objectIterator = \Aws\flatmap(
  355. $this->getClient()->getPaginator('ListObjects', $op),
  356. function (Result $result) use ($filterFn) {
  357. $contentsAndPrefixes = $result->search('[Contents[], CommonPrefixes[]][]');
  358. // Filter out dir place holder keys and use the filter fn.
  359. return array_filter(
  360. $contentsAndPrefixes,
  361. function ($key) use ($filterFn) {
  362. return (!$filterFn || call_user_func($filterFn, $key))
  363. && (!isset($key['Key']) || substr($key['Key'], -1, 1) !== '/');
  364. }
  365. );
  366. }
  367. );
  368. return true;
  369. }
  370. /**
  371. * Close the directory listing handles
  372. *
  373. * @return bool true on success
  374. */
  375. public function dir_closedir()
  376. {
  377. $this->objectIterator = null;
  378. gc_collect_cycles();
  379. return true;
  380. }
  381. /**
  382. * This method is called in response to rewinddir()
  383. *
  384. * @return boolean true on success
  385. */
  386. public function dir_rewinddir()
  387. {
  388. $this->boolCall(function() {
  389. $this->objectIterator = null;
  390. $this->dir_opendir($this->openedPath, null);
  391. return true;
  392. });
  393. }
  394. /**
  395. * This method is called in response to readdir()
  396. *
  397. * @return string Should return a string representing the next filename, or
  398. * false if there is no next file.
  399. * @link http://www.php.net/manual/en/function.readdir.php
  400. */
  401. public function dir_readdir()
  402. {
  403. // Skip empty result keys
  404. if (!$this->objectIterator->valid()) {
  405. return false;
  406. }
  407. // First we need to create a cache key. This key is the full path to
  408. // then object in s3: protocol://bucket/key.
  409. // Next we need to create a result value. The result value is the
  410. // current value of the iterator without the opened bucket prefix to
  411. // emulate how readdir() works on directories.
  412. // The cache key and result value will depend on if this is a prefix
  413. // or a key.
  414. $cur = $this->objectIterator->current();
  415. if (isset($cur['Prefix'])) {
  416. // Include "directories". Be sure to strip a trailing "/"
  417. // on prefixes.
  418. $result = rtrim($cur['Prefix'], '/');
  419. $key = $this->formatKey($result);
  420. $stat = $this->formatUrlStat($key);
  421. } else {
  422. $result = $cur['Key'];
  423. $key = $this->formatKey($cur['Key']);
  424. $stat = $this->formatUrlStat($cur);
  425. }
  426. // Cache the object data for quick url_stat lookups used with
  427. // RecursiveDirectoryIterator.
  428. $this->getCacheStorage()->set($key, $stat);
  429. $this->objectIterator->next();
  430. // Remove the prefix from the result to emulate other stream wrappers.
  431. return $this->openedBucketPrefix
  432. ? substr($result, strlen($this->openedBucketPrefix))
  433. : $result;
  434. }
  435. private function formatKey($key)
  436. {
  437. $protocol = explode('://', $this->openedPath)[0];
  438. return "{$protocol}://{$this->openedBucket}/{$key}";
  439. }
  440. /**
  441. * Called in response to rename() to rename a file or directory. Currently
  442. * only supports renaming objects.
  443. *
  444. * @param string $path_from the path to the file to rename
  445. * @param string $path_to the new path to the file
  446. *
  447. * @return bool true if file was successfully renamed
  448. * @link http://www.php.net/manual/en/function.rename.php
  449. */
  450. public function rename($path_from, $path_to)
  451. {
  452. // PHP will not allow rename across wrapper types, so we can safely
  453. // assume $path_from and $path_to have the same protocol
  454. $this->initProtocol($path_from);
  455. $partsFrom = $this->withPath($path_from);
  456. $partsTo = $this->withPath($path_to);
  457. $this->clearCacheKey($path_from);
  458. $this->clearCacheKey($path_to);
  459. if (!$partsFrom['Key'] || !$partsTo['Key']) {
  460. return $this->triggerError('The Amazon S3 stream wrapper only '
  461. . 'supports copying objects');
  462. }
  463. return $this->boolCall(function () use ($partsFrom, $partsTo) {
  464. $options = $this->getOptions(true);
  465. // Copy the object and allow overriding default parameters if
  466. // desired, but by default copy metadata
  467. $this->getClient()->copy(
  468. $partsFrom['Bucket'],
  469. $partsFrom['Key'],
  470. $partsTo['Bucket'],
  471. $partsTo['Key'],
  472. isset($options['acl']) ? $options['acl'] : 'private',
  473. $options
  474. );
  475. // Delete the original object
  476. $this->getClient()->deleteObject([
  477. 'Bucket' => $partsFrom['Bucket'],
  478. 'Key' => $partsFrom['Key']
  479. ] + $options);
  480. return true;
  481. });
  482. }
  483. public function stream_cast($cast_as)
  484. {
  485. return false;
  486. }
  487. /**
  488. * Validates the provided stream arguments for fopen and returns an array
  489. * of errors.
  490. */
  491. private function validate($path, $mode)
  492. {
  493. $errors = [];
  494. if (!$this->getOption('Key')) {
  495. $errors[] = 'Cannot open a bucket. You must specify a path in the '
  496. . 'form of s3://bucket/key';
  497. }
  498. if (!in_array($mode, ['r', 'w', 'a', 'x'])) {
  499. $errors[] = "Mode not supported: {$mode}. "
  500. . "Use one 'r', 'w', 'a', or 'x'.";
  501. }
  502. // When using mode "x" validate if the file exists before attempting
  503. // to read
  504. if ($mode == 'x' &&
  505. $this->getClient()->doesObjectExist(
  506. $this->getOption('Bucket'),
  507. $this->getOption('Key'),
  508. $this->getOptions(true)
  509. )
  510. ) {
  511. $errors[] = "{$path} already exists on Amazon S3";
  512. }
  513. return $errors;
  514. }
  515. /**
  516. * Get the stream context options available to the current stream
  517. *
  518. * @param bool $removeContextData Set to true to remove contextual kvp's
  519. * like 'client' from the result.
  520. *
  521. * @return array
  522. */
  523. private function getOptions($removeContextData = false)
  524. {
  525. // Context is not set when doing things like stat
  526. if ($this->context === null) {
  527. $options = [];
  528. } else {
  529. $options = stream_context_get_options($this->context);
  530. $options = isset($options[$this->protocol])
  531. ? $options[$this->protocol]
  532. : [];
  533. }
  534. $default = stream_context_get_options(stream_context_get_default());
  535. $default = isset($default[$this->protocol])
  536. ? $default[$this->protocol]
  537. : [];
  538. $result = $this->params + $options + $default;
  539. if ($removeContextData) {
  540. unset($result['client'], $result['seekable'], $result['cache']);
  541. }
  542. return $result;
  543. }
  544. /**
  545. * Get a specific stream context option
  546. *
  547. * @param string $name Name of the option to retrieve
  548. *
  549. * @return mixed|null
  550. */
  551. private function getOption($name)
  552. {
  553. $options = $this->getOptions();
  554. return isset($options[$name]) ? $options[$name] : null;
  555. }
  556. /**
  557. * Gets the client from the stream context
  558. *
  559. * @return S3ClientInterface
  560. * @throws \RuntimeException if no client has been configured
  561. */
  562. private function getClient()
  563. {
  564. if (!$client = $this->getOption('client')) {
  565. throw new \RuntimeException('No client in stream context');
  566. }
  567. return $client;
  568. }
  569. private function getBucketKey($path)
  570. {
  571. // Remove the protocol
  572. $parts = explode('://', $path);
  573. // Get the bucket, key
  574. $parts = explode('/', $parts[1], 2);
  575. return [
  576. 'Bucket' => $parts[0],
  577. 'Key' => isset($parts[1]) ? $parts[1] : null
  578. ];
  579. }
  580. /**
  581. * Get the bucket and key from the passed path (e.g. s3://bucket/key)
  582. *
  583. * @param string $path Path passed to the stream wrapper
  584. *
  585. * @return array Hash of 'Bucket', 'Key', and custom params from the context
  586. */
  587. private function withPath($path)
  588. {
  589. $params = $this->getOptions(true);
  590. return $this->getBucketKey($path) + $params;
  591. }
  592. private function openReadStream()
  593. {
  594. $client = $this->getClient();
  595. $command = $client->getCommand('GetObject', $this->getOptions(true));
  596. $command['@http']['stream'] = true;
  597. $result = $client->execute($command);
  598. $this->size = $result['ContentLength'];
  599. $this->body = $result['Body'];
  600. // Wrap the body in a caching entity body if seeking is allowed
  601. if ($this->getOption('seekable') && !$this->body->isSeekable()) {
  602. $this->body = new CachingStream($this->body);
  603. }
  604. return true;
  605. }
  606. private function openWriteStream()
  607. {
  608. $this->body = new Stream(fopen('php://temp', 'r+'));
  609. return true;
  610. }
  611. private function openAppendStream()
  612. {
  613. try {
  614. // Get the body of the object and seek to the end of the stream
  615. $client = $this->getClient();
  616. $this->body = $client->getObject($this->getOptions(true))['Body'];
  617. $this->body->seek(0, SEEK_END);
  618. return true;
  619. } catch (S3Exception $e) {
  620. // The object does not exist, so use a simple write stream
  621. return $this->openWriteStream();
  622. }
  623. }
  624. /**
  625. * Trigger one or more errors
  626. *
  627. * @param string|array $errors Errors to trigger
  628. * @param mixed $flags If set to STREAM_URL_STAT_QUIET, then no
  629. * error or exception occurs
  630. *
  631. * @return bool Returns false
  632. * @throws \RuntimeException if throw_errors is true
  633. */
  634. private function triggerError($errors, $flags = null)
  635. {
  636. // This is triggered with things like file_exists()
  637. if ($flags & STREAM_URL_STAT_QUIET) {
  638. return $flags & STREAM_URL_STAT_LINK
  639. // This is triggered for things like is_link()
  640. ? $this->formatUrlStat(false)
  641. : false;
  642. }
  643. // This is triggered when doing things like lstat() or stat()
  644. trigger_error(implode("\n", (array) $errors), E_USER_WARNING);
  645. return false;
  646. }
  647. /**
  648. * Prepare a url_stat result array
  649. *
  650. * @param string|array $result Data to add
  651. *
  652. * @return array Returns the modified url_stat result
  653. */
  654. private function formatUrlStat($result = null)
  655. {
  656. $stat = $this->getStatTemplate();
  657. switch (gettype($result)) {
  658. case 'NULL':
  659. case 'string':
  660. // Directory with 0777 access - see "man 2 stat".
  661. $stat['mode'] = $stat[2] = 0040777;
  662. break;
  663. case 'array':
  664. // Regular file with 0777 access - see "man 2 stat".
  665. $stat['mode'] = $stat[2] = 0100777;
  666. // Pluck the content-length if available.
  667. if (isset($result['ContentLength'])) {
  668. $stat['size'] = $stat[7] = $result['ContentLength'];
  669. } elseif (isset($result['Size'])) {
  670. $stat['size'] = $stat[7] = $result['Size'];
  671. }
  672. if (isset($result['LastModified'])) {
  673. // ListObjects or HeadObject result
  674. $stat['mtime'] = $stat[9] = $stat['ctime'] = $stat[10]
  675. = strtotime($result['LastModified']);
  676. }
  677. }
  678. return $stat;
  679. }
  680. /**
  681. * Creates a bucket for the given parameters.
  682. *
  683. * @param string $path Stream wrapper path
  684. * @param array $params A result of StreamWrapper::withPath()
  685. *
  686. * @return bool Returns true on success or false on failure
  687. */
  688. private function createBucket($path, array $params)
  689. {
  690. if ($this->getClient()->doesBucketExist($params['Bucket'])) {
  691. return $this->triggerError("Bucket already exists: {$path}");
  692. }
  693. return $this->boolCall(function () use ($params, $path) {
  694. $this->getClient()->createBucket($params);
  695. $this->clearCacheKey($path);
  696. return true;
  697. });
  698. }
  699. /**
  700. * Creates a pseudo-folder by creating an empty "/" suffixed key
  701. *
  702. * @param string $path Stream wrapper path
  703. * @param array $params A result of StreamWrapper::withPath()
  704. *
  705. * @return bool
  706. */
  707. private function createSubfolder($path, array $params)
  708. {
  709. // Ensure the path ends in "/" and the body is empty.
  710. $params['Key'] = rtrim($params['Key'], '/') . '/';
  711. $params['Body'] = '';
  712. // Fail if this pseudo directory key already exists
  713. if ($this->getClient()->doesObjectExist(
  714. $params['Bucket'],
  715. $params['Key'])
  716. ) {
  717. return $this->triggerError("Subfolder already exists: {$path}");
  718. }
  719. return $this->boolCall(function () use ($params, $path) {
  720. $this->getClient()->putObject($params);
  721. $this->clearCacheKey($path);
  722. return true;
  723. });
  724. }
  725. /**
  726. * Deletes a nested subfolder if it is empty.
  727. *
  728. * @param string $path Path that is being deleted (e.g., 's3://a/b/c')
  729. * @param array $params A result of StreamWrapper::withPath()
  730. *
  731. * @return bool
  732. */
  733. private function deleteSubfolder($path, $params)
  734. {
  735. // Use a key that adds a trailing slash if needed.
  736. $prefix = rtrim($params['Key'], '/') . '/';
  737. $result = $this->getClient()->listObjects([
  738. 'Bucket' => $params['Bucket'],
  739. 'Prefix' => $prefix,
  740. 'MaxKeys' => 1
  741. ]);
  742. // Check if the bucket contains keys other than the placeholder
  743. if ($contents = $result['Contents']) {
  744. return (count($contents) > 1 || $contents[0]['Key'] != $prefix)
  745. ? $this->triggerError('Subfolder is not empty')
  746. : $this->unlink(rtrim($path, '/') . '/');
  747. }
  748. return $result['CommonPrefixes']
  749. ? $this->triggerError('Subfolder contains nested folders')
  750. : true;
  751. }
  752. /**
  753. * Determine the most appropriate ACL based on a file mode.
  754. *
  755. * @param int $mode File mode
  756. *
  757. * @return string
  758. */
  759. private function determineAcl($mode)
  760. {
  761. switch (substr(decoct($mode), 0, 1)) {
  762. case '7': return 'public-read';
  763. case '6': return 'authenticated-read';
  764. default: return 'private';
  765. }
  766. }
  767. /**
  768. * Gets a URL stat template with default values
  769. *
  770. * @return array
  771. */
  772. private function getStatTemplate()
  773. {
  774. return [
  775. 0 => 0, 'dev' => 0,
  776. 1 => 0, 'ino' => 0,
  777. 2 => 0, 'mode' => 0,
  778. 3 => 0, 'nlink' => 0,
  779. 4 => 0, 'uid' => 0,
  780. 5 => 0, 'gid' => 0,
  781. 6 => -1, 'rdev' => -1,
  782. 7 => 0, 'size' => 0,
  783. 8 => 0, 'atime' => 0,
  784. 9 => 0, 'mtime' => 0,
  785. 10 => 0, 'ctime' => 0,
  786. 11 => -1, 'blksize' => -1,
  787. 12 => -1, 'blocks' => -1,
  788. ];
  789. }
  790. /**
  791. * Invokes a callable and triggers an error if an exception occurs while
  792. * calling the function.
  793. *
  794. * @param callable $fn
  795. * @param int $flags
  796. *
  797. * @return bool
  798. */
  799. private function boolCall(callable $fn, $flags = null)
  800. {
  801. try {
  802. return $fn();
  803. } catch (\Exception $e) {
  804. return $this->triggerError($e->getMessage(), $flags);
  805. }
  806. }
  807. /**
  808. * @return LruArrayCache
  809. */
  810. private function getCacheStorage()
  811. {
  812. if (!$this->cache) {
  813. $this->cache = $this->getOption('cache') ?: new LruArrayCache();
  814. }
  815. return $this->cache;
  816. }
  817. /**
  818. * Clears a specific stat cache value from the stat cache and LRU cache.
  819. *
  820. * @param string $key S3 path (s3://bucket/key).
  821. */
  822. private function clearCacheKey($key)
  823. {
  824. clearstatcache(true, $key);
  825. $this->getCacheStorage()->remove($key);
  826. }
  827. /**
  828. * Returns the size of the opened object body.
  829. *
  830. * @return int|null
  831. */
  832. private function getSize()
  833. {
  834. $size = $this->body->getSize();
  835. return $size !== null ? $size : $this->size;
  836. }
  837. }