Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static async Task TestStorageAsync(byte[] file)
- {
- List<string> MSItokenResult = await GetMSITokenWithClientAsync();
- try
- {
- if (MSItokenResult.Count() > 0)
- {
- CloudStorageAccount.UseV1MD5 = false;
- //get token
- string accessToken = MSItokenResult.FirstOrDefault();
- //create token credential
- TokenCredential tokenCredential = new TokenCredential(accessToken);
- //create storage credentials
- StorageCredentials storageCredentials = new StorageCredentials(tokenCredential);
- string fileName = Guid.NewGuid().ToString() + ".jpeg";
- // Create a block blob using the credentials.
- CloudBlockBlob blob = new CloudBlockBlob(new Uri(azureStorageBlobHostUrl + "/" + blobContainerName + "/" + fileName), storageCredentials);
- await blob.UploadFromByteArrayAsync(file, 0, file.Count());
- Console.WriteLine("Blob url: " + blob.Uri.ToString());
- Console.WriteLine("");
- Console.WriteLine("File uploaded successfully using storage auth with managed identity.");
- Console.WriteLine("");
- Console.WriteLine("Use menu options (1, 2, Quit)");
- }
- else
- {
- Console.WriteLine("Skipped creating storage credentials for blob storage, an error occured while acquiring MSI token");
- Console.WriteLine("");
- Console.WriteLine("Use menu options and try again. (1, 2, Quit)");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message, ex);
- Console.WriteLine("");
- Console.WriteLine(ex.StackTrace);
- Console.WriteLine("");
- Console.WriteLine(ex.HelpLink);
- Console.WriteLine("");
- Console.WriteLine("Use menu options and try again. (1, 2, Quit)");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement