Contact us +32 498 138 661

info@rambla.be

RAWS tutorial

In this tutorial we will learn how to attach metadata (in the form of RASS meta elements) to files and directories on the Rambla CDN. This tutorial is built up the same way as tutorial 8, in which simple tagging was explained. The difference is that meta elements are more powerful than tags: they allow you to set values and attributes. They also require the use of vocabularies.

This tutorial assumes that a directory named 'tutorial10' has already been created on the CDN and a file named 'test.mp4' has been uploaded to this directory. The complete source code for this tutorial is also part of the client download package ('raws_tutorial_10_meta.php'). You can try running it yourself, after having edited the named constants on top of the script. For more information about using the PHP client, see tutorial 2.

The Meta Element

A meta element always requires a vocabulary ('vocab') and a name ('meta_name'). The vocabulary is a separate resource that consists of an XML namespace and a vocabulary name, which usually corresponds to the XML namespace prefix. It is used to qualify the name of the meta element. Additionally, the meta element may also have a value (= the element's text) and extra attributes (= other than 'vocab' and 'meta_name').

Let's look at some examples:

<meta vocab="dc" meta_name="title" lang="en">The lion sleeps tonight</title>

This meta element tells you that the attached file is titled "The lion sleeps tonight". To do this, the Dublin Core (vocab='dc') title tag is being used. In Dublin Core syntax it corresponds to: "<dc:title>The lion sleeps tonight</dc:title>". In a future tutorial, we'll explain how can retrieve the entry in Dublin Core format using the ext resource.

<meta vocab="media" meta_name="price" type="rent" price="19.99" currency="EUR" />

This meta element attaches a rent price of 19.99 euro to a given file. To do this, the Media RSS (vocab='media') price tag is being used. This element doesn't have a value. Instead, the additional attributes 'type', 'price' and 'currency' are used to specify pricing details and conditions. If you were to retrieve this resource as part of an external atom feed (using the 'ext' resource), it would look like like this: "<media:price type="rent" price="19.99" currency="EUR" />".

Setting meta elements

For accessing meta elements, the same principles apply as for accessing tags (see tutorial 7). When using the PHP libraries, you also call the same functions. The difference is that you will now be setting the $meta_array parameter (= third) instead of the $tag_array parameter (= second).

The $meta_array parameter takes an array of Rass_Extension_Meta objects, wherein each object is a wrapper for a single meta element. The class constructor for Rass_Extension_Meta looks like this: public function __construct($vocab = null, $meta_name = null, $text = null, $attrs = null, $lang = null)

  1. $vocab : a string containing the vocabulary name
  2. $meta_name : a string containing the meta element name
  3. $text : a string containing the text value of the meta element
  4. $attrs : an associative array wherein key and value are both strings that correspond to the name and value of additional attributes
  5. $lang : ISO 639-1 abbreviation of the language being used

By using setMetadataFromPath(), we can set our Dublin Core 'title' on our freshly uploaded file:

  $meta_elems = array(new Rass_Extension_Meta("dc", "title", "The lion sleeps tonight"));
$meta_entry = $rass->setMetadataFromPath($item_entry->path, array(), $meta_elems);
foreach ($meta_entry->content->params->meta as $t) {
echo "vocab: " . $t->vocab . "\n";
echo "meta_name: " . $t->meta_name . "\n";
echo "value: " . $t->text . "\n";
}
>> vocab: dc
>> meta_name: title
>> value: The lion sleeps tonight

Adding meta elements

When using the setMetadataFromPath() and setMetadata() functions, you will overwrite all existing manual metadata of a meta instance. To set additional tags on a meta instance, you should use addMetadata() instead. We'll use it here to add a rent price to our 'test.mp4' file.

  $meta_elems = array(new Rass_Extension_Meta("media", "price", null, 
array("type" => "rent", "price" => "19.99", "currency" => "EUR")));
$meta_entry = $rass->addMetadata($meta_entry, array(), $meta_elems);
foreach ($meta_entry->content->params->meta as $t) {
echo "\nvocab: " . $t->vocab . "\n";
echo "meta_name: " . $t->meta_name . "\n";
if ($t->text) {
echo "value: " . $t->text . "\n";
}
foreach ($t->attrs as $key => $value) {
echo "Attr: " . $key . "=" . $value . "\n";
}
}
>> vocab: dc
>> meta_name: title
>> value: The lion sleeps tonight
>>
>> vocab: media
>> meta_name: price
>> Attr: currency=EUR
>> Attr: price=19.99
>> Attr: type=rent

You can also change the contents of the Rass_Extension_Meta objects in the 'meta' array or add new Rass_Extension_Meta objects to the array, and call addMetadata() in order to modify the meta resource on the RASS server. For example, the following code will change the rent price and add some subscription info:

  $meta_entry = $rass->getMetaEntry($item_entry->path);
foreach ($meta_entry->content->params->meta as $t) {
if (($t->name == "price") && ($t->vocab == "media")) {
$a = $t->attrs;
$a["price"] = "20.00";
$a["info"] = "http://www.dummy.jp/subscription_info";
$t->attrs = $a;
}
}
$meta_entry = $rass->addMetadata($meta_entry, array(), array());

Deleting metaDATA

You can call deleteMetaEntry() to send a HTTP DELETE request for a given meta entry. This will remove all metadata that has been (manually) attached to the file or directory. The file or directory itself will not be deleted.

$rass->deleteMetaEntry($meta_entry);

In tutorial 7, we already saw how to use the meta resource to retrieve collections by doing a search based on (parts of) tag(s). In this tutorial, we will look at these topics in further detail, using the PHP client libraries (see tutorial 2 for more details). The complete source code for this tutorial is also part of the client download package ('rass_tag_search.php'). You can try running it yourself, after having edited the named constants on top of the script.

Pre-Configuration

Before we start with the tutorial, we need do upload some files and attach tags to them. First, we create a directory 'tutorial9' on the CDN. Next, we upload six files and attach metadata to some of these files:

  • 'eagle.mp4', which has been tagged with "animal", "bird" and "birdwatching"
  • 'owl.mp4', which has been tagged with "animal", "bird" and "documentary"
  • 'snake.mp4', which has been tagged with "animal", "reptile" and "documentary"
  • 'spiderman.mp4', which has been tagged with "superhero", "rebirth" and "moviee"
  • 'test1.mp4" without any tags
  • 'test2.mp4" without any tags

Getting a META Feed

The getMetaFeed() function takes 2 arguments:

  1. $path : string containing the location of the file or directory for which metadata should be set.
  2. $query (= optional) : a Rass_MetaQuery object that acts as a wrapper for all query-string arguments

In the example below, we will call getMetaFeed() with a $path argument that points to the '/movies' directory and a query-string argument ('kind=file') that instructs RASS to only return entries concerning files.

  $query = new Rass_MetaQuery("file");
$feed = $rass->getMetaFeed($dir_entry->path, $query);
foreach ($feed as $entry) {
echo "\nFound " . $entry->kind . " entry with path = " . $entry->path;
}
}
>> Found file entry with path = /tutorial9/eagle.mp4
>> Found file entry with path = /tutorial9/owl.mp4
>> Found file entry with path = /tutorial9/snake.mp4
>> Found file entry with path = /tutorial9/spiderman.mp4
>> Found file entry with path = /tutorial9/test1.mp4
>> Found file entry with path = /tutorial9/test2.mp4

As you can see, the meta feed contains an entry for each uploaded file, even if no metadata has been set. This is also the case when you pass the 'name' argument as query-string argument. For example:

  $query = new Rass_MetaQuery("file");
$query->setName("test?.*");
$feed = $rass->getMetaFeed($dir_entry->path, $query);
foreach ($feed as $entry) {
echo "\nFound " . $entry->kind . " entry with path = " . $entry->path;
}
>> Found file entry with path = /tutorial9/test1.mp4
>> Found file entry with path = /tutorial9/test2.mp4

In this case, the file matching pattern only matches the 'test1.mp4' and 'test2.mp4' files. Note that you can not combine a name-based search with a metadata-based search in the same request.

Searches based on tag elements

The 'tag' query-string argument is not case-sensitive, but does require that you specify complete tags (no missing characters) as part of the query-string. Searches based on metadata can only return locations (files and/or directories) on which metadata has been set. Here's a basic example:

  echo "\n\nSearch for all files tagged with 'bird' :";
$query->setSearchArg("tag", array("bird"));
$feed = $rass->getMetaFeed($dir_entry->path, $query);
foreach ($feed as $entry) {
echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
}
>> Search for all files tagged with 'bird' :
>> found file entry with path = /tutorial9/eagle.mp4
>> found file entry with path = /tutorial9/owl.mp4

You can also use multiple 'tag' values to search on:

  echo "\n\nSearch for all files tagged with 'bird' OR 'reptile' :";
$query->setSearchArg("tag", array("bird", "reptile"));
$feed = $rass->getMetaFeed($dir_entry->path, $query);
foreach ($feed as $entry) {
echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
}
>> Search for all files tagged with 'bird' OR 'reptile' :
>> found file entry with path = /tutorial9/eagle.mp4
>> found file entry with path = /tutorial9/owl.mp4
>> found file entry with path = /tutorial9/snake.mp4

USING SEARCH OPERATORS

As explained in tutorial 7, RASS supports search operators (OR, AND, NOT) when doing metadata-based searches. In the two previous examples, the OR-operator (= default) has been used implicitly. To use another operator, you have to include it in your query-string. This can be done by using the setSearchArg() function of the $query object, which takes 4 parameters:

  1. $argname : string containing the search argument name (= tag', 'qtag', 'iqtag', 'meta', 'qmeta' or 'iqmeta')
  2. $or_args : array of argument values (= strings) to be used with the OR-operator
  3. $and_args : array of argument values (= strings) to be used with the AND-operator
  4. $not_args : array of argument values (= strings) to be used with the NOT-operator

The following example will use the AND-operator (+ character, URL encoded as %2B), which will result in a query-string like "tag=%2Banimal,%2Bdocumentary".

  echo "\n\nSearch for all files tagged with 'animal' AND 'documentary' :";
$query->setSearchArg("tag", array(), array("animal", "documentary"));
$feed = $rass->getMetaFeed($dir_entry->path, $query);
foreach ($feed as $entry) {
echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
}
>> Search for all files tagged with 'animal' AND 'documentary' :
>> found file entry with path = /tutorial9/owl.mp4
>> found file entry with path = /tutorial9/snake.mp4

Operators may also be combined. Here's a request for all files that must have the 'animal' tag, but may not have the 'bird' tag.

  echo "\n\nSearch for all files tagged with 'animal' BUT NOT with 'bird' :";
$query->setSearchArg("tag", array("animal"), array(), array("bird"));
$feed = $rass->getMetaFeed($dir_entry->path, $query);
foreach ($feed as $entry) {
echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
}
>> Search for all files tagged with 'animal' BUT NOT with 'bird' :
>> found file entry with path = /tutorial9/snake.mp4

Searching on part(s) of a tag

The 'qtag' (= case sensitive) and 'iqtag' (= case insensitive) allow you do searches on part of a tag. For instance:

  echo "\n\nCase-sensitive search for all files with a tag that contains the string 'ani' :";
$query = new Rass_MetaQuery("file");
$query->setSearchArg("qtag", array("ani"));
$feed = $rass->getMetaFeed($dir_entry->path, $query);
foreach ($feed as $entry) {
echo "\n found " . $entry->kind . " entry with path = " . $entry->path;
}
>> Case-sensitive search for all files with a tag that contains the string 'ani' :
>> found file entry with path = /tutorial9/eagle.mp4
>> found file entry with path = /tutorial9/owl.mp4
>> found file entry with path = /tutorial9/snake.mp4

The same search operators as for the 'tag' argument apply here. However, it is not possible to combine 'tag', 'qtag' and/or 'iqtag' in a single request. If both the 'tag' and 'iqtag' arguments are part of a single query-string, the 'iqtag' argument will be ignored. For more examples, see the tutorial's PHP script and the Rambla wiki pages.

In our previous tutorial we explained how you can use the RASS meta resource to attach metadata to files and/or directories on the CDN. In this tutorial, we will focus on how to do simple tagging with the PHP client. In our next tutorial, we will show you how to send search requests using tags. More advanced metadata topics (e.g. using the meta element) will also be discussed in future tutorials.

This tutorial assumes that a directory named 'tutorial8' has already been created on the CDN and a file named 'test.mp4' has been uploaded to this directory. The complete source code for this tutorial is also part of the client download package ('raws_tutorial_8_tagging.php'). You can try running it yourself, after having edited the named constants on top of the script. For more information about using the PHP client, see tutorial 2.

Setting tags

The function setMetadataFromPath() may be used to create or overwrite the manual metadata of a RASS meta instance. It takes the following parameters:

  1. $path : string containing the location of the file or directory for which metadata should be set.
  2. $tag_array : array of Rass_Extension_Tag objects. This class acts as a wrapper for tag elements: it simply takes the tag value in its constructor.
  3. $meta_array : array of Rass_Extension_Meta objects. This class acts as a wrapper for meta elements. We'll learn more about this in a future tutorial.

After uploading a file using RASS, we have access to the string that needs to be passed in the $path argument ($item_entry->path). We'll use it here to set two tags on our uploaded file: "animal" and "bird".

  $item_entry = $rass->postItem($dir_entry->path, "test.mp4", LOCAL_FILE);
$meta_entry = $rass->setMetadataFromPath($item_entry->path,
array(new Rass_Extension_Tag("animal"), new Rass_Extension_Tag("bird")),
array());

RASS responds with the meta entry for the file that we've just tagged. The entry's 'params' element contains the two tag elements: 'animal' and 'bird' (<tag>animal</tag><tag>bird</tag>). In our object structure, we can access these elements through the 'tag' datamember, which contains an array of Rass_Extension_Tag objects. We can iterate over this array to find out which tags have been set by RASS.

  foreach ($meta_entry->content->params->tag as $t) {
echo "Tag: " . $t->text . "\n";
}
>> Tag: animal
>> Tag: bird

Updating meta RESOURCES

When using the setMetadataFromPath() and setMetadata() functions, you will overwrite all existing manual metadata of a meta instance. To set additional tags on a meta instance, use the addMetadata() function instead. This function takes 3 parameters:

  1. $entry : the Rass_MetaEntry that corresponds to your existing meta resource.
  2. $tag_array : array of Rass_Extension_Tag objects. These will be added to the ones already part of the $entry object.
  3. $meta_array : array of Rass_Extension_Meta objects. These will be added to the ones already part of the $entry object.

The following code will attach the "mockumentary" tag to our 'test.mp4' file.

  $meta_entry = $rass->getMetaEntry($item_entry->path);
$meta_entry = $rass->addMetadata($meta_entry,
array(new Rass_Extension_Tag("mockumentary")),
 array());
foreach ($meta_entry->content->params->tag as $t) {
echo "Tag: " . $t->text . "\n";
}
>> Tag: animal
>> Tag: bird
>> Tag: mockumentary

You can also change the text of Rass_Extension_Tag objects in the 'tag' array or add new Rass_Extension_Tag objects to the array, followed by a call to addMetadata() in order to modify the actual meta resource on the RASS server. For example, the following code changes the "mockumentary" tag to "documentary":

  foreach ($meta_entry->content->params->tag as $t) {
if ($t->text == "mockumentary") {
$t->text = "documentary";
}
}
$meta_entry = $rass->addMetadata($meta_entry, array(), array());

Deleting metaDATA

You can call deleteMetaEntry() to send a HTTP DELETE request for a given meta entry. This will remove all metadata that has been (manually) attached to the file or directory. The file or directory itself will not be deleted.

  $rass->deleteMetaEntry($meta_entry);

The Rambla Storage Service's (RASS) meta resource allows you to attach metadata to files and/or directories on the Rambla CDN. You can access the meta resource for a given file or directory by adding its relative path to the URL, after the meta resource indicator (= '/meta') and your user name (e.g. '/monty'). For example, a file owned by user monty that has "/movies/test1.mp4" as its relative path will be accessible via the following URL: http://rass.cdn03.rambla.be/meta/monty/movies/test1.mp4.

A meta resource instance automatically exists for each file and directory on the CDN. After creating a new directory or uploading a new file via RASS (see tutorials 1 and 2), the entry returned by RASS will contain an 'atom:link' with an 'alternate' type of relation that points to the related meta resource. For example, after uploading a file named 'movie.mp4' to the user account of user 'monty', RASS will return an item entry that contains the following link element:

<link href="https://rass.cdn01.rambla.be/meta/monty/movie.mp4/" rel="self" type="application/atom+xml" />

See tutorial 5 for more info on atom link elements, and tutorial 6 for details on how to handle them with the RAWS PHP client.

The meta entry

A meta entry contains two types of metadata:

  • Automatic metadata, which is generated by RASS (file or directory name, size, mimetype) for all files and directories. This kind of metadata is kept permanently by RASS; even if you delete your manual metadata, it will remain available.
  • Metadata manually set by users: currently this can either be (plain) 'tag' elements, or (complex) 'meta' elements. This tutorial will focus on how to use the 'tag' element (the 'meta' element will be explained in later tutorials)

Without metadata having been set manually, a typical meta entry looks like this (note: some atom elements have been omitted for better readability):

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:raws="http://rambla.be/raws/ns-metadata/1.0" raws:kind="file"
 raws:path="/movies/test1.mp4">
<id>http://rass.cdn03.rambla.be/meta/monty/movies/test1.mp4/</id>
<link href="http://rass.cdn03.rambla.be/meta/monty/movies/test1.mp4/" rel="self" type="application/atom+xml"/>
<link href="http://monty.cdn03.rambla.be/movies/test1.mp4" rel="enclosure" type="video/mp4"/>
<content type="application/xml">
<params xmlns="http://rambla.be/raws/ns-metadata/1.0">
<name>test1.mp4</name>
<size>17691733</size>
<mimetype>video/mp4</mimetype>
</params>
</content>
</entry>

Note that meta instances for files also contain an 'enclosure' link which points to the (public) download location of the file on the CDN.

Setting tags

To set one or more tags, you send a HTTP POST request to the meta resource. The body of the request should contain the tag elements that you want to attach to the file or directory. For example, to set the tags 'bird' and 'eagle' on the file "test1.mp4" in the "movies" directory of user "monty", you send a POST request to http://rass.cdn03.rambla.be/meta/monty/movies/test1.mp4 with the following <params>  element in the body:

<params xmlns="http://rambla.be/raws/ns-metadata/1.0">
<tag>bird</tag>
<tag>eagle</tag>
</params>

By sending such HTTP POST request, all existing tags will be removed and only the tag elements inside the request body will be set. To remove all tags, you can POST a body without any tag elements (or use the HTTP DELETE method).

The meta feed

If you send a HTTP GET request to a meta instance that corresponds to a directory, a meta feed will be returned. This feed will contain meta entries for all files and/or sub-directories (depending on the value of the 'kind' query-string argument) inside the directory. Note that the meta resource supports pagination, so the feed that is being returned may be partial (default: 100 entries in a single response). For more details about pagination, see the previous tutorial.

For example, to get a feed of all files located inside the 'movies' directory:

http://rass.cdn03.rambla.be/meta/monty/movies/?kind=file

You can use the 'name' query-string argument and the wildcard characters ? (for a single char, URL-encoded as %3F) and * (for multiple chars, URL-encoded as %2A) to return only those results that corresponding to a certain filename pattern. For example, to return entries for all files that start with "test" and have the ".mp4" extension, add (the url-encoded equivalent of) 'name=test*.mp4' to the query-string:

http://rass.cdn03.rambla.be/meta/monty/movies/?kind=file;name=test%2A.mp4

Note: the 'name' argument may not be combined with the 'tag' and 'meta' arguments (= these will be ignored if the 'name' argument is present in the query-string).

Tag searches

The 'tag' query-string argument allows you to retrieve all locations containing (a) certain tag(s). Tag searches are always recursive and return by default (= if used without the 'kind' query-string argument) both files and directories with the given tag. There are three types of query-string arguments that can be used for searches:

  • tag : requires the complete tag (case-insensitive) to be included in the query-string
  • qtag : allows you do (case-sensitive) searches on part of (a) tag(s)
  • iqtag : allows you do (case-insensitive) searches on part of (a) tag(s)

For instance, to retrieve all files that are tagged with 'bird' below the movies directory, send the following HTTP GET request:

GET http://rass.cdn03.rambla.be/meta/monty/movies/?kind=file;tag=bird

This will return a meta feed containing the entry for our "test1.mp4" file.

Search operators

When doing a search, you can also use the OR (= piping symbol, used by default), AND (= plus symbol) and NOT (= minus symbol) operators. Note that the piping ('%7C'), plus ('%2B') and minus (= '%2D') characters in the query-string must also be URL-encoded.

Take the following example (without URL-encoding the query-string is "?kind=file;qtag=bir,-eagl"):

GET http://rass.cdn03.rambla.be/meta/monty/movies/?kind=file;qtag=bir,%2Deagl

This request asks RASS to do a search inside the 'movies' directory for files that have a tag containing the word 'bir' (e.g. bird) but not the word 'eagl' (e.g. eagle). Since our test1.mp4 file has an 'eagle' tag, it will not be returned by this search. If no other entry corresponds to the search criteria, RASS will return an empty feed.

The admin interface

RASS comes with a web application that allows you to administer your metadata. By default, the admin interface only shows the file and/or directory locations to which metadata has been attached manually. You can make other locations show up in the admin interface by sending a GET request with "sync=1" as part of your query-string. For example, GET 'http://rass.cdn03.rambla.be/meta/monty/movies/?sync=1' will add the 'movies' directory and all files inside it as locations to the admin interface. For a demonstration of the RASS admin basics, see this screencast.

Syndicate content