flysystem-azure-blob-storage icon indicating copy to clipboard operation
flysystem-azure-blob-storage copied to clipboard

Azure Laravel Configuration

Open derekrprice opened this issue 5 years ago • 3 comments

The Laravel section of the FlySystem docs only directs to the Laravel manual section on File Systems, which contains examples of configuring s3 connections, but not Azure Blob connections. At least one of the two sets of documentation should contain an example configuration for FlySystem's Azure Blob Storage adapter.

derekrprice avatar Aug 22 '20 12:08 derekrprice

Laravel utilizes Flysystem as the backing for it's Filesystem but they aren't related projects, so it doesn't make sense for The PHP League to be responsible for setting up integrations that aren't officially supported by Laravel (which the azure driver isn't). However, it's pretty simple to add Azure Blob Storage to your laravel project, you can either use this package (which, full disclosure, I made) https://github.com/ajoy39/laravel-storage-azure or literally just grab the service provider from that package and add it to your own project, modifying the config variables as necessary. All that package does is add that service provider and tell you how to setup the config options.

Keep in mind that the reason laravel doesn't support azure as a driver out of the box is that the azure driver lacks some functionality that Laravel's Filesystem includes. For example there is no support for temporary URLs, or file URLs in general. The only way to download a file uploaded with this driver is to use the laravel Storage::download() method.

alec-joy avatar Aug 28 '20 14:08 alec-joy

Actually, you can easily generate temporary URLs via SAS tokens. I'm doing it my project already:

private static function getSasUri(Request $request, string $file) {
    $key = new AzureStorageKey(config('app.calibrations_storage_key'));
    $container = config('app.calibrations_storage_container');
    $helper = new BlobSharedAccessSignatureHelper($key['AccountName'], $key['AccountKey']);

    $duration = config('app.calibrations_storage_sas_duration');
    $expires = Carbon::parse("now + $duration", 'UTC');
    // Refer to following link for full candidate values to construct a service level SAS
    // https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas
    $sas = $helper->generateBlobServiceSharedAccessSignatureToken(
        Resources::RESOURCE_TYPE_BLOB,
        "/$container/$file",
        'r',            // Read
        $expires,  // A valid ISO 8601 format expiry time
        '',             // An optional ISO 8601 format start time
        $request->ip(),               // Token is only usable from this client's IP.
    );

    return "{$key['BlobEndpoint']}/$container/$file?$sas";
}

The AzureStorageKey class is a simple one that parses an Azure Blob endpoint configuration string and makes its elements available via a read-only ArrayAccess interface. If you have anonymous access enabled, you can also generate permanent ones, or you could use a really long expiry time on a temporary URL. Not likely to get the time, but if I do, maybe I'll submit a patch for Laravel.

derekrprice avatar Aug 28 '20 15:08 derekrprice

I just meant that Storage::temporaryURL() doesn't work using azure as the driver for Laravel's filesystem, you can implement it yourself as you have done above. I have also thought about trying to add it as something that is natively supported, I think the changes would be made to this package as it's the driver Laravel uses but I haven't really dug into it yet.

alec-joy avatar Aug 28 '20 15:08 alec-joy