Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Merge 931c7c4 into f18c3a9
Browse files Browse the repository at this point in the history
  • Loading branch information
SolveSoul committed Sep 23, 2020
2 parents f18c3a9 + 931c7c4 commit 4c5dca6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
54 changes: 48 additions & 6 deletions src/AzureBlobStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Matthewbdaly\LaravelAzureStorage;

use Illuminate\Support\Arr;
use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter as BaseAzureBlobStorageAdapter;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use Matthewbdaly\LaravelAzureStorage\Exceptions\InvalidCustomUrl;
use MicrosoftAzure\Storage\Blob\BlobSharedAccessSignatureHelper;
use MicrosoftAzure\Storage\Common\Internal\Resources;

/**
* Blob storage adapter
Expand Down Expand Up @@ -32,16 +35,24 @@ final class AzureBlobStorageAdapter extends BaseAzureBlobStorageAdapter
*/
private $url;

/**
* The account key to access the storage
*
* @var string
*/
private $key;

/**
* Create a new AzureBlobStorageAdapter instance.
*
* @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $client Client.
* @param string $container Container.
* @param string|null $url URL.
* @param string|null $prefix Prefix.
* @throws InvalidCustomUrl URL is not valid.
* @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $client Client.
* @param string $container Container.
* @param string $key
* @param string|null $url URL.
* @param string|null $prefix Prefix.
* @throws InvalidCustomUrl URL is not valid.
*/
public function __construct(BlobRestProxy $client, string $container, string $url = null, $prefix = null)
public function __construct(BlobRestProxy $client, string $container, string $key, string $url = null, $prefix = null)
{
parent::__construct($client, $container, $prefix);
$this->client = $client;
Expand All @@ -50,6 +61,7 @@ public function __construct(BlobRestProxy $client, string $container, string $ur
throw new InvalidCustomUrl();
}
$this->url = $url;
$this->key = $key;
$this->setPathPrefix($prefix);
}

Expand All @@ -66,4 +78,34 @@ public function getUrl(string $path)
}
return $this->client->getBlobUrl($this->container, $path);
}

/**
* Generate Temporary Url with SAS query
*
* @param string $path
* @param \Datetime|string $ttl
* @param array $options
* @return string
*/
public function getTemporaryUrl(string $path, $ttl, array $options = [])
{
$sas = new BlobSharedAccessSignatureHelper($this->client->getAccountName(), $this->key);
$sasString = $sas->generateBlobServiceSharedAccessSignatureToken(
Resources::RESOURCE_TYPE_BLOB,
$this->container . '/' . $path,
Arr::get($options, 'signed_permissions', 'r'),
$ttl,
Arr::get($options, 'signed_start', ''),
Arr::get($options, 'signed_ip', ''),
Arr::get($options, 'signed_protocol', 'https'),
Arr::get($options, 'signed_identifier', ''),
Arr::get($options, 'cache_control', ''),
Arr::get($options, 'content_disposition', ''),
Arr::get($options, 'content_encoding', ''),
Arr::get($options, 'content_language', ''),
Arr::get($options, 'content_type', '')
);

return sprintf('%s?%s', $this->getUrl($path), $sasString);
}
}
1 change: 1 addition & 0 deletions src/AzureStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function boot()
$adapter = new AzureBlobStorageAdapter(
$client,
$config['container'],
$config['key'],
$config['url'] ?? null,
$config['prefix'] ?? null
);
Expand Down
21 changes: 16 additions & 5 deletions tests/AzureBlobStorageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function it_correctly_generates_the_file_url()
{
$client = BlobRestProxy::createBlobService('DefaultEndpointsProtocol=https;AccountName=azure_account;AccountKey=' . base64_encode('azure_key'));

$adapter = new AzureBlobStorageAdapter($client, 'azure_container');
$adapter = new AzureBlobStorageAdapter($client, 'azure_container', 'azure_key',);

$this->assertEquals('https://azure_account.blob.core.windows.net/azure_container/test.txt', $adapter->getUrl('test.txt'));
}
Expand All @@ -37,7 +37,7 @@ public function it_supports_preceding_slash()
public function it_supports_custom_url()
{
$client = BlobRestProxy::createBlobService('DefaultEndpointsProtocol=https;AccountName=azure_account;AccountKey=' . base64_encode('azure_key'));
$adapter = new AzureBlobStorageAdapter($client, 'azure_container', 'https://example.com');
$adapter = new AzureBlobStorageAdapter($client, 'azure_container','azure_key', 'https://example.com');

$this->assertEquals('https://example.com/azure_container/test.txt', $adapter->getUrl('test.txt'));
}
Expand All @@ -46,24 +46,35 @@ public function it_supports_custom_url()
public function it_supports_custom_url_with_root_container()
{
$client = BlobRestProxy::createBlobService('DefaultEndpointsProtocol=https;AccountName=azure_account;AccountKey=' . base64_encode('azure_key'));
$adapter = new AzureBlobStorageAdapter($client, '$root', 'https://example.com');
$adapter = new AzureBlobStorageAdapter($client, '$root','azure_key', 'https://example.com');

$this->assertEquals('https://example.com/test.txt', $adapter->getUrl('test.txt'));
}

/** @test */
public function it_supports_temporary_url()
{
$client = BlobRestProxy::createBlobService('DefaultEndpointsProtocol=https;AccountName=azure_account;AccountKey=' . base64_encode('azure_key'));
$adapter = new AzureBlobStorageAdapter($client, 'azure_container', 'azure_key', null, 'test_path');
$tempUrl = $adapter->getTemporaryUrl('test_path/test.txt', now()->addMinutes(1));

$this->assertStringStartsWith('https://azure_account.blob.core.windows.net/azure_container/test_path/test.txt', $tempUrl);
$this->assertStringContainsString('sig=', $tempUrl);
}

/** @test */
public function it_handles_invalid_custom_url()
{
$this->expectException('Matthewbdaly\LaravelAzureStorage\Exceptions\InvalidCustomUrl');
$client = BlobRestProxy::createBlobService('DefaultEndpointsProtocol=https;AccountName=azure_account;AccountKey=' . base64_encode('azure_key'));
$adapter = new AzureBlobStorageAdapter($client, 'azure_container', 'foo');
$adapter = new AzureBlobStorageAdapter($client, 'azure_container', 'azure_key','foo');
}

/** @test */
public function it_handles_custom_prefix()
{
$client = BlobRestProxy::createBlobService('DefaultEndpointsProtocol=https;AccountName=azure_account;AccountKey=' . base64_encode('azure_key'));
$adapter = new AzureBlobStorageAdapter($client, 'azure_container', null, 'test_path');
$adapter = new AzureBlobStorageAdapter($client, 'azure_container', 'azure_key',null, 'test_path');

$this->assertEquals('https://azure_account.blob.core.windows.net/azure_container/test_path/test.txt', $adapter->getUrl('test_path/test.txt'));
}
Expand Down

0 comments on commit 4c5dca6

Please sign in to comment.