rass_basics_manage_file_dir.php

00001 <?php
00002 # Sample script for RAWS tutorial 2: see http://rambla.eu/raws-tutorial-2-basic-rass-operations-php-client for more info.
00003 #
00004 # Copyright (C) 2010 rambla.be
00005 
00006 # Licensed under the Apache License, Version 2.0 (the "License");
00007 # you may not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 #      http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS,
00014 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 require_once '../RawsClient/Raws/Rass.php';
00018 
00019 # Provide the path to a local testfile, that can be uploaded via RASS
00020 define('LOCAL_FILE', '/my/path/to/a/file.mp4');
00021 
00022 # Provide your own RASS credentials here
00023 define('USER', 'xxx'); # your user account name
00024 define('PWD', 'xxx'); # your user account pwd
00025 define('CDN', "cdn0x"); # your sub-cdn (e.g. "cdn01")
00026 
00027 define('BASE', 'http://rass.' . CDN . '.rambla.be/');
00028 
00029 # all RASS API methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
00030 try {
00031   # Instantiate an object that manages the RASS connection, passing it your login credentials and the base service uri
00032   $rass = new Rass(USER, PWD, BASE);
00033 
00034   # Creates a "tutorial2" directory below your root dir
00035   $dir_entry = $rass->createDir("tutorial2", True);
00036   # Retrieve the entry's "path" attribute
00037   echo "Created directory with path: " . $dir_entry->path . "\n";
00038   # Note: the URL for accessing this new dir resource is available in the "Location" header of the response
00039   echo "New dir resource can be accessed at the URL: " . $rass->getResponse()->getHeader("Location") . "\n";
00040 
00041   # Uploads the local file as 'taste.mp4' to the "tutorial2" directory on the CDN
00042   $item_entry = $rass->postItem($dir_entry->path, "taste.mp4", LOCAL_FILE);
00043   # Retrieve the entry's "path" attribute
00044   echo "Created file with path: " . $item_entry->path . "\n";
00045   # Retrieve the actual filename on the CDN
00046   echo "Filename: " . $item_entry->content->params->name->text . "\n";
00047   # Get the location of the file on the CDN from the entry's 'enclosure' link
00048   echo "Public download location of the uploaded file: " . $item_entry->get_enclosure_url() . "\n";
00049   # Note: the URL for accessing this new item resource is available in the "Location" header of the response
00050   echo "New item resource can be accessed at the URL: " . $rass->getResponse()->getHeader("Location") . "\n";
00051 
00052   # POST the same file for a second time => RASS will add a numerical suffix
00053   $item_entry2 = $rass->postItem($dir_entry->path, "taste.mp4", LOCAL_FILE);
00054   # Retrieve the entry's "path" attribute
00055   echo "Created file with path: " . $item_entry2->path . "\n";
00056   # Retrieve the actual filename on the CDN
00057   echo "Filename: " . $item_entry2->content->params->name->text . "\n";
00058   # Get the location of the file on the CDN from the entry's 'enclosure' link
00059   echo "Public download location of the uploaded file: " . $item_entry2->get_enclosure_url() . "\n";
00060   # Note: the URL for accessing this new item resource is available in the "Location" header of the response
00061   echo "New item resource can be accessed at the URL: " . $rass->getResponse()->getHeader("Location") . "\n";
00062   
00063   // # PUT the same file for a second time -> RASS will return error => Zend_Gdata_App_Exception exception is raised
00064   // $item_entry2 = $rass->createItem($dir_entry->path . "/taste.mp4", LOCAL_FILE);
00065   
00066   # DELETE file on the CDN
00067   $rass->deleteItem($item_entry2->path);
00068   echo "Deleted file: " . $item_entry2->path . "\n";
00069   
00070   # DELETE directory on the CDN. Set second argument to True to delete recursively.
00071   $rass->deleteDir($dir_entry->path, True);
00072   echo "Deleted dir: " . $dir_entry->path . "\n";
00073   
00074 }
00075 catch(Zend_Gdata_App_Exception $e) {
00076     # Report the exception to the user
00077     echo "\nCaught exception: " . get_class($e) . "\n";
00078     echo "Message: " . $e->getMessage() . "\n";
00079     # get the HTTP status code
00080     echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
00081     echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
00082     # get the raws:error elements
00083     $rawsResponse = Raws::parseRawsResponse($e->getResponse());
00084     echo "Raws Code: " . $rawsResponse->getCode() . "\n";
00085     echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
00086     $reasons = $rawsResponse->getReasons();
00087     foreach ($reasons as $reason) {
00088       echo "Raws Reason: " . $reason . "\n";
00089     }
00090     $details = $rawsResponse->getDetails();
00091     foreach ($details as $key => $value) {
00092       echo "Raws Detail: " . $key . " -> " . $value . "\n";
00093     }
00094 }
00095 catch (Zend_Exception $e) {
00096     echo "Caught exception: " . get_class($e) . "\n";
00097     echo "Message: " . $e->getMessage() . "\n";
00098 }

Generated on Mon Mar 28 15:03:20 2011 for RawsClient PHP by  doxygen 1.5.3