Question

There are so many examples about the Shared Access Signature, but I still couldn't figure out what is wrong with my code.

What I'm trying to do is, I'm trying to create a downloadable link for the blob and show it to the user. And examples are usually about how to keep it alive. I'm looking for how to expire it.

I'm able to create downloadable link and it is working but it is not being expired on given time. I create a SharedAccessBlobPolicy and set the SharedAccessExpiryTime property, but it doesn't expire on time.

For instance If I set it for 5 minutes, it is still working after 30 minutes.

SharedAccessBlobPolicy sharedPolicy = new SharedAccessBlobPolicy()
                     {                  
                      SharedAccessExpiryTime =  DateTime.UtcNow.AddMinutes(5),
                      Permissions = SharedAccessBlobPermissions.Read
                      };

Here is my code:

string blobName = "test.txt"; //just for an example

CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("myConnectionString"));
CloudBlobClient client = account.CreateCloudBlobClient();  


// Build shared access signature
CloudBlobContainer container = client.GetContainerReference(ConfigurationManager.AppSettings.Get("myContainerReference"));
container.CreateIfNotExists();


////Get a reference to a blob within the container.

//Create a new stored access policy and define its constraints.
SharedAccessBlobPolicy sharedPolicy = new SharedAccessBlobPolicy()
                 {                  
                  SharedAccessExpiryTime =  DateTime.UtcNow.AddMinutes(5),
                  Permissions = SharedAccessBlobPermissions.Read
                  };


//Get the container's existing permissions.
BlobContainerPermissions permissions = new BlobContainerPermissions();

// The public access setting explicitly specifies that the container is private, 
// so that it can't be accessed anonymously.
permissions.PublicAccess = BlobContainerPublicAccessType.Off;

//Add the new policy to the container's permissions.
string policyName = "myPolicy";

permissions.SharedAccessPolicies.Clear();   
permissions.SharedAccessPolicies.Add(policyName,sharedPolicy);
container.SetPermissions(permissions);

CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
string sasBlobToken = blob.GetSharedAccessSignature(null, policyName);  
string downloadLink = blob.Uri + sasBlobToken;

downloadLink supposed to be working for 5 minutes, but it never dies. (I bet it dies, but just don't know when it dies)

P.S. I'm not sharing whole code here, just FYI that there is no debug error or any other type of errors. It works perfectly fine as downloadable link. It just does not expires on given time.

I couldn't figured out what am I missing. Thanks for your attention

Was it helpful?

Solution

I just tried this and the link expired on me after 5 minutes. Please check if the browser is not caching the file. If that's the case then you will continue to see the files as they are not fetched from storage service on every request. Instead they are served from browser cache only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top