Get Azure File from snapshot with php

202 views Asked by At

Is there any documentation to get a SAS URL to download a file from a Snapshot of a Azure Share File?

Using this is easy to download a direct Azure File with SAS, but not any snapshot: GenerateFileDownloadLinkWithSAS (https://github.com/Azure/azure-storage-php/blob/master/samples/FileSamples.php)

Here my code:

use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use MicrosoftAzure\Storage\Common\Internal\Resources;
use MicrosoftAzure\Storage\Common\Internal\StorageServiceSettings;
use MicrosoftAzure\Storage\Common\Models\Range;
use MicrosoftAzure\Storage\Common\Models\Metrics;
use MicrosoftAzure\Storage\Common\Models\RetentionPolicy;
use MicrosoftAzure\Storage\Common\Models\ServiceProperties;
use MicrosoftAzure\Storage\File\FileRestProxy;
use MicrosoftAzure\Storage\File\FileSharedAccessSignatureHelper;
use MicrosoftAzure\Storage\File\Models\CreateShareOptions;
use MicrosoftAzure\Storage\File\Models\ListSharesOptions;
use MicrosoftAzure\Storage\File\Models\ListDirectoriesAndFilesOptions;

function MapFileURL($shareName,$filePath)
{
    global $fileRestProxy;
    global $mapConString;
    
    $prepareFilePath = implode('/', array_map(function ($v)
    {
        return rawurlencode($v);
    }, explode('/', $filePath))
    );
    
    // Create a SharedAccessSignatureHelper
    $settings = StorageServiceSettings::createFromConnectionString($mapConString);
    $accountName = $settings->getName();
    $accountKey = $settings->getKey();
    $helper = new FileSharedAccessSignatureHelper(
        $accountName,
        $accountKey
        );
    $endDate=MapIsoDate(time() + 13300);
    // Generate a file readonly SAS token
    // Refer to following link for full candidate values to construct a service level SAS
    // https://learn.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas
    $sas = $helper->generateFileServiceSharedAccessSignatureToken(
        Resources::RESOURCE_TYPE_FILE,
        $shareName . "/" . $prepareFilePath,
        'r',     // Read
        $endDate
        );
    $connectionStringWithSAS = Resources::FILE_ENDPOINT_NAME.'='.'https://'.$accountName.'.'.Resources::FILE_BASE_DNS_NAME.';'.Resources::SAS_TOKEN_NAME.'='.$sas;
    $fileClientWithSAS = FileRestProxy::createFileService($connectionStringWithSAS);
    // Get a downloadable file URL
    $fileUrlWithSAS = sprintf(
        '%s%s?%s',
        (string)$fileClientWithSAS->getPsrPrimaryUri(),
        $shareName . "/" . $prepareFilePath,
        $sas
        );
    return $fileUrlWithSAS;
}

What would be missing to be able to download the file from a Azure File snapshot?

2

There are 2 answers

0
Gaurav Mantri On BEST ANSWER

What would be missing to be able to download the file from a Azure File snapshot?

What you need to do is append the share's snapshot date/time to your SAS URL. Something like:

https://account.file.core.windows.net/share/file.png?sastoken&snapshot=2021-05-01T13:49:56.0000000Z
0
karrtojal On

Here is the code that works:

function MapSnapshotFileURL($shareName, $filePath, $snapshotTime)
{
    global $fileRestProxy;
    global $mapConString;
    
    // Preparar path para enviar a la funciĆ³n azure.
    $prepareFilePath = implode('/', array_map(function ($v)
    {
        return rawurlencode($v);
    }, explode('/', $filePath))
    );
    
    // Create a SharedAccessSignatureHelper
    $settings = StorageServiceSettings::createFromConnectionString($mapConString);
    $accountName = $settings->getName();
    $accountKey = $settings->getKey();
    $helper = new FileSharedAccessSignatureHelper(
        $accountName,
        $accountKey
        );
    $endDate=MapIsoDate(time() + 13300);
    //$endDate='2019-07-16T08:30:00Z';
    // Generate a file readonly SAS token
    // Refer to following link for full candidate values to construct a service level SAS
    // https://learn.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas
    $sas = $helper->generateFileServiceSharedAccessSignatureToken(
        Resources::RESOURCE_TYPE_FILE,
        $shareName . "/" . $prepareFilePath,
        'r',     // Read
        $endDate // '2020-01-01T08:30:00Z'      // A valid ISO 8601 format expiry time
        );
    $connectionStringWithSAS = Resources::FILE_ENDPOINT_NAME.'='.'https://'.$accountName.'.'.Resources::FILE_BASE_DNS_NAME.';'.Resources::SAS_TOKEN_NAME.'='.$sas;
    $fileClientWithSAS = FileRestProxy::createFileService($connectionStringWithSAS);
    // Get a downloadable file URL
    $fileUrlWithSAS = sprintf(
        '%s%s?%s&%s',
        (string)$fileClientWithSAS->getPsrPrimaryUri(),
        $shareName . "/" . $prepareFilePath,
        $sas,
        "snapshot=".$snapshotTime
        );
    return $fileUrlWithSAS;
}