StreamWrapper.php 31 KB

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