From 102b7bf39e2c1f86f707a3f5963dba456fc87395 Mon Sep 17 00:00:00 2001 From: Roni Yusuf Manalu Date: Mon, 12 Nov 2018 10:18:38 +0700 Subject: [PATCH 1/2] Added getUrl method to support Storage::url --- .gitignore | 1 + README.md | 11 ------- src/AzureBlobStorageAdapter.php | 41 +++++++++++++++++++++++++++ src/AzureStorageServiceProvider.php | 5 ++-- tests/AzureBlobStorageAdapterTest.php | 19 +++++++++++++ 5 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 src/AzureBlobStorageAdapter.php create mode 100644 tests/AzureBlobStorageAdapterTest.php diff --git a/.gitignore b/.gitignore index 344818a..80dfa65 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ vendor/ composer.lock build/ infection-log.txt +/.idea diff --git a/README.md b/README.md index c22f982..f081772 100644 --- a/README.md +++ b/README.md @@ -29,14 +29,3 @@ Then add this to the `disks` section of `config/filesystems.php`: ``` Finally, add the fields `AZURE_STORAGE_NAME`, `AZURE_STORAGE_KEY` and `AZURE_STORAGE_CONTAINER` to your `.env` file with the appropriate credentials. Then you can set the `azure` driver as either your default or cloud driver and use it to fetch and retrieve files as usual. - -Constructing a URL ------------------- - -This driver doesn't support the `Storage::url($path)` method, and adding support as a third-party package doesn't appear to be practical. However, you can construct a URL to retrieve the asset as follows: - -```php -$url = 'https://' . config('filesystems.disks.azure.name'). '.blob.core.windows.net/' . config('filesystems.disks.azure.container') . '/' . $filename; -``` - -You may want to create a helper function for this. diff --git a/src/AzureBlobStorageAdapter.php b/src/AzureBlobStorageAdapter.php new file mode 100644 index 0000000..ea0e208 --- /dev/null +++ b/src/AzureBlobStorageAdapter.php @@ -0,0 +1,41 @@ +baseFileUrl = $client->getPsrPrimaryUri().$container; + } + + /** + * Get the file URL by given path. + * + * @param string $path + * @return string + */ + public function getUrl($path) + { + return $this->baseFileUrl.'/'.$path; + } +} diff --git a/src/AzureStorageServiceProvider.php b/src/AzureStorageServiceProvider.php index d56b4fd..9c660d1 100644 --- a/src/AzureStorageServiceProvider.php +++ b/src/AzureStorageServiceProvider.php @@ -2,11 +2,10 @@ namespace Matthewbdaly\LaravelAzureStorage; -use Storage; -use League\Flysystem\Filesystem; +use Illuminate\Support\Facades\Storage; use Illuminate\Support\ServiceProvider; +use League\Flysystem\Filesystem; use MicrosoftAzure\Storage\Blob\BlobRestProxy; -use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter; /** * Service provider for Azure Blob Storage diff --git a/tests/AzureBlobStorageAdapterTest.php b/tests/AzureBlobStorageAdapterTest.php new file mode 100644 index 0000000..2ba197c --- /dev/null +++ b/tests/AzureBlobStorageAdapterTest.php @@ -0,0 +1,19 @@ +assertEquals('https://azure_account.blob.core.windows.net/azure_container/test.txt', $adapter->getUrl('test.txt')); + } +} From 593c066c19c5f69d310906e82f8274d2f9ec0113 Mon Sep 17 00:00:00 2001 From: Roni Yusuf Manalu Date: Mon, 12 Nov 2018 10:34:09 +0700 Subject: [PATCH 2/2] Added one more test --- tests/AzureBlobStorageAdapterTest.php | 8 ++++++++ tests/TestCase.php | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/AzureBlobStorageAdapterTest.php b/tests/AzureBlobStorageAdapterTest.php index 2ba197c..7b37265 100644 --- a/tests/AzureBlobStorageAdapterTest.php +++ b/tests/AzureBlobStorageAdapterTest.php @@ -16,4 +16,12 @@ public function it_correctly_generates_the_file_url() $this->assertEquals('https://azure_account.blob.core.windows.net/azure_container/test.txt', $adapter->getUrl('test.txt')); } + + /** @test */ + public function it_now_supports_the_url_method() + { + $storage = $this->app['filesystem']; + + $this->assertEquals('https://my_azure_storage_name.blob.core.windows.net/MY_AZURE_STORAGE_CONTAINER/test.txt', $storage->url('test.txt')); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 516c36c..f68cd19 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -25,7 +25,7 @@ protected function getEnvironmentSetUp($app) $app['config']->set('filesystems.disks.azure', [ 'driver' => 'azure', 'name' => 'MY_AZURE_STORAGE_NAME', - 'key' => 'MY_AZURE_STORAGE_KEY', + 'key' => base64_encode('MY_AZURE_STORAGE_KEY'), 'container' => 'MY_AZURE_STORAGE_CONTAINER', ]); }