HPCloud-PHP
1.2.0
PHP bindings for HPCloud and OpenStack services.
|
Provides stream wrapping for Swift. More...
Public Member Functions | |
dir_closedir () | |
Close a directory. | |
dir_opendir ($path, $options) | |
Open a directory for reading. | |
dir_readdir () | |
Read an entry from the directory. | |
dir_rewinddir () | |
Rewind to the beginning of the listing. | |
rename ($path_from, $path_to) | |
Rename a swift object. | |
stream_cast ($cast_as) | |
Cast stream into a lower-level stream. | |
stream_close () | |
Close a stream, writing if necessary. | |
stream_eof () | |
Check whether the stream has reached its end. | |
stream_flush () | |
Initiate saving data on the remote object storage. | |
stream_open ($path, $mode, $options, &$opened_path) | |
Open a stream resource. | |
stream_read ($count) | |
Read N bytes from the stream. | |
stream_seek ($offset, $whence) | |
Perform a seek. | |
stream_set_option ($option, $arg1, $arg2) | |
Set options on the underlying stream. | |
stream_stat () | |
Perform stat()/lstat() operations. | |
stream_tell () | |
Get the current position in the stream. | |
stream_write ($data) | |
Write data to stream. | |
unlink ($path) | |
Unlink a file. | |
url_stat ($path, $flags) | |
object () | |
Get the Object. | |
objectStorage () | |
EXPERT: Get the ObjectStorage for this wrapper. | |
token () | |
EXPERT: Get the auth token for this wrapper. | |
serviceCatalog () | |
EXPERT: Get the service catalog (IdentityServices) for this wrapper. |
Public Attributes | |
const | DEFAULT_SCHEME = 'swift' |
$context | |
The stream context. |
Protected Member Functions | |
writeRemote () | |
Write data to the remote object storage. | |
generateStat ($object, $container, $size) | |
Generate a reasonably accurate STAT array. | |
setMode ($mode) | |
Set the fopen mode. | |
cxt ($name, $default=NULL) | |
Get an item out of the context. | |
parseUrl ($url) | |
Parse a URL. | |
initializeObjectStorage () | |
Based on the context, initialize the ObjectStorage. | |
initializeCDN ($token, $catalog) | |
Initialize CDN service. | |
authenticate () |
Protected Attributes | |
$contextArray = array() | |
$schemeName = self::DEFAULT_SCHEME | |
$authToken | |
$isBinary = FALSE | |
$isText = TRUE | |
$isWriting = FALSE | |
$isReading = FALSE | |
$isTruncating = FALSE | |
$isAppending = FALSE | |
$noOverwrite = FALSE | |
$createIfNotFound = TRUE | |
$isNeverDirty = FALSE | |
If this is TRUE, no data is ever sent to the remote server. | |
$triggerErrors = FALSE | |
$isDirty = FALSE | |
Indicate whether the local differs from remote. | |
$store | |
Object storage instance. | |
$container | |
The Container. | |
$obj | |
The Object. | |
$objStream | |
The IO stream for the Object. | |
$dirListing = array() | |
Directory listing. | |
$dirIndex = 0 | |
$dirPrefix = '' |
Static Protected Attributes | |
static | $serviceCatalogCache = array() |
Cache of auth token -> service catalog. |
Provides stream wrapping for Swift.
This provides a full stream wrapper to expose swift://
URLs to the PHP stream system.
Swift streams provide authenticated and priviledged access to the swift data store. These URLs are not generally used for granting unauthenticated access to files (which can be done using the HTTP stream wrapper – no need for swift-specific logic).
URL Structure
This takes URLs of the following form:
swift://CONTAINER/FILE
Example:
swift://public/example.txt
The example above would access the public
container and attempt to retrieve the file named example.txt
.
Slashes are legal in Swift filenames, so a pathlike URL can be constructed like this:
swift://public/path/like/file/name.txt
The above would attempt to find a file in object storage named path/like/file/name.txt
.
A note on UTF-8 and URLs: PHP does not yet natively support many UTF-8 characters in URLs. Thus, you ought to rawurlencode() your container name and object name (path) if there is any possibility that it will contain UTF-8 characters.
Locking
This library does not support locking (e.g. flock()). This is because the OpenStack Object Storage implementation does not support locking. But there are a few related things you should keep in mind:
Usage
The principle purpose of this wrapper is to make it easy to access and manipulate objects on a remote object storage instance. Managing containers is a secondary concern (and can often better be managed using the HPCloud API). Consequently, almost all actions done through the stream wrapper are focused on objects, not containers, servers, etc.
Retrieving an Existing Object
Retrieving an object is done by opening a file handle to that object.
Writing an Object
Nothing is written to the remote storage until the file is closed. This keeps network traffic at a minimum, and respects the more-or-less stateless nature of ObjectStorage.
USING FILE/STREAM RESOURCES
In general, you should access files like this:
USING FILE-LEVEL FUNCTIONS
PHP provides a number of file-level functions that stream wrappers can optionally support. Here are a few such functions:
The HPCloud stream wrapper provides support for these file-level functions. But there are a few things you should know:
DIRECTORIES
OpenStack Swift does not really have directories. Rather, it allows characters such as '/' to be used to designate namespaces on object names. (For simplicity, this library uses only '/' as a separator).
This allows for simulated directory listings. Requesting `scandir('swift://foo/bar/')` is really a request to "find all of the items in the 'foo' container whose names start with 'bar/'".
Because of this...
Swift still has support for "directory markers" (special zero-byte files that act like directories). However, since there are no standards for how said markers ought to be created, they are not supported by the stream wrapper.
As usual, the underlying HPCloud::Storage::ObjectStorage::Container class supports the full range of Swift features.
SUPPORTED CONTEXT PARAMETERS
This section details paramters that can be passed either through a stream context or through HPCloud::Bootstrap::setConfiguration().
You are required to pass in authentication information. This comes in one of three forms:
tenantname
instead of tenantid
.The third method (token) can be used when the application has already authenticated. In this case, a token has been generated and assigned to an account and tenant.
The following parameters may be set either in the stream context or through HPCloud::Bootstrap::setConfiguration():
Definition at line 272 of file StreamWrapper.php.
|
protected |
Definition at line 1638 of file StreamWrapper.php.
References $account, $key, $tenantId, ObjectStorage\$token, and StreamWrapper\cxt().
Referenced by StreamWrapper\initializeCDN(), and StreamWrapper\initializeObjectStorage().
|
protected |
Get an item out of the context.
string | $name | The name to look up. First look it up in the context, then look it up in the Bootstrap config. |
mixed | $default | The default value to return if no config param was found. |
mixed |
Definition at line 1426 of file StreamWrapper.php.
Referenced by StreamWrapper\authenticate(), StreamWrapperFS\fakeStat(), StreamWrapper\initializeCDN(), StreamWrapper\initializeObjectStorage(), StreamWrapperFS\mkdir(), StreamWrapper\stream_open(), StreamWrapperFS\url_stat(), and StreamWrapper\writeRemote().
dir_closedir | ( | ) |
Close a directory.
This closes a directory handle, freeing up the resources.
NB: Some versions of PHP 5.3 don't clear all buffers when closing, and the handle can occasionally remain accessible for some period of time.
Definition at line 377 of file StreamWrapper.php.
dir_opendir | ( | $path, | |
$options | |||
) |
Open a directory for reading.
See opendir() and scandir().
string | $path | The URL to open. |
int | $options | Unused. |
boolean |
Definition at line 413 of file StreamWrapper.php.
References $container, ObjectStorage\$url, StreamWrapper\initializeObjectStorage(), and StreamWrapper\parseUrl().
dir_readdir | ( | ) |
Read an entry from the directory.
This gets a single line from the directory.
string |
Definition at line 470 of file StreamWrapper.php.
dir_rewinddir | ( | ) |
Rewind to the beginning of the listing.
This repositions the read pointer at the first entry in the directory.
Definition at line 519 of file StreamWrapper.php.
|
protected |
Generate a reasonably accurate STAT array.
Notes on mode:
Notes on mtime/atime/ctime:
Notes on size:
Definition at line 1271 of file StreamWrapper.php.
References $container, and $object.
Referenced by StreamWrapper\stream_stat(), and StreamWrapper\url_stat().
|
protected |
Initialize CDN service.
When the use_cdn
parameter is passed into the context, we try to use a CDN service wherever possible.
If use_cdn
is set to TRUE, we try to create a new CDN object. This will require a service catalog.
When use_cdn is set to TRUE, the wrapper tries to use CDN service. In such cases, we need a handle to the CDN object. This initializes that handle, which can later be used to get other information.
Also note that CDN's default behavior is to fetch over SSL CDN. To disable this, set 'cdn_require_ssl' to FALSE.
Definition at line 1611 of file StreamWrapper.php.
References $catalog, ObjectStorage\$token, StreamWrapper\authenticate(), StreamWrapper\cxt(), and ObjectStorage\newFromServiceCatalog().
Referenced by StreamWrapper\initializeObjectStorage().
|
protected |
Based on the context, initialize the ObjectStorage.
The following parameters may be set either in the stream context or through HPCloud::Bootstrap::setConfiguration():
To find these params, the method first checks the supplied context. If the key is not found there, it checks the Bootstrap::conf().
This should be rewritten to use ObjectStorage::newFromServiceCatalog().
Definition at line 1519 of file StreamWrapper.php.
References $account, $endpoint, $key, $tenantId, ObjectStorage\$token, StreamWrapper\authenticate(), StreamWrapper\cxt(), StreamWrapper\initializeCDN(), ObjectStorage\newFromServiceCatalog(), and ObjectStorage\newFromSwiftAuth().
Referenced by StreamWrapper\dir_opendir(), StreamWrapper\rename(), StreamWrapper\stream_open(), StreamWrapperFS\testDirectoryExists(), StreamWrapper\unlink(), and StreamWrapper\url_stat().
object | ( | ) |
Get the Object.
This provides low-level access to the PHCloud::Storage::ObjectStorage::Object instance in which the content is stored.
Accessing the object's payload (Object::content()) is strongly discouraged, as it will modify the pointers in the stream that the stream wrapper is using.
HOWEVER, accessing the Object's metadata properties, content type, and so on is okay. Changes to this data will be written on the next flush, provided that the file stream data has also been changed.
To access this:
Definition at line 1212 of file StreamWrapper.php.
objectStorage | ( | ) |
EXPERT: Get the ObjectStorage for this wrapper.
object | HPCloud::ObjectStorage An ObjectStorage object. |
Definition at line 1223 of file StreamWrapper.php.
References $store.
|
protected |
Parse a URL.
In order to provide full UTF-8 support, URLs must be rawurlencoded before they are passed into the stream wrapper.
This parses the URL and urldecodes the container name and the object name.
string | $url | A Swift URL. |
array |
Definition at line 1472 of file StreamWrapper.php.
References $key, and ObjectStorage\$url.
Referenced by StreamWrapper\dir_opendir(), StreamWrapper\rename(), StreamWrapper\stream_open(), StreamWrapperFS\testDirectoryExists(), StreamWrapper\unlink(), and StreamWrapper\url_stat().
rename | ( | $path_from, | |
$path_to | |||
) |
Rename a swift object.
This works by copying the object (metadata) and then removing the original version.
This DOES support cross-container renaming.
See Container::copy().
string | $path_from | A swift URL that exists on the remote. |
string | $path_to | A swift URL to another path. |
boolean |
Definition at line 570 of file StreamWrapper.php.
References $container, $object, StreamWrapper\initializeObjectStorage(), and StreamWrapper\parseUrl().
serviceCatalog | ( | ) |
EXPERT: Get the service catalog (IdentityServices) for this wrapper.
This is only available when a file is opened via fopen().
array | A service catalog. |
Definition at line 1247 of file StreamWrapper.php.
References ObjectStorage\token().
|
protected |
Set the fopen mode.
string | $mode | The mode string, e.g. r+ or wb . |
HPCloud::Storage::ObjectStorage::StreamWrapper |
Definition at line 1338 of file StreamWrapper.php.
Referenced by StreamWrapper\stream_open().
stream_cast | ( | $cast_as | ) |
Cast stream into a lower-level stream.
This is used for stream_select() and perhaps others.Because it exposes the lower-level buffer objects, this function can have unexpected side effects.
resource |
Definition at line 618 of file StreamWrapper.php.
stream_close | ( | ) |
Close a stream, writing if necessary.
This will close the present stream. Importantly, this will also write to the remote object storage if any changes have been made locally.
See stream_open().
Definition at line 643 of file StreamWrapper.php.
References StreamWrapper\writeRemote().
stream_eof | ( | ) |
Check whether the stream has reached its end.
This checks whether the stream has reached the end of the object's contents.
Called when feof()
is called on a stream.
See stream_seek().
boolean |
Definition at line 672 of file StreamWrapper.php.
stream_flush | ( | ) |
Initiate saving data on the remote object storage.
If the local copy of this object has been modified, it is written remotely.
Called when fflush()
is called on a stream.
Definition at line 684 of file StreamWrapper.php.
References StreamWrapper\writeRemote().
stream_open | ( | $path, | |
$mode, | |||
$options, | |||
& | $opened_path | ||
) |
Open a stream resource.
This opens a given stream resource and prepares it for reading or writing.
If a file is opened in write mode, its contents will be retrieved from the remote storage and cached locally for manipulation. If the file is opened in a write-only mode, the contents will be created locally and then pushed remotely as necessary.
During this operation, the remote host may need to be contacted for authentication as well as for file retrieval.
string | $path | The URL to the resource. See the class description for details, but typically this expects URLs in the form swift://CONTAINER/OBJECT . |
string | $mode | Any of the documented mode strings. See fopen(). For any file that is in a writing mode, the file will be saved remotely on flush or close. Note that there is an extra mode: 'nope'. It acts like 'c+' except that it is never written remotely. This is useful for debugging the stream locally without sending that data to object storage. (Note that data is still fetched – just never written.) |
int | $options | An OR'd list of options. Only STREAM_REPORT_ERRORS has any meaning to this wrapper, as it is not working with local files. |
string | $opened_path | This is not used, as this wrapper deals only with remote objects. |
Definition at line 782 of file StreamWrapper.php.
References ObjectStorage\$token, ObjectStorage\$url, ObjectStorage\container(), StreamWrapper\cxt(), StreamWrapper\initializeObjectStorage(), StreamWrapper\parseUrl(), and StreamWrapper\setMode().
stream_read | ( | $count | ) |
Read N bytes from the stream.
This will read up to the requested number of bytes. Or, upon hitting the end of the file, it will return NULL.
See fread(), fgets(), and so on for examples.
int | $count | The number of bytes to read (usually 8192). |
string |
Definition at line 980 of file StreamWrapper.php.
stream_seek | ( | $offset, | |
$whence | |||
) |
Perform a seek.
This is called whenever fseek()
or rewind()
is called on a Swift stream.
Definition at line 995 of file StreamWrapper.php.
stream_set_option | ( | $option, | |
$arg1, | |||
$arg2 | |||
) |
Set options on the underlying stream.
The settings here do not trickle down to the network socket, which is left open for only a brief period of time. Instead, they impact the middle buffer stream, where the file is read and written to between flush/close operations. Thus, tuning these will not have any impact on network performance.
See stream_set_blocking(), stream_set_timeout(), and stream_write_buffer().
Definition at line 1014 of file StreamWrapper.php.
stream_stat | ( | ) |
Perform stat()/lstat() operations.
To use standard stat()
on a Swift stream, you will need to set account information (tenant ID, account ID, secret, etc.) through HPCloud::Bootstrap::setConfiguration().
array |
Definition at line 1046 of file StreamWrapper.php.
References ObjectStorage\container(), and StreamWrapper\generateStat().
stream_tell | ( | ) |
Get the current position in the stream.
See ftell() and fseek().
int |
Definition at line 1065 of file StreamWrapper.php.
stream_write | ( | $data | ) |
Write data to stream.
This writes data to the local stream buffer. Data is not pushed remotely until stream_close() or stream_flush() is called.
string | $data | Data to write to the stream. |
int |
Definition at line 1082 of file StreamWrapper.php.
token | ( | ) |
EXPERT: Get the auth token for this wrapper.
string | A token. |
Definition at line 1234 of file StreamWrapper.php.
unlink | ( | $path | ) |
Unlink a file.
This removes the remote copy of the file. Like a normal unlink operation, it does not destroy the (local) file handle until the file is closed. Therefore you can continue accessing the object locally.
Note that OpenStack Swift does not draw a distinction between file objects and "directory" objects (where the latter is a 0-byte object). This will delete either one. If you are using directory markers, not that deleting a marker will NOT delete the contents of the "directory".
unlink()
does not take a context.string | $path | The URL. |
boolean |
Definition at line 1109 of file StreamWrapper.php.
References $container, $name, ObjectStorage\$token, ObjectStorage\$url, StreamWrapper\initializeObjectStorage(), and StreamWrapper\parseUrl().
url_stat | ( | $path, | |
$flags | |||
) |
Reimplemented in StreamWrapperFS.
Definition at line 1145 of file StreamWrapper.php.
References $container, $name, StreamWrapper\$obj, ObjectStorage\$token, ObjectStorage\$url, StreamWrapper\generateStat(), StreamWrapper\initializeObjectStorage(), and StreamWrapper\parseUrl().
|
protected |
Write data to the remote object storage.
Internally, this is used by flush and close.
Definition at line 700 of file StreamWrapper.php.
References ObjectStorage\container(), and StreamWrapper\cxt().
Referenced by StreamWrapper\stream_close(), and StreamWrapper\stream_flush().
|
protected |
Definition at line 296 of file StreamWrapper.php.
|
protected |
The Container.
Definition at line 333 of file StreamWrapper.php.
$context |
The stream context.
This is set automatically when the stream wrapper is created by PHP. Note that it is not set through a constructor.
Definition at line 292 of file StreamWrapper.php.
|
protected |
Definition at line 293 of file StreamWrapper.php.
|
protected |
Definition at line 307 of file StreamWrapper.php.
|
protected |
Definition at line 351 of file StreamWrapper.php.
|
protected |
Directory listing.
Used for directory methods.
Definition at line 350 of file StreamWrapper.php.
Referenced by StreamWrapperFS\testDirectoryExists().
|
protected |
Definition at line 352 of file StreamWrapper.php.
|
protected |
Definition at line 305 of file StreamWrapper.php.
|
protected |
Definition at line 300 of file StreamWrapper.php.
|
protected |
Indicate whether the local differs from remote.
When the file is modified in such a way that it needs to be written remotely, the isDirty flag is set to TRUE.
Definition at line 323 of file StreamWrapper.php.
|
protected |
If this is TRUE, no data is ever sent to the remote server.
Definition at line 312 of file StreamWrapper.php.
|
protected |
Definition at line 303 of file StreamWrapper.php.
|
protected |
Definition at line 301 of file StreamWrapper.php.
|
protected |
Definition at line 304 of file StreamWrapper.php.
|
protected |
Definition at line 302 of file StreamWrapper.php.
|
protected |
Definition at line 306 of file StreamWrapper.php.
|
protected |
The Object.
Definition at line 338 of file StreamWrapper.php.
Referenced by StreamWrapper\url_stat().
|
protected |
The IO stream for the Object.
Definition at line 343 of file StreamWrapper.php.
|
protected |
Definition at line 295 of file StreamWrapper.php.
|
staticprotected |
Cache of auth token -> service catalog.
This will eventually be replaced by a better system, but for a system of moderate complexity, many, many file operations may be run during the course of a request. Caching the catalog can prevent numerous calls to identity services.
Definition at line 284 of file StreamWrapper.php.
|
protected |
Object storage instance.
Definition at line 328 of file StreamWrapper.php.
|
protected |
Definition at line 314 of file StreamWrapper.php.
const DEFAULT_SCHEME = 'swift' |
Definition at line 274 of file StreamWrapper.php.