Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. public static async Task TestStorageAsync(byte[] file)
  2. {
  3. List<string> MSItokenResult = await GetMSITokenWithClientAsync();
  4.  
  5. try
  6. {
  7. if (MSItokenResult.Count() > 0)
  8. {
  9. CloudStorageAccount.UseV1MD5 = false;
  10. //get token
  11. string accessToken = MSItokenResult.FirstOrDefault();
  12.  
  13. //create token credential
  14. TokenCredential tokenCredential = new TokenCredential(accessToken);
  15.  
  16. //create storage credentials
  17. StorageCredentials storageCredentials = new StorageCredentials(tokenCredential);
  18.  
  19. string fileName = Guid.NewGuid().ToString() + ".jpeg";
  20.  
  21. // Create a block blob using the credentials.
  22. CloudBlockBlob blob = new CloudBlockBlob(new Uri(azureStorageBlobHostUrl + "/" + blobContainerName + "/" + fileName), storageCredentials);
  23.  
  24. await blob.UploadFromByteArrayAsync(file, 0, file.Count());
  25.  
  26. Console.WriteLine("Blob url: " + blob.Uri.ToString());
  27. Console.WriteLine("");
  28. Console.WriteLine("File uploaded successfully using storage auth with managed identity.");
  29. Console.WriteLine("");
  30. Console.WriteLine("Use menu options (1, 2, Quit)");
  31. }
  32. else
  33. {
  34. Console.WriteLine("Skipped creating storage credentials for blob storage, an error occured while acquiring MSI token");
  35. Console.WriteLine("");
  36. Console.WriteLine("Use menu options and try again. (1, 2, Quit)");
  37. }
  38.  
  39. }
  40. catch (Exception ex)
  41. {
  42. Console.WriteLine(ex.Message, ex);
  43. Console.WriteLine("");
  44. Console.WriteLine(ex.StackTrace);
  45. Console.WriteLine("");
  46. Console.WriteLine(ex.HelpLink);
  47. Console.WriteLine("");
  48. Console.WriteLine("Use menu options and try again. (1, 2, Quit)");
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement