Container Class
Create a new container.
When a new container is created, no check is done against the server to ensure that the container exists. Thus, it is possible to have a local container object that does not point to a legitimate server-side container.
Constructor
Container
-
name
-
token
-
url
Parameters:
-
name
StringThe name of the container.
-
token
StringAn authentication token.
-
url
StringThe URL of the container.
Item Index
Methods
acl
-
fn
Get the ACL for the current container.
In some cases, this will result in a request to the remote server.
Parameters:
-
fn
FunctionThe callback, which will receive fn(Error e, ACL acl);
bytes
()
Number
Get the byte count for this container.
Retrieves the number of bytes this container currently consumes.
Returns:
copy
-
info
-
newName
-
[containerName]
-
fn
Copy a resource from one path to another, or from one container to another.
This does a remote-side copy, so the payload is never transferred locally.
An interesting side effect of copy() is that you can change the metadata of a destination object by supplying alternative metadata during copy. You can also change content type and other properties. Since you can copy an object to itself, this is the cheapest way to change metadata.
To do this, provide an ObjectInfo as the first argument. That will then be used to set headers and metadata.
Parameters:
-
info
ObjectInfo | StringThe object to copy.
-
newName
StringThe name of the new object.
-
[containerName]
String optional(Optional) The name of the container to copy to. If none is specified, the present container is used.
-
fn
FunctionThe callback, called like this: fn(Error e);
count
()
Number
Get the number of objects in the container.
This returns the count of objects currently inside of the container. This is the total number of objects, not the number of objects at the "top level" of the container.
Returns:
decodeMetadata
-
headers
Decode the http headers meta data into a seperate object containing just the metadata.
Parameters:
-
headers
Objecthttp headers from a response.
Returns:
delete
-
name
-
fn
Delete an object.
Parameters:
-
name
StringThe name of the object to delete.
-
fn
FunctionThe callback. Called with fn(Error e, boolean status). Errors are returned if the resource could not be found.
status
will be true if the resource was deleted.
encodeMetadata
-
metadata
-
[headers]
Convert the metadata to headers.
Parameters:
-
metadata
ObjectAn object with key value pairs of metadata.
-
[headers]
Object optionalAn object with http headers (optional).
Returns:
fetchDetails
-
contriner
-
fn
fn(Error, Container);
Parameters:
-
contriner
String -
fn
Function
metadata
-
fn
Get the metadata for a container.
Depending on how the container was constructed, this may require a trip to the remote server to fetch metadata.
Parameters:
-
fn
FunctionThe callback, which will receive two parameters: fn(Error e, Object metadata).
name
()
String
Get the name of this container.
Returns:
newFromJSON
-
json
-
token
-
url
Create a new Container from JSON data.
This is used to create a new container object from a JSON response.
Parameters:
-
json
ObjectJSON data in the correct format.
-
token
StringThe auth token.
-
url
StringThe URL to object storage. This will be modified internally to point to this container.
Returns:
newFromResponse
-
name
-
response
-
token
-
endpoint
Construct a new Container from a response.
Internally, this is used to build a new container from HTTP headers.
Parameters:
-
name
StringThe name of the container.
-
response
HTTPResponseAn HTTP response object.
-
token
StringAn authentication token.
-
endpoint
StringThe URL to the Swift REST endpoint. This is used as the base URL to construct a URL to the container itself.
Returns:
objectInfo
-
name
-
fn
Get information about an object.
This includes all of the standard data (name, content type, content length, eTag/md5, and so on) as well as any metadata that was attaached to the object.
This does NOT get the object's body. The object() method must be used for that.
Parameters:
-
name
StringThe name of the object to retrieve.
-
fn
FunctionA callback.
objectQuery
-
params
-
fn
Query for an object.
See objects(), objectsWithPrefix(), and objectsByPath() for simpler queries.
The following params are supported: - params.limit: set the maximum number of items returned - params.marker: get the next item after the named marker. Marker is an object name. - params.prefix: Use prefix/delimiter notation to get a "subdirectory" listing. - params.delimiter: the delimiter to use to separate directories. Usually '/'. - params.path: Use a path prefix to get children on a path.
Parameters:
-
params
ObjectAny number of paramters, as specified above.
-
fn
FunctionThe callback, executed as fn(Error e, Array list). The Array is a list of ObjectInfo items and (depending on params) Subdir items.
objects
-
limit
-
[marker]
-
fn
Query for all the objects based on a limited set of criteria.
This is a wrapper around objectQuery.
Parameters:
-
limit
NumberThe number of objects to limit the query to.
-
[marker]
String optionalGet the next item after the marker. A marker is an object name.
-
fn
FunctionThe callback, executed as fn(Error e, Array list). The Array is a list of ObjectInfo items and (depending on params) Subdir items.
objectsByPath
-
path
-
delimiter
-
[limit]
-
[marker]
-
fn
Specify a path (subdirectory) to traverse.
OpenStack Swift provides two basic ways to handle directory-like structures. The first is using a prefix (see objectsWithPrefix()). The second is to create directory markers and use a path.
A directory marker is just a file with a name that is
directory-like. You create it exactly as you create any other file.
Typically, it is 0 bytes long with the content type application/directory
Using objectsByPath() with directory markers will return a list of Object instances, some of which are regular files, and some of which are just empty directory marker files. When creating directory markers, you may wish to set metadata or content-type information indicating that they are directory markers.
At one point, the OpenStack documentation suggested that the path method was legacy. More recent versions of the documentation no longer indicate this.
Parameters:
-
path
StringThe path to query objects for.
-
delimiter
StringA delimiter used to seperate paths in a directory like structure.
-
[limit]
Number optionalThe number of objects to limit the query to.
-
[marker]
String optionalGet the next item after the marker. A marker is an object name.
-
fn
FunctionThe callback will receive fn(Error e, Array list), where the array is a list of ObjectInfo objects.
objectsWithPrefix
-
prefix
-
delimiter
-
limit
-
marker
-
fn
Retrieve a list of Objects with the given prefix.
Object Storage containers support directory-like organization. To get a list of items inside of a particular "subdirectory", provide the directory name as a "prefix". This will return only objects that begin with that prefix.
(Directory-like behavior is also supported by using "directory markers". See objectsByPath().)
Prefixes
Prefixes are basically substring patterns that are matched against files on the remote object storage.
When a prefix is used, object storage will begin to return not just Object instsances, but also Subdir instances. A Subdir is simply a container for a "path name".
Delimiters
Object Storage (OpenStack Swift) does not have a native concept of files and directories when it comes to paths. Instead, it merely represents them and simulates their behavior under specific circumstances.
The default behavior (when prefixes are used) is to treat the '/'
character as a delimiter. Thus, when it encounters a name like
this: foo/bar/baz.txt
and the prefix is foo/
, it will
parse return a Subdir called foo/bar
.
Setting delimiter
will tell the Object Storage server which
character to parse the filenames on. This means that if you use
delimiters other than '/', you need to be very consistent with your
usage or else you may get surprising results.
Parameters:
-
prefix
String -
delimiter
String -
limit
Int -
marker
String -
fn
FunctionThe callback will receive fn(Error e, Array list), where the array is a list of ObjectInfo objects.
remoteObject
-
name
-
fn
Get a RemoteObject instance.
This fetches an object from object storage.
The returned object is a stream, and will also have an ObjectInfo attached.
Importantly, no processing is done on the returned object. It is passed on as-is. No length or etag checking is done. No content type validation is done. The data is not read first and buffered.
Parameters:
-
name
StringThe name of the object to fetch.
-
fn
FunctionThe callback, called as fn(Error e, RemoteObject o)
save
-
obj
-
content
-
fn
Write an object to the remote data store.
Importantly, certain fields on the ObjectInfo are ignored when saving: - eTag: This is generated on the fly to ensure accuracy. - Content-Length: This is not used, since we send with chunked encoding.
Parameters:
-
obj
ObjectInfoThe object to write.
-
content
String | Buffer | Stream -
fn
FunctionThe callback. fn(Error e, HTTPResponse r)
setMetadata
-
metadata
Set the metadata on the present object.
This does NOT save the metadata on the remote server.
Parameters:
-
metadata
ObjectName/value pairs for metadata. It is recommended that you encode the values prior to putting them here, as the Swift REST docs make no assumptions about how the metadata is encoded or decoded.
token
()
String
Get the token used with this container.
Returns:
updateObjectMetadata
-
info
-
fn
Update the metadata on an object.
This allows you to update an object's metadata without requiring you to re-post the object's data payload.
According to the Swift documentation, this ONLY modifies the arbitrary metadata (See ObjectInfo.setMetadata()). Other info, including content type, disposition, and so on, cannot be changed this way.
IMPORTANT: To change the Content-Type of an object, you can use the Container.copy() method, copying the source to the same destination.
Parameters:
-
info
ObjectInfoThe local copy of the object that should be updated on the remote server.
-
fn
FunctionThe callback. This is called as fn(Error e).
url
()
String
Get the URL of this container.