|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using Microsoft.WindowsAzure.Storage; |
| 5 | +using Microsoft.WindowsAzure.Storage.Blob; |
| 6 | +using Microsoft.WindowsAzure.Storage.Queue; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.IO; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Azure.Functions.Java.Tests.E2E |
| 13 | +{ |
| 14 | + class StorageHelpers |
| 15 | + { |
| 16 | + public static CloudStorageAccount _storageAccount = CloudStorageAccount.Parse(Constants.StorageConnectionStringSetting); |
| 17 | + public static CloudQueueClient _queueClient = _storageAccount.CreateCloudQueueClient(); |
| 18 | + public static CloudBlobClient _cloudBlobClient = _storageAccount.CreateCloudBlobClient(); |
| 19 | + |
| 20 | + public async static Task DeleteQueue(string queueName) |
| 21 | + { |
| 22 | + CloudQueue queue = _queueClient.GetQueueReference(queueName); |
| 23 | + await queue.DeleteAsync(); |
| 24 | + } |
| 25 | + |
| 26 | + public async static Task ClearQueue(string queueName) |
| 27 | + { |
| 28 | + CloudQueue queue = _queueClient.GetQueueReference(queueName); |
| 29 | + if (await queue.ExistsAsync()) |
| 30 | + { |
| 31 | + await queue.ClearAsync(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public async static Task CreateQueue(string queueName) |
| 36 | + { |
| 37 | + CloudQueue queue = _queueClient.GetQueueReference(queueName); |
| 38 | + await queue.CreateIfNotExistsAsync(); |
| 39 | + } |
| 40 | + |
| 41 | + public async static Task<string> InsertIntoQueue(string queueName, string queueMessage) |
| 42 | + { |
| 43 | + CloudQueue queue = _queueClient.GetQueueReference(queueName); |
| 44 | + await queue.CreateIfNotExistsAsync(); |
| 45 | + CloudQueueMessage message = new CloudQueueMessage(queueMessage); |
| 46 | + await queue.AddMessageAsync(message); |
| 47 | + return message.Id; |
| 48 | + } |
| 49 | + |
| 50 | + public async static Task<string> ReadFromQueue(string queueName) |
| 51 | + { |
| 52 | + CloudQueue queue = _queueClient.GetQueueReference(queueName); |
| 53 | + CloudQueueMessage retrievedMessage = null; |
| 54 | + await Utilities.RetryAsync(async () => |
| 55 | + { |
| 56 | + retrievedMessage = await queue.GetMessageAsync(); |
| 57 | + return retrievedMessage != null; |
| 58 | + }, pollingInterval: 4000); |
| 59 | + await queue.DeleteMessageAsync(retrievedMessage); |
| 60 | + return retrievedMessage.AsString; |
| 61 | + } |
| 62 | + |
| 63 | + public async static Task<IEnumerable<string>> ReadMessagesFromQueue(string queueName) |
| 64 | + { |
| 65 | + CloudQueue queue = _queueClient.GetQueueReference(queueName); |
| 66 | + IEnumerable<CloudQueueMessage> retrievedMessages = null; |
| 67 | + List<string> messages = new List<string>(); |
| 68 | + await Utilities.RetryAsync(async () => |
| 69 | + { |
| 70 | + retrievedMessages = await queue.GetMessagesAsync(3); |
| 71 | + return retrievedMessages != null; |
| 72 | + }); |
| 73 | + foreach(CloudQueueMessage msg in retrievedMessages) |
| 74 | + { |
| 75 | + messages.Add(msg.AsString); |
| 76 | + await queue.DeleteMessageAsync(msg); |
| 77 | + } |
| 78 | + return messages; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + public async static Task ClearBlobContainers() |
| 83 | + { |
| 84 | + await ClearBlobContainer(Constants.TriggerInputBindingBlobContainer); |
| 85 | + await ClearBlobContainer(Constants.InputBindingBlobContainer); |
| 86 | + await ClearBlobContainer(Constants.OutputBindingBlobContainer); |
| 87 | + } |
| 88 | + |
| 89 | + public async static Task CreateBlobContainers() |
| 90 | + { |
| 91 | + await CreateBlobContainer(Constants.TriggerInputBindingBlobContainer); |
| 92 | + await CreateBlobContainer(Constants.InputBindingBlobContainer); |
| 93 | + await CreateBlobContainer(Constants.OutputBindingBlobContainer); |
| 94 | + } |
| 95 | + |
| 96 | + public async static Task UpdloadFileToContainer(string containerName, string expectedFileName) |
| 97 | + { |
| 98 | + string sourceFile = $"{expectedFileName}.txt"; |
| 99 | + File.WriteAllText(sourceFile, "Hello World"); |
| 100 | + CloudBlobContainer cloudBlobContainer = _cloudBlobClient.GetContainerReference(containerName); |
| 101 | + CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(sourceFile); |
| 102 | + await cloudBlockBlob.UploadFromFileAsync(sourceFile); |
| 103 | + } |
| 104 | + |
| 105 | + public async static Task<string> DownloadFileFromContainer(string containerName, string expectedFileName) |
| 106 | + { |
| 107 | + string destinationFile = $"{expectedFileName}_DOWNLOADED.txt"; |
| 108 | + string sourceFile = $"{expectedFileName}.txt"; |
| 109 | + CloudBlobContainer cloudBlobContainer = _cloudBlobClient.GetContainerReference(containerName); |
| 110 | + CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(sourceFile); |
| 111 | + await Utilities.RetryAsync(async () => |
| 112 | + { |
| 113 | + return await cloudBlockBlob.ExistsAsync(); |
| 114 | + }, pollingInterval: 4000, timeout: 120 * 1000); |
| 115 | + await cloudBlockBlob.DownloadToFileAsync(destinationFile, FileMode.Create); |
| 116 | + return File.ReadAllText(destinationFile); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + private static async Task<CloudBlobContainer> CreateBlobContainer(string containerName) |
| 121 | + { |
| 122 | + BlobContainerPermissions permissions = new BlobContainerPermissions |
| 123 | + { |
| 124 | + PublicAccess = BlobContainerPublicAccessType.Blob |
| 125 | + }; |
| 126 | + CloudBlobContainer cloudBlobContainer = _cloudBlobClient.GetContainerReference(containerName); |
| 127 | + await cloudBlobContainer.CreateIfNotExistsAsync(); |
| 128 | + await cloudBlobContainer.SetPermissionsAsync(permissions); |
| 129 | + return cloudBlobContainer; |
| 130 | + } |
| 131 | + |
| 132 | + private static async Task ClearBlobContainer(string containerName) |
| 133 | + { |
| 134 | + CloudBlobContainer cloudBlobContainer = _cloudBlobClient.GetContainerReference(containerName); |
| 135 | + BlobContinuationToken blobContinuationToken = null; |
| 136 | + do |
| 137 | + { |
| 138 | + if (!await cloudBlobContainer.ExistsAsync()) { continue; } |
| 139 | + var results = await cloudBlobContainer.ListBlobsSegmentedAsync(null, blobContinuationToken); |
| 140 | + // Get the value of the continuation token returned by the listing call. |
| 141 | + blobContinuationToken = results.ContinuationToken; |
| 142 | + foreach (IListBlobItem item in results.Results) |
| 143 | + { |
| 144 | + Console.WriteLine(item.Uri); |
| 145 | + String blobName = System.IO.Path.GetFileName(item.Uri.AbsolutePath); |
| 146 | + CloudBlob cloudBlob = cloudBlobContainer.GetBlobReference(blobName); |
| 147 | + await cloudBlob.DeleteIfExistsAsync(); |
| 148 | + } |
| 149 | + } while (blobContinuationToken != null); // Loop while the continuation token is not null. |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments