PHP code example of alinauf / laravel-azure-storage
1. Go to this page and download the library: Download alinauf/laravel-azure-storage library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Illuminate\Support\Facades\Storage;
// Use the azure disk
$disk = Storage::disk('azure');
// Upload a file
$disk->put('path/to/file.txt', 'Contents');
// Upload from a stream
$disk->putStream('path/to/file.txt', fopen('/local/file.txt', 'r'));
// Read a file
$contents = $disk->get('path/to/file.txt');
// Check if file exists
if ($disk->exists('path/to/file.txt')) {
// ...
}
// Delete a file
$disk->delete('path/to/file.txt');
// Copy a file
$disk->copy('source.txt', 'destination.txt');
// Move a file
$disk->move('old-location.txt', 'new-location.txt');
// Get file size in bytes
$size = $disk->size('path/to/file.txt');
// Get last modified timestamp
$timestamp = $disk->lastModified('path/to/file.txt');
// Get MIME type
$mimeType = $disk->mimeType('path/to/file.txt');
// List files in a directory
$files = $disk->files('path/to/directory');
// List all files recursively
$allFiles = $disk->allFiles('path/to/directory');
// List directories
$directories = $disk->directories('path/to/directory');
// Delete a directory and all its contents
$disk->deleteDirectory('path/to/directory');
// Get the public URL
$url = $disk->url('path/to/file.jpg');
// Returns: https://your-account.blob.core.windows.net/container/path/to/file.jpg
// Generate a temporary signed URL (SAS token)
$url = $disk->temporaryUrl('path/to/file.jpg', now()->addHours(1));
// With custom permissions
$url = $disk->temporaryUrl('path/to/file.jpg', now()->addHours(1), [
'permissions' => 'rw', // read + write
]);
use Illuminate\Support\Facades\Storage;
Storage::put('file.txt', 'Contents');
$url = Storage::url('file.txt');
use Illuminate\Support\Facades\Storage;
$disk = Storage::disk('azure');
// Store a file (private by default)
$disk->put('invoices/invoice-001.pdf', $pdfContents);
// Generate a temporary URL for display (expires in 30 minutes)
$url = $disk->temporaryUrl('invoices/invoice-001.pdf', now()->addMinutes(30));
// Use in a Blade template
// <a href="{{ $url }}">Download Invoice</a>
// <img src="{{ $url }}" alt="Preview">
// Check the container's visibility
$visibility = $disk->getVisibility('any-path'); // 'public' or 'private'
// Set visibility (only when allow_set is true)
$disk->setVisibility('any-path', 'private');
use AliNauf\AzureStorage\Support\SasTokenGenerator;
$generator = new SasTokenGenerator(
accountName: config('azure-storage.account_name'),
accountKey: config('azure-storage.account_key'),
);
// Generate a blob SAS token
$sas = $generator->generateBlobSas(
container: 'mycontainer',
blob: 'path/to/file.jpg',
expiry: now()->addHours(2),
permissions: 'r', // read only
);
// Generate a full signed URL
$url = $generator->generateSignedUrl(
container: 'mycontainer',
blob: 'path/to/file.jpg',
expiry: now()->addHours(2),
);
// Generate a container-level SAS token
$containerSas = $generator->generateContainerSas(
container: 'mycontainer',
expiry: now()->addDays(7),
permissions: 'rl', // read + list
);
use AliNauf\AzureStorage\AzureBlobStorageClient;
$client = new AzureBlobStorageClient(
accountName: config('azure-storage.account_name'),
accountKey: config('azure-storage.account_key'),
container: config('azure-storage.container'),
);
// Upload a blob
$response = $client->putBlob('path/to/file.txt', 'Contents', 'text/plain');
// Get blob properties
$properties = $client->getBlobProperties('path/to/file.txt');
// Returns: ['content_length' => 123, 'content_type' => 'text/plain', 'last_modified' => 1234567890, 'etag' => '...']
// List blobs with prefix
$result = $client->listBlobs('path/to/', 100);
// Returns: ['blobs' => [...], 'next_marker' => '...']
// Copy a blob
$response = $client->copyBlob('source.txt', 'destination.txt');