blob目前是否有SAS URL?

时间:2015-01-13 06:32:43

标签: url azure asp.net-mvc-5 azure-storage-blobs

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

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

是否可以检查blob是否已有当前的SAS网址,如果是,则检索该SAS网址?

提前致谢。

修改

以下是获取sas网址的代码:

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",则会创建并返回SAS。如果我然后将相同的参数传递给函数,我是否可以检查FileName是否已经为其创建了SAS,如果是,则返回相同的SAS URL?

1 个答案:

答案 0 :(得分:2)

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

说过你有几种可能的方法:

  • 拥有存储SAS的缓存,缓存密钥将是您的blob URI。应针对SAS生存期配置适当的缓存到期时间
  • 为每个请求创建SAS(如果您无法共同关联请求)

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