blob 当前是否有 SAS url?

Does a blob currently have a SAS url?

我有一个 MVC 5 互联网应用程序,可将文件上传到 Azure blob 存储。我目前已经实现了为容器中的 blob 创建 SAS 的代码。

我有一个 MVC 视图,它通过 <img src> 标签列出了许多不同对象的许多图像。这些图像中的每一个都是相同的图像。我的意思是它们具有相同的文件名并且在同一个容器中。

是否可以检查一个 blob 是否已经有一个当前的 SAS url,如果有,检索那个 SAS url?

提前致谢。

编辑

这是我获取 sas 的代码 url:

public string GetBlobSasUri(string containerName, string fileName)
{
    CloudBlobContainer container = GetCloudBlobContainer(containerName);
    //Get a reference to a blob within the container.
    CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

    //Set the expiry time and permissions for the blob.
    //In this case the start time is specified as a few minutes in the past, to mitigate clock skew.
    //The shared access signature will be valid immediately.
    SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
    sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
    sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(4);
    sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

    //Generate the shared access signature on the blob, setting the constraints directly on the signature.
    string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);

    //Return the URI string for the container, including the SAS token.
    return blob.Uri + sasBlobToken;
}

如果我传入 containerName"Test" 以及 FileName"Example.png",则会创建并 returned。如果我随后将相同的参数传递给函数,我是否可以检查 FileName 是否已经为其创建了 SAS,如果是,return 相同的 SAS url?

SAS 是共享访问签名。这意味着 - 它只是识别给定资源的数字签名。它不是 attached 以任何方式指向资源。 SAS 不仅仅是一些随机生成的令牌,它被分配给资源。 SAS 在请求期间进行验证 - 评估、签名检查、资源检查、操作检查。因此,服务本身(blob 服务)和该服务中的资源(容器、blob)不知道 SAS 是否存在。

话虽如此,您有两种可能的方法:

  • 具有存储 SAS 的缓存,其中缓存键将是您的 blob URI。应根据 SAS 生命周期配置适当的缓存过期时间
  • 为每个请求创建 SAS(以防您无法关联请求)

这是 Constructing Shared Access Signature URI 上的 MSDN 文档。