StreamWrapper
in package
Amazon S3 stream wrapper to use "s3://<bucket>/<key>" files with PHP streams, supporting "r", "w", "a", "x".
Supported stream related PHP functions:
- fopen, fclose, fread, fwrite, fseek, ftell, feof, fflush
- opendir, closedir, readdir, rewinddir
- copy, rename, unlink
- mkdir, rmdir, rmdir (recursive)
- file_get_contents, file_put_contents
- file_exists, filesize, is_file, is_dir
Opening "r" (read only) streams:
Read only streams are truly streaming by default and will not allow you to seek. This is because data read from the stream is not kept in memory or on the local filesystem. You can force a "r" stream to be seekable by setting the "seekable" stream context option true. This will allow true streaming of data from Amazon S3, but will maintain a buffer of previously read bytes in a 'php://temp' stream to allow seeking to previously read bytes from the stream.
You may pass any GetObject parameters as 's3' stream context options. These options will affect how the data is downloaded from Amazon S3.
Opening "w" and "x" (write only) streams:
Because Amazon S3 requires a Content-Length header, write only streams will maintain a 'php://temp' stream to buffer data written to the stream until the stream is flushed (usually by closing the stream with fclose).
You may pass any PutObject parameters as 's3' stream context options. These options will affect how the data is uploaded to Amazon S3.
When opening an "x" stream, the file must exist on Amazon S3 for the stream to open successfully.
Opening "a" (write only append) streams:
Similar to "w" streams, opening append streams requires that the data be buffered in a "php://temp" stream. Append streams will attempt to download the contents of an object in Amazon S3, seek to the end of the object, then allow you to append to the contents of the object. The data will then be uploaded using a PutObject operation when the stream is flushed (usually with fclose).
You may pass any GetObject and/or PutObject parameters as 's3' stream context options. These options will affect how the data is downloaded and uploaded from Amazon S3.
Stream context options:
- "seekable": Set to true to create a seekable "r" (read only) stream by using a php://temp stream buffer
- For "unlink" only: Any option that can be passed to the DeleteObject operation
Table of Contents
Properties
- $context : resource|null
- $body : EntityBody
- $client : S3Client
- $mode : string
- $nextStat : array<string|int, mixed>
- $objectIterator : ListObjectsIterator
- $openedBucket : string
- $openedBucketPrefix : string
- $params : array<string|int, mixed>
Methods
- dir_closedir() : bool
- Close the directory listing handles
- dir_opendir() : bool
- Support for opendir().
- dir_readdir() : string
- This method is called in response to readdir()
- dir_rewinddir() : bool
- This method is called in response to rewinddir()
- mkdir() : bool
- Support for mkdir().
- register() : mixed
- Register the 's3://' stream wrapper
- rename() : bool
- Called in response to rename() to rename a file or directory. Currently only supports renaming objects.
- rmdir() : bool
- Remove a bucket from Amazon S3
- stream_cast() : resource
- Cast the stream to return the underlying file resource
- stream_close() : mixed
- Close the stream
- stream_eof() : bool
- stream_flush() : bool
- stream_open() : bool
- stream_read() : string
- Read data from the underlying stream
- stream_seek() : bool
- Seek to a specific byte in the stream
- stream_stat() : array<string|int, mixed>
- stream_tell() : int
- Get the current position of the stream
- stream_write() : int
- Write data the to the stream
- unlink() : bool
- Delete a specific object
- url_stat() : array<string|int, mixed>
- Provides information for is_dir, is_file, filesize, etc. Works on buckets, keys, and prefixes
- clearStatInfo() : mixed
- Clear the next stat result from the cache
- formatUrlStat() : array<string|int, mixed>
- Prepare a url_stat result array
- getOption() : mixed|null
- Get a specific stream context option
- getOptions() : array<string|int, mixed>
- Get the stream context options available to the current stream
- getParams() : array<string|int, mixed>
- Get the bucket and key from the passed path (e.g. s3://bucket/key)
- getSignedRequest() : RequestInterface
- Serialize and sign a command, returning a request object
- openAppendStream() : bool
- Initialize the stream wrapper for an append stream
- openReadStream() : bool
- Initialize the stream wrapper for a read only stream
- openWriteStream() : bool
- Initialize the stream wrapper for a write only stream
- triggerError() : bool
- Trigger one or more errors
- createBucket() : bool
- Creates a bucket for the given parameters.
- createPseudoDirectory() : bool
- Creates a pseudo-folder by creating an empty "/" suffixed key
- determineAcl() : string
- Determine the most appropriate ACL based on a file mode.
Properties
$context
public
resource|null
$context
Stream context (this is set by PHP when a context is used)
$body
protected
EntityBody
$body
Underlying stream resource
$client
protected
static S3Client
$client
Client used to send requests
$mode
protected
string
$mode
Mode the stream was opened with
$nextStat
protected
static array<string|int, mixed>
$nextStat
= array()
The next key to retrieve when using a directory iterator. Helps for fast directory traversal.
$objectIterator
protected
ListObjectsIterator
$objectIterator
Iterator used with opendir() and subsequent readdir() calls
$openedBucket
protected
string
$openedBucket
The bucket that was opened when opendir() was called
$openedBucketPrefix
protected
string
$openedBucketPrefix
The prefix of the bucket that was opened with opendir()
$params
protected
array<string|int, mixed>
$params
Current parameters to use with the flush operation
Methods
dir_closedir()
Close the directory listing handles
public
dir_closedir() : bool
Return values
bool —true on success
dir_opendir()
Support for opendir().
public
dir_opendir(string $path, string $options) : bool
The opendir() method of the Amazon S3 stream wrapper supports a stream context option of "listFilter". listFilter must be a callable that accepts an associative array of object data and returns true if the object should be yielded when iterating the keys in a bucket.
Parameters
- $path : string
-
The path to the directory (e.g. "s3://dir[]")
- $options : string
-
Whether or not to enforce safe_mode (0x04). Unused.
Tags
Return values
bool —true on success
dir_readdir()
This method is called in response to readdir()
public
dir_readdir() : string
Tags
Return values
string —Should return a string representing the next filename, or false if there is no next file.
dir_rewinddir()
This method is called in response to rewinddir()
public
dir_rewinddir() : bool
Return values
bool —true on success
mkdir()
Support for mkdir().
public
mkdir(string $path, int $mode, int $options) : bool
Parameters
- $path : string
-
Directory which should be created.
- $mode : int
-
Permissions. 700-range permissions map to ACL_PUBLIC. 600-range permissions map to ACL_AUTH_READ. All other permissions map to ACL_PRIVATE. Expects octal form.
- $options : int
-
A bitwise mask of values, such as STREAM_MKDIR_RECURSIVE.
Tags
Return values
boolregister()
Register the 's3://' stream wrapper
public
static register(S3Client $client) : mixed
Parameters
- $client : S3Client
-
Client to use with the stream wrapper
rename()
Called in response to rename() to rename a file or directory. Currently only supports renaming objects.
public
rename(string $path_from, string $path_to) : bool
Parameters
- $path_from : string
-
the path to the file to rename
- $path_to : string
-
the new path to the file
Tags
Return values
bool —true if file was successfully renamed
rmdir()
Remove a bucket from Amazon S3
public
rmdir(string $path, int $options) : bool
Parameters
- $path : string
-
the directory path
- $options : int
-
A bitwise mask of values
Tags
Return values
bool —true if directory was successfully removed
stream_cast()
Cast the stream to return the underlying file resource
public
stream_cast(int $cast_as) : resource
Parameters
- $cast_as : int
-
STREAM_CAST_FOR_SELECT or STREAM_CAST_AS_STREAM
Return values
resourcestream_close()
Close the stream
public
stream_close() : mixed
stream_eof()
public
stream_eof() : bool
Return values
boolstream_flush()
public
stream_flush() : bool
Return values
boolstream_open()
public
stream_open(string $path, string $mode, int $options, string &$opened_path) : bool
Parameters
- $path : string
- $mode : string
- $options : int
- $opened_path : string
Return values
boolstream_read()
Read data from the underlying stream
public
stream_read(int $count) : string
Parameters
- $count : int
-
Amount of bytes to read
Return values
stringstream_seek()
Seek to a specific byte in the stream
public
stream_seek(int $offset[, int $whence = SEEK_SET ]) : bool
Parameters
- $offset : int
-
Seek offset
- $whence : int = SEEK_SET
-
Whence (SEEK_SET, SEEK_CUR, SEEK_END)
Return values
boolstream_stat()
public
stream_stat() : array<string|int, mixed>
Return values
array<string|int, mixed>stream_tell()
Get the current position of the stream
public
stream_tell() : int
Return values
int —Returns the current position in the stream
stream_write()
Write data the to the stream
public
stream_write(string $data) : int
Parameters
- $data : string
Return values
int —Returns the number of bytes written to the stream
unlink()
Delete a specific object
public
unlink(string $path) : bool
Parameters
- $path : string
Return values
boolurl_stat()
Provides information for is_dir, is_file, filesize, etc. Works on buckets, keys, and prefixes
public
url_stat(string $path, int $flags) : array<string|int, mixed>
Parameters
- $path : string
- $flags : int
Tags
Return values
array<string|int, mixed> —Returns an array of stat data
clearStatInfo()
Clear the next stat result from the cache
protected
clearStatInfo([string $path = null ]) : mixed
Parameters
- $path : string = null
-
If a path is specific, clearstatcache() will be called
formatUrlStat()
Prepare a url_stat result array
protected
formatUrlStat([string|array<string|int, mixed> $result = null ]) : array<string|int, mixed>
Parameters
- $result : string|array<string|int, mixed> = null
-
Data to add
Return values
array<string|int, mixed> —Returns the modified url_stat result
getOption()
Get a specific stream context option
protected
getOption(string $name) : mixed|null
Parameters
- $name : string
-
Name of the option to retrieve
Return values
mixed|nullgetOptions()
Get the stream context options available to the current stream
protected
getOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>getParams()
Get the bucket and key from the passed path (e.g. s3://bucket/key)
protected
getParams(string $path) : array<string|int, mixed>
Parameters
- $path : string
-
Path passed to the stream wrapper
Return values
array<string|int, mixed> —Hash of 'Bucket', 'Key', and custom params
getSignedRequest()
Serialize and sign a command, returning a request object
protected
getSignedRequest(CommandInterface $command) : RequestInterface
Parameters
- $command : CommandInterface
-
Command to sign
Return values
RequestInterfaceopenAppendStream()
Initialize the stream wrapper for an append stream
protected
openAppendStream(array<string|int, mixed> $params, array<string|int, mixed> &$errors) : bool
Parameters
- $params : array<string|int, mixed>
-
Operation parameters
- $errors : array<string|int, mixed>
-
Any encountered errors to append to
Return values
boolopenReadStream()
Initialize the stream wrapper for a read only stream
protected
openReadStream(array<string|int, mixed> $params, array<string|int, mixed> &$errors) : bool
Parameters
- $params : array<string|int, mixed>
-
Operation parameters
- $errors : array<string|int, mixed>
-
Any encountered errors to append to
Return values
boolopenWriteStream()
Initialize the stream wrapper for a write only stream
protected
openWriteStream(array<string|int, mixed> $params, array<string|int, mixed> &$errors) : bool
Parameters
- $params : array<string|int, mixed>
-
Operation parameters
- $errors : array<string|int, mixed>
-
Any encountered errors to append to
Return values
booltriggerError()
Trigger one or more errors
protected
triggerError(string|array<string|int, mixed> $errors[, mixed $flags = null ]) : bool
Parameters
- $errors : string|array<string|int, mixed>
-
Errors to trigger
- $flags : mixed = null
-
If set to STREAM_URL_STAT_QUIET, then no error or exception occurs
Tags
Return values
bool —Returns false
createBucket()
Creates a bucket for the given parameters.
private
createBucket(string $path, array<string|int, mixed> $params) : bool
Parameters
- $path : string
-
Stream wrapper path
- $params : array<string|int, mixed>
-
A result of StreamWrapper::getParams()
Return values
bool —Returns true on success or false on failure
createPseudoDirectory()
Creates a pseudo-folder by creating an empty "/" suffixed key
private
createPseudoDirectory(string $path, array<string|int, mixed> $params) : bool
Parameters
- $path : string
-
Stream wrapper path
- $params : array<string|int, mixed>
-
A result of StreamWrapper::getParams()
Return values
booldetermineAcl()
Determine the most appropriate ACL based on a file mode.
private
determineAcl(int $mode) : string
Parameters
- $mode : int
-
File mode