1

是否有任何文档可以获取 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 文件快照下载文件会缺少什么?

4

2 回答 2

1

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

您需要做的是将共享的快照日期/时间附加到您的 SAS URL。就像是:

https://account.file.core.windows.net/share/file.png?sastoken&snapshot=2021-05-01T13:49:56.0000000Z
于 2021-05-05T00:00:23.597 回答
1

这是有效的代码:

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;
}
于 2021-05-05T08:32:52.093 回答