Last active
August 29, 2015 14:07
-
-
Save jmurowaniecki/6f66f708914924e4821f to your computer and use it in GitHub Desktop.
Windows Azure Blob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__.'/../../system/azure/autoload.php'; | |
use WindowsAzure\Common\ServicesBuilder; | |
use WindowsAzure\Blob\Models\CreateContainerOptions; | |
use WindowsAzure\Blob\Models\PublicAccessType; | |
use WindowsAzure\Common\ServiceException; | |
$config = array( | |
'AccountName' => '', | |
'AccountKey' => '', | |
'DefaultEndpointsProtocol' => 'http', | |
); | |
class Azure { | |
var $connectionString = false; | |
var $blobRestProxy = false; | |
public function __construct($DefaultEndpointsProtocol = false, $AccountName = false, $AccountKey = false) { | |
if (!$DefaultEndpointsProtocol || !$AccountName || !$AccountKey) | |
return $this; | |
$this->config($DefaultEndpointsProtocol, $AccountName, $AccountKey); | |
} | |
public function config($DefaultEndpointsProtocol = null, $AccountName = null, $AccountKey = null) { | |
$this->connectionString = "DefaultEndpointsProtocol={$DefaultEndpointsProtocol};AccountName={$AccountName};AccountKey={$AccountKey}"; | |
$this->blobRestProxy = ServicesBuilder::getInstance()->createBlobService($this->connectionString); | |
} | |
public function createContainer($container = false) { | |
if (!$container) | |
return false; | |
$options = new CreateContainerOptions(); | |
$options->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS); | |
$options->addMetaData('created', date('U')); | |
try { | |
$this->blobRestProxy->createContainer($container, $options); | |
} | |
catch (ServiceException $e) { | |
// Implementar no futuro debug pra db para $e->getCode() e $e->getMessage() | |
// die($e->getCode().':'.$e->getMessage()); | |
return false; | |
} | |
return true; | |
} | |
public function uploadBlob($filename = false, $container = false, $content = false) { | |
if (!$filename || !file_exists($filename) || !$container) | |
return false; | |
if (!$content) | |
$content = fopen($filename, 'r'); | |
try { | |
$this->blobRestProxy->createBlockBlob($container, $filename, $content); | |
} | |
catch(ServiceException $e) { | |
// Implementar no futuro debug pra db para $e->getCode() e $e->getMessage() | |
// die($e->getCode().':'.$e->getMessage()); | |
return false; | |
} | |
return true; | |
} | |
public function listContainer($container = false) { | |
if (!$container) | |
return false; | |
$blobs = array(); | |
try { | |
$blob_list = $this->blobRestProxy->listBlobs($container); | |
foreach($blob_list->getBlobs() as $blob) | |
$blobs[$blob->getName()] = $blob->getUrl(); | |
} | |
catch(ServiceException $e) { | |
// Implementar no futuro debug pra db para $e->getCode() e $e->getMessage() | |
// die($e->getCode().':'.$e->getMessage()); | |
return false; | |
} | |
return $blobs; | |
} | |
public function getBlob($container = false, $filename = false) { | |
if (!$container || !$filename) | |
return false; | |
return $this->blobRestProxy->getBlob($container, $filename); | |
} | |
} | |
/* | |
$blob = new Azure($config['DefaultEndpointsProtocol'], $config['AccountName'], $config['AccountKey']); | |
echo 'container temporary is '.($blob->createContainer('temporary') ? 'created' : 'failed'); | |
echo 'uploading file '.($blob->uploadBlob('./elite.jpg', 'temporary') ? 'uploaded' : 'failed')."\n\n\n"; | |
$blob->listContainer('temporary'); | |
print_r($blob->getBlob('temporary', './elite.jpg')); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Methods are explained here.