Skip to content

Instantly share code, notes, and snippets.

@nickweavers
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickweavers/9936267 to your computer and use it in GitHub Desktop.
Save nickweavers/9936267 to your computer and use it in GitHub Desktop.
<?php
/**
* A test vehicle to allow myself to learn the new RackSpace OpenCloud PHP SDK
*
*/
// include the Rackspace Cloud files PHP API
require 'rackspace_config.php';
require 'libraries/rackspace/vendor/autoload.php';
use OpenCloud\Rackspace;
use OpenCloud\ObjectStore\Resource\DataObject;
use Guzzle\Http\Exception\ClientErrorResponseException;
if(count($_POST) > 0) {
upload_file();
header("location: {$_SERVER['PHP_SELF']}");
} elseif ($_GET['view_file']) {
view_file();
} elseif ($_GET['delete_file']) {
delete_file();
header("location: {$_SERVER['PHP_SELF']}");
} else {
show_form();
}
function upload_file() {
// Lets connect to Rackspace
$client = new Rackspace(Rackspace::UK_IDENTITY_ENDPOINT, array( // Note the use of
'username' => USER,
'apiKey' => PSWD
));
// Create a service object to use the object store service. We give it the region name on LON (For London) as this is where are server and cloud files are
$service = $client->objectStoreService('cloudFiles', 'LON');
$container = null;
// Get the object container
$container_name = "AAAA";
// Get the object container
try {
// If container already exists
$container = $service->getContainer($container_name);
} catch (ClientErrorResponseException $e) {
// If the container doesn't exist, create it
$container = $service->createContainer($container_name);
}
// store file information
$file_to_be_uploaded = $_FILES['file_to_be_uploaded']['tmp_name'];
$file_name = $_FILES['file_to_be_uploaded']['name'];
$file_type = $_FILES['file_to_be_uploaded']['type'];
$meta = array(
'Mime-type' => $file_type
);
$metaHeaders = DataObject::stockHeaders($meta);
// upload the file to Rackspace CF servers
$data = fopen($_FILES['file_to_be_uploaded']['tmp_name'], 'r+');
$container->uploadObject($_FILES['file_to_be_uploaded']['name'], $data, $metaHeaders);
// thats it you are done.
}
function getFileList () {
// Lets connect to Rackspace
$client = new Rackspace(Rackspace::UK_IDENTITY_ENDPOINT, array( // Note the use of
'username' => USER,
'apiKey' => PSWD
));
// Create a service object to use the object store service. We give it the region name on LON (For London) as this is where are server and cloud files are
$service = $client->objectStoreService('cloudFiles', 'LON');
// Get the object container
$container_name = "AAAA";
// Get the object container
try {
// If container already exists
$container = $service->getContainer($container_name);
} catch (ClientErrorResponseException $e) {
// If the container doesn't exist, create it
$container = $service->createContainer($container_name);
}
$files = $container->objectList();
return $files;
}
function show_form() {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
Select a PDF File: <input type="file" name="file_to_be_uploaded" />
<input type="submit" name="submit" value="Send to Rackspace Cloud Files!" />
</form>
<?php
if ($files = getFileList()) {
foreach ($files as $file) {
?>
<br>
<?php echo $file->getName(); ?>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?view_file=" . urlencode($file->getName()); ?>">View</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?delete_file=" . urlencode($file->getName()); ?>">Delete</a>
<?php
}
}
}
function view_file() {
// Lets connect to Rackspace
$client = new Rackspace(Rackspace::UK_IDENTITY_ENDPOINT, array( // Note the use of
'username' => USER,
'apiKey' => PSWD
));
// Create a service object to use the object store service. We give it the region name on LON (For London) as this is where are server and cloud files are
$service = $client->objectStoreService('cloudFiles', 'LON');
// Get the object container
$container_name = "AAAA";
// Get the object container
try {
// If container already exists
$container = $service->getContainer($container_name);
} catch (ClientErrorResponseException $e) {
// If the container doesn't exist, create it
$container = $service->createContainer($container_name);
}
$file_name = $_GET['view_file'];
$file = $container->getObject($file_name);
$file_type = $file->getContentType();
header("Cache-Control:no-cache, must-revalidate"); // HTTP/1.1
header("Expires:Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Type: " . $file_type);
$output = fopen("php://output", "w");
if (fwrite($output, $file->getContent()) === FALSE) {
$status = "* ERROR - Cannot write to php://output\n\n";
} else {
$status = "* File successfully written to php://output\n\n";
}
//fclose($output);
return;
}
function delete_file() {
// Lets connect to Rackspace
$client = new Rackspace(Rackspace::UK_IDENTITY_ENDPOINT, array( // Note the use of
'username' => USER,
'apiKey' => PSWD
));
// Create a service object to use the object store service. We give it the region name on LON (For London) as this is where are server and cloud files are
$service = $client->objectStoreService('cloudFiles', 'LON');
// Get the object container
$container_name = "AAAA";
// Get the object container
try {
// If container already exists
$container = $service->getContainer($container_name);
} catch (ClientErrorResponseException $e) {
// If the container doesn't exist, create it
return;
}
$file_name = $_GET['delete_file'];
$file = $container->getObject($file_name);
$file->delete();
return;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment