PHP code example of axinter / azureblobstorage

1. Go to this page and download the library: Download axinter/azureblobstorage 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/ */

    

axinter / azureblobstorage example snippets


use AxInter\AzureBlobStorage\Config;
use AxInter\AzureBlobStorage\FileSystem;

$connectionString = 'DefaultEndpointsProtocol=https;AccountName=your-account;AccountKey=your-key;EndpointSuffix=core.windows.net';
$config = new Config($connectionString);
$fileSystem = new FileSystem($config, 'your-container-name');

$connectionString = 'DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1';
$config = new Config($connectionString);
$fileSystem = new FileSystem($config, 'test-container');

// Create a container
$fileSystem->createContainer();

// Check if container exists
if ($fileSystem->containerExists()) {
    echo "Container exists!";
}

// Delete a container
$fileSystem->deleteContainer();

// Write string content
$fileSystem->write('path/to/file.txt', 'Hello, Azure!');

// Upload from stream (efficient for large files)
$stream = fopen('/path/to/large-file.pdf', 'r');
$fileSystem->writeStream('documents/large-file.pdf', $stream);
fclose($stream);

// Read file content as string
$content = $fileSystem->read('path/to/file.txt');

// Download as stream
$stream = $fileSystem->readStream('documents/large-file.pdf');
file_put_contents('/local/path/file.pdf', $stream);

// Check if file exists
if ($fileSystem->exists('path/to/file.txt')) {
    echo "File exists!";
}

// Get file information
$size = $fileSystem->getSize('path/to/file.txt');
$mimeType = $fileSystem->getMimeType('path/to/file.txt');
$url = $fileSystem->getUrl('path/to/file.txt');

// Copy file
$fileSystem->copy('source.txt', 'destination.txt');

// Move file
$fileSystem->move('old-path.txt', 'new-path.txt');

// Delete file
$fileSystem->delete('path/to/file.txt');

// List all files in container
$files = $fileSystem->list();

// List files with prefix
$files = $fileSystem->list('documents/');

foreach ($files as $file) {
    echo "Name: {$file['name']}\n";
    echo "Size: {$file['size']} bytes\n";
    echo "Last Modified: {$file['last_modified']}\n";
    echo "Content Type: {$file['content_type']}\n";
}

// Set custom metadata
$fileSystem->setMetadata('path/to/file.txt', [
    'author' => 'John Doe',
    'department' => 'Engineering',
    'version' => '1.0'
]);

// Retrieve metadata
$metadata = $fileSystem->getMetadata('path/to/file.txt');
echo $metadata['author']; // John Doe

// Write operations automatically create versions
$fileSystem->write('document.txt', 'Version 1');
$fileSystem->write('document.txt', 'Version 2'); // Previous version saved automatically

// List all versions
$versions = $fileSystem->listVersions('document.txt');
foreach ($versions as $version) {
    echo "Version ID: {$version['version_id']}\n";
    echo "Is Current: {$version['is_current_version']}\n";
    echo "Modified: {$version['last_modified']}\n";
}

// Get specific version
$oldContent = $fileSystem->getVersion('document.txt', '1703001234');

// Restore previous version (creates a new current version)
$fileSystem->restoreVersion('document.txt', '1703001234');

// Delete a specific version
$fileSystem->deleteVersion('document.txt', '1703001234');

use AxInter\AzureBlobStorage\Exceptions\BlobNotFoundException;
use AxInter\AzureBlobStorage\Exceptions\InvalidStreamException;
use AxInter\AzureBlobStorage\Exceptions\VersionNotFoundException;
use AxInter\AzureBlobStorage\Exceptions\AzureBlobStorageException;

try {
    $content = $fileSystem->read('non-existent.txt');
} catch (BlobNotFoundException $e) {
    echo "File not found: " . $e->getMessage();
} catch (AzureBlobStorageException $e) {
    echo "Storage error: " . $e->getMessage();
}