Wednesday, June 28, 2017

pull down a file from Azure!

Alright, it turn out that, yes, this works! You could jam a static method like so...

public static string Download(string storageDirectory, Guid fileId, string fileName, string
      connectionString)
{
   var storageAccount = CloudStorageAccount.Parse(connectionString);
   var blobStorage = storageAccount.CreateCloudBlobClient();
   CloudBlobContainer container = blobStorage.GetContainerReference("dummyfiles");
   string blobId = string.Format("{0}/{1}_{2}", storageDirectory, fileId, fileName);
   CloudBlockBlob blob = container.GetBlockBlobReference(blobId);
   var token = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
   {
      Permissions = SharedAccessBlobPermissions.Read,
      SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(1)
   }, new SharedAccessBlobHeaders()
   {
      ContentDisposition = string.Format("attachment; filename=\"{0}\"", fileName)
   });
   return string.Format("{0}{1}", blob.Uri, token);
}

 
 

...into AzureService to download a file you uploaded. Obviously, the Upload method would need some love so that fileId would be handed in to be used lieu of the Guid.NewGuid() hack.

No comments:

Post a Comment