I have a SAS token which will expire within 2 minutes.
SAS = AzureClient.GetCloudContainer().GetSharedAccessSignature(new SharedAccessPolicy()
{
SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(1)
}, "readonly");
var sasCreds = new StorageCredentialsSharedAccessSignature(SAS);
CloudStorageAccount _storageAccount = AzureClient.GetCloudStorageAccount();
CloudBlobClient sasBlobClient = new CloudBlobClient(_storageAccount.BlobEndpoint, sasCreds);
CloudBlob sasBlob = sasBlobClient.GetBlobReference("blobname");
Where readonly is the policy name.
Now I am doing the following operation:
using (BlobStream stream = sasBlob.OpenRead())
{
using (FileStream fileStream = File.OpenWrite(@"Smething.txt"))
{
BlobStreamReader(stream,fileStream);
}
}
private void BlobStreamReader(BlobStream blob,Stream OutputStream)
{
int buffersize = 4194304; // 4MB
byte[] data = new byte[buffersize];
do
{
int bytesRead = blob.Read(data,0,buffersize);
if (bytesRead == 0) break;
OutputStream.Write(data,0,bytesRead);
}
while (true);
}
The problem is that download is failing when the SAS is expired. I had an understanding that the SAS token is needed only for authentication and if download is started with its expiry time then download will continue even if the SAS is expired.
It is correct that the SAS token is needed only for authentication. However, in your case, BlobStream will issue a new request whenever it needs more data from the server. Because each request needs to be authenticated separately and your SAS token expires before the entire download is finished, it is expected to fail.
If you want to download the entire blob, DownloadToStream is actually a better alternative, because it will only issue a single request to the server and then download the entire blob.