Skip to content

Instantly share code, notes, and snippets.

@0GiS0
Last active January 31, 2019 19:51
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 0GiS0/3a034fd64379959d6320c1913e7b73fb to your computer and use it in GitHub Desktop.
Save 0GiS0/3a034fd64379959d6320c1913e7b73fb to your computer and use it in GitHub Desktop.
<?php
namespace App\Service;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use Psr\Log\LoggerInterface;
class BlobService
{
private $logger;
private $blobClient;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
$this->blobClient = BlobRestProxy::createBlobService($_SERVER['AZURE_STORAGE_CONNECTION_STRING']);
}
public function allContainers()
{
try {
$container_list = $this->blobClient->listContainers();
return $container_list->getContainers();
} catch (ServiceException $exception) {
$this->logger->error('failed to get all containers: ' . $exception->getCode() . ':' . $exception->getMessage());
throw $exception;
}
}
public function allBlobs($container = 'images')
{
try {
$result = $this->blobClient->listBlobs($container);
return $result->getBlobs();
} catch (ServiceException $exception) {
$this->logger->error('failed to get all blobs: ' . $exception->getCode() . ':' . $exception->getMessage());
throw $exception;
}
}
public function upload($file, $container = 'images')
{
try {
$content = file_get_contents($file);
$this->blobClient->createBlockBlob($container, $file->getClientOriginalName(), $content);
} catch (ServiceException $exception) {
$this->logger->error('failed to upload the file: ' . $exception->getCode() . ':' . $exception->getMessage());
throw $exception;
}
}
public function delete($blobName, $container = 'images')
{
try {
$this->blobClient->deleteBlob($container, $blobName);
} catch (ServiceException $exception) {
$this->logger->error('failed to delete the file: ' . $exception->getCode() . ':' . $exception->getMessage());
throw $exception;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment