示例#1
0
        internal static void DeleteBlob(string storageAccountName, Uri blobUri, AzureHttpClient client, TestContext context, CancellationToken token)
        {
            //this *always* calls GetStorageAccountProperties and GetStorageAccountKeys
            Uri blob, queue, table;

            context.WriteLine("Preparing to delete blob: {0}.", blobUri);

            context.WriteLine("Calling GetStorageAccountProperties and GetStorageAccountKeys for storage account {0}", storageAccountName);
            var storagePropsTask = client.GetStorageAccountPropertiesAsync(storageAccountName, token);
            var storageKeysTask = client.GetStorageAccountKeysAsync(storageAccountName, token);

            Task.WaitAll(storagePropsTask, storageKeysTask);

            context.WriteLine(storagePropsTask.Result.ToString());
            context.WriteLine(storageKeysTask.Result.ToString());

            FixupEndpoints(storagePropsTask.Result, out blob, out queue, out table);

            context.WriteLine("Instantiating Cloud Storage Account object.");

            CloudStorageAccount account = new CloudStorageAccount(
                                            new StorageCredentialsAccountAndKey(
                                                storagePropsTask.Result.Name,
                                                storageKeysTask.Result.Primary),
                                                blob, queue, table);

            context.WriteLine("Cloud Storage Account object intantiated.");

            CloudBlobClient blobClient = account.CreateCloudBlobClient();

            CloudBlob blobRef = blobClient.GetBlobReference(blobUri.ToString());

            context.WriteLine("Deleting blob {0}.", blobUri);

            blobRef.DeleteIfExists();

            context.WriteLine("Blob {0} deleted.", blobUri);
        }
示例#2
0
        //this *always* calls GetStorageAccountProperties and GetStorageAccountKeys
        internal static Uri UploadToBlob(AzureHttpClient client, TestContext context, string storageAccountName, string containerName, string fileToUpload, CancellationToken token)
        {
            //container names must be all lowercase...
            //this is the same container vs uses...
            string blobUploadContainer = "vsdeploy";
            if (!string.IsNullOrEmpty(containerName))
            {
                blobUploadContainer = containerName.ToLowerInvariant();
            }

            const int MB = 1048576;
            const int kb = 1024;
            const int MaxMBs = 600; //package can't be larger than 600 MB
            const long MaxFileSize = MB * MaxMBs;
            const int MinsPerMB = 3;

            Uri blob, queue, table;

            context.WriteLine("Preparing to upload file {0} to storage account {1}.", fileToUpload, storageAccountName);
            string fileName = Path.GetFileName(fileToUpload).ToLowerInvariant();

            context.WriteLine("Calling GetStorageAccountProperties and GetStorageAccountKeys for storage account {0}", storageAccountName);
            var storagePropsTask = client.GetStorageAccountPropertiesAsync(storageAccountName, token);
            var storageKeysTask = client.GetStorageAccountKeysAsync(storageAccountName, token);

            Task.WaitAll(storagePropsTask, storageKeysTask);

            context.WriteLine(storagePropsTask.Result.ToString());
            context.WriteLine(storageKeysTask.Result.ToString());

            FixupEndpoints(storagePropsTask.Result, out blob, out queue, out table);

            using (FileStream stream = File.Open(fileToUpload, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                long fileSize = stream.Length;
                double MBs = ((double)fileSize) / MB;

                context.WriteLine("Ready to upload file {0}, file size {1}", fileName, MBs.ToString("F6", CultureInfo.CurrentCulture));

                if (MBs < 1) MBs = 1; // no shorter than 3 minute timeout

                if (fileSize > MaxFileSize)
                {
                    throw new ArgumentException(string.Format("File {0} is too large.", fileName), "FileToUpload");
                }

                context.WriteLine("Instantiating Cloud Storage Account object.");

                CloudStorageAccount account = new CloudStorageAccount(
                                                new StorageCredentialsAccountAndKey(
                                                    storagePropsTask.Result.Name,
                                                    storageKeysTask.Result.Primary),
                                                    blob, queue, table);

                context.WriteLine("Cloud Storage Account object intantiated.");

                //set the timeout based on the size of the file: 3 mins per MB
                TimeSpan timeout = TimeSpan.FromMinutes((double)(MBs * MinsPerMB));
                context.WriteLine("Setting upload timeout to {0} minutes", timeout.Minutes);

                CloudBlobClient blobClient = account.CreateCloudBlobClient();

                blobClient.RetryPolicy = RetryPolicies.Retry(4, TimeSpan.Zero);
                blobClient.Timeout = timeout;

                CloudBlobContainer container = blobClient.GetContainerReference(blobUploadContainer);

                container.CreateIfNotExist();

                //turn off public access to the container
                BlobContainerPermissions perms = new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off };
                container.SetPermissions(perms);

                CloudBlob blobRef = container.GetBlobReference(fileName);

                DateTime startTime = DateTime.Now;
                context.WriteLine("Beginning upload of file {0} to blob {1} at time: {2}", fileName, blobRef.Uri, startTime.ToString("T", CultureInfo.CurrentCulture));

                IAsyncResult result = blobRef.BeginUploadFromStream(stream, null, null);

                context.WriteLine("Upload Started, waiting...");

                blobRef.EndUploadFromStream(result);

                DateTime endTime = DateTime.Now;
                TimeSpan uploadTime = endTime - startTime;

                double KBytePerSecond = (stream.Length / uploadTime.TotalSeconds) / kb;

                context.WriteLine("Upload of file {0} to blob {1} completed at {2}.", fileName, blobRef.Uri, endTime.ToString("T", CultureInfo.CurrentCulture));
                context.WriteLine("Upload took {0} seconds at an average rate of {1}kb per second.", uploadTime.Seconds.ToString(CultureInfo.CurrentCulture), KBytePerSecond.ToString("F4", CultureInfo.CurrentCulture));

                return blobRef.Uri;

            }
        }
示例#3
0
        internal static string CreateStorageAccount(TestContext testContext, AzureHttpClient testClient, string accountName, string location, string affinityGroup)
        {
            testContext.WriteLine("Beginning CreateStorageAccount operation.");

            string storageAccountName = accountName.ToLower().Substring(0, 24);

            testContext.WriteLine("Creating Storage account with name {0}", storageAccountName);

            var createAndWaitTask = testClient.CreateStorageAccountAsync(storageAccountName, accountName, null, location, affinityGroup, true, null)
                .ContinueWith(Utilities.PollUntilComplete(testClient, "Create Storage Account", testContext, default(CancellationToken)));

            createAndWaitTask.Wait();

            testContext.WriteLine("Storage account {0} created.", storageAccountName);

            testContext.WriteLine("End CreateStorageAccount operation.");

            testContext.WriteLine("Get StorageAccountProperties on new account.");

            var getTask = testClient.GetStorageAccountPropertiesAsync(storageAccountName);

            testContext.WriteLine(getTask.Result.ToString());

            return storageAccountName;
        }