使用 php 从快照获取 Azure 文件

Get Azure File from snapshot with php

是否有任何文档可以让 SAS URL 从 Azure 共享文件的快照下载文件?

使用它可以轻松下载带有 SAS 的直接 Azure 文件,但不能下载任何快照: GenerateFileDownloadLinkWithSAS (https://github.com/Azure/azure-storage-php/blob/master/samples/FileSamples.php)

这是我的代码:

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://docs.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;
}

能够从 Azure 文件快照下载文件会缺少什么?

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

您需要做的是将共享快照 date/time 附加到您的 SAS URL。类似于:

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

这是有效的代码:

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://docs.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;
}