Skip to content

consilience/flysystem-azure-file-storage

Repository files navigation

Latest Stable Version Total Downloads Latest Unstable Version License

Azure File Storage adapter for Flysystem

This repo is fork of League\Flysystem\Azure with the underlying Azure API library changed from microsoft/azure-storage to microsoft/azure-storage-file. The original driver supports Azure blob storage, with a flat binary object structure. This driver supports Azure file storage, which includes directory capabilities.

A separate service provider package for Laravel 5.5+ is available here: https://github.com/academe/laravel-azure-file-storage-driver The service provider allows Azure File Storage shares tbe be used as a native filesystem within Laravel.

Installation

Install package

composer require consilience/flysystem-azure-file-storage

How to use this driver

Note: if you are using Laravel then the filesystem driver will wrap and abstract all of this for you.

use League\Flysystem\Filesystem;
use Consilience\Flysystem\Azure\AzureFileAdapter;
use MicrosoftAzure\Storage\File\FileRestProxy;
use Illuminate\Support\ServiceProvider;

// A helper method for constructing the connectionString may be usedful,
// if there is a demand.

$connectionString = sprintf(
    'DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s',
    '{storage account name}',
    '{file storage key}'
);

$config = [
    'endpoint' => $connectionString,
    'container' => '{file share name}',
    // Optional to prevent directory deletion recursively deleting
    // all descendant files and direcories.
    //'disableRecursiveDelete' => true,
    // Optional driver options can also be added here. e.g. CacheControl, Metadata.
];

$fileService = FileRestProxy::createFileService(
    $connectionString,
    [] // $optionsWithMiddlewares
);

$filesystem = new Filesystem(new AzureFileAdapter(
    $fileService,
    $config,
    'optional-directory-prefix'
));

// Now the $filesystem object can be used as a standard
// Flysystem file system.
// See https://flysystem.thephpleague.com/api/

// A few examples:

$content    = $filesystem->read('path/to/my/file.txt');
$resource   = $filesystem->readResource('path/to/my/file.txt');
$success    = $filesystem->createDir('new/directory/here');
$success    = $filesystem->rename('path/to/my/file.txt', 'some/other/folder/another.txt');

// The URL of a file can be found like this:

$url = $filesystem->getAdapter()->getUrl('path/to/my/foo.bar');

Testing

Set up .env and run live tests:

composer install
vendor/bin/phpunit --testsuite flysystem-azure-live-tests

These will create/delete a few test files and directories in the root of the Azure file share.