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

Commit

Permalink
Merge pull request #4 from rymanalu/storage-url
Browse files Browse the repository at this point in the history
Added getUrl method to support Storage::url
  • Loading branch information
matthewbdaly authored Nov 12, 2018
2 parents 0999258 + 593c066 commit 99fdec2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ vendor/
composer.lock
build/
infection-log.txt
/.idea
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
41 changes: 41 additions & 0 deletions src/AzureBlobStorageAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Matthewbdaly\LaravelAzureStorage;

use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter as BaseAzureBlobStorageAdapter;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;

class AzureBlobStorageAdapter extends BaseAzureBlobStorageAdapter
{
/**
* Base file URL.
*
* @var string
*/
protected $baseFileUrl;

/**
* Create a new AzureBlobStorageAdapter instance.
*
* @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $client
* @param string $container
* @param string|null $prefix
*/
public function __construct(BlobRestProxy $client, $container, $prefix = null)
{
parent::__construct($client, $container, $prefix);

$this->baseFileUrl = $client->getPsrPrimaryUri().$container;
}

/**
* Get the file URL by given path.
*
* @param string $path
* @return string
*/
public function getUrl($path)
{
return $this->baseFileUrl.'/'.$path;
}
}
5 changes: 2 additions & 3 deletions src/AzureStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/AzureBlobStorageAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests;

use Matthewbdaly\LaravelAzureStorage\AzureBlobStorageAdapter;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;

class AzureBlobStorageAdapterTest extends TestCase
{
/** @test */
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');

$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'));
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
}
Expand Down

0 comments on commit 99fdec2

Please sign in to comment.