StreamWrapper.php 30 KB

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