public AzurePageBlobStream(string connString, string databaseName)
        {
            CloudPageBlob blob = GetBlobReference(connString, databaseName);

            if (!blob.ExistsAsync().Result)
            {
                if (WriteDebugLogs)
                {
                    Console.WriteLine("Creating new page blob file " + databaseName);
                }
                blob.CreateAsync(DefaultStreamSize).Wait();
                blob.SetPremiumBlobTierAsync(DefaultBlobTier).Wait();
            }
            Blob = blob;

            for (var i = 0; i < NumberOfLocks; i++)
            {
                Locks[i] = new object();
            }
            for (var i = 0; i < Math.Max(Environment.ProcessorCount * 2, 10); i++)
            {
                Buffers.Add(new byte[PageSize * Pages]);
            }
            if (Length != 0)
            {
                ReadAhead(0);
            }
        }
Beispiel #2
0
        protected async Task <EnsureBlobExistsResult> EnsureBlobExistsAsync(CloudPageBlob pageBlob, int byteCountToWrite, CancellationToken?cancellationToken = null)
        {
            if (this.VerifiedThatBlobExists)
            {
                return(EnsureBlobExistsResult.AlreadyExisted);
            }

            if (cancellationToken == null)
            {
                cancellationToken = CancellationToken.None;
            }

            await this.EnsureContainerExistsAsync();

            bool blobExists = await pageBlob.ExistsAsync(this.RequestOptions, this.OperationContext, cancellationToken.Value);

            EnsureBlobExistsResult result = blobExists
                ? EnsureBlobExistsResult.AlreadyExisted
                : EnsureBlobExistsResult.Created;

            if (!blobExists)
            {
                long initialByteLength = CalculateBlobLengthThroughEndOfPage(byteCountToWrite);
                await pageBlob.CreateAsync(initialByteLength, CreateAccessCondition(pageBlob), this.RequestOptions, this.OperationContext);
            }

            this.VerifiedThatBlobExists = true;
            return(result);
        }
Beispiel #3
0
        private async Task InitStream(string accountName, string accountKey, string containerName, bool isPublic, bool deleteIfExists)
        {
            _container = Utils.InitContainer(accountName, accountKey, containerName, isPublic);

            // Get a reference to the page blob that will be created.
            _pageBlob = _container.GetPageBlobReference(_streamName);

            BlobRequestOptions options = new BlobRequestOptions();

            options.MaximumExecutionTime = MaxExecutionTime;

            bool exists = await Utils.WrapInRetry(_logger, async() =>
                                                  { return(await _pageBlob.ExistsAsync(options, null)); });

            if (exists)
            {
                if (deleteIfExists)
                {
                    await Utils.WrapInRetry(_logger, async() => { await _pageBlob.DeleteAsync(DeleteSnapshotsOption.None, null, options, null); });
                }
                else
                {
                    throw new ApplicationException("Trying to open existing page blob " + _streamName);
                }
            }

            await Utils.WrapInRetry(_logger, async() => { await _pageBlob.CreateAsync(_streamSize, null, options, null); });

            await Utils.WrapInRetry(_logger, async() =>
            {
                _pageBlob.Metadata["writePosition"] = 0.ToString();
                await _pageBlob.SetMetadataAsync(null, options, null);
                await _pageBlob.SetPropertiesAsync(null, options, null);
            });
        }
Beispiel #4
0
        public override void SetParameters(object parameters)
        {
            var jParameters = parameters as JObject;

            if (jParameters == null)
            {
                throw new ArgumentException("Expecting JObject parameters");
            }

            var settings = jParameters.ToObject <PageBlobSettings>();
            var client   = new CloudBlobClient(
                new Uri(string.Format("https://{0}.blob.core.windows.net/", settings.AccountName)),
                new StorageCredentials(settings.AccountName, settings.AccountKey));

            var container = client.GetContainerReference(settings.ContainerName.ToLower());

            container.CreateIfNotExistsAsync().Wait();
            cloudBlob = container.GetPageBlobReference(settings.BlobName);
            if (!cloudBlob.ExistsAsync().Result)
            {
                cloudBlob.CreateAsync(settings.BlobSizeGB * 1024L * 1024L * 1024L).Wait();
            }

            cloudBlob.FetchAttributesAsync().Wait();
        }
        public static List <string> CreateBlobsTask(CloudBlobContainer container, int count, BlobType type)
        {
            string        name;
            List <string> blobs = new List <string>();

            for (int i = 0; i < count; i++)
            {
                switch (type)
                {
                case BlobType.BlockBlob:
                    name = "bb" + Guid.NewGuid().ToString();
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
                    blockBlob.PutBlockListAsync(new string[] { }).Wait();
                    blobs.Add(name);
                    break;

                case BlobType.PageBlob:
                    name = "pb" + Guid.NewGuid().ToString();
                    CloudPageBlob pageBlob = container.GetPageBlobReference(name);
                    pageBlob.CreateAsync(0).Wait();
                    blobs.Add(name);
                    break;
                }
            }
            return(blobs);
        }
        /// <summary>
        /// Basic operations to work with page blobs
        /// </summary>
        /// <returns>Task</returns>
        private static async Task BasicStoragePageBlobOperationsAsync()
        {
            const string PageBlobName          = "samplepageblob";
            string       pageBlobContainerName = "demopageblobcontainer-" + Guid.NewGuid();

            // Retrieve storage account information from connection string
            // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a blob client for interacting with the blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Create a container for organizing blobs within the storage account.
            Console.WriteLine("1. Creating Container");
            CloudBlobContainer container = blobClient.GetContainerReference(pageBlobContainerName);
            await container.CreateIfNotExistsAsync();

            // Create a page blob in the newly created container.
            Console.WriteLine("2. Creating Page Blob");
            CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName);
            await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes

            // Write to a page blob
            Console.WriteLine("2. Write to a Page Blob");
            byte[] samplePagedata = new byte[512];
            Random random         = new Random();

            random.NextBytes(samplePagedata);
            await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length);

            // List all blobs in this container. Because a container can contain a large number of blobs the results
            // are returned in segments (pages) with a maximum of 5000 blobs per segment. You can define a smaller size
            // using the maxResults parameter on ListBlobsSegmentedAsync.
            Console.WriteLine("3. List Blobs in Container");
            BlobContinuationToken token = null;

            do
            {
                BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token);

                token = resultSegment.ContinuationToken;
                foreach (IListBlobItem blob in resultSegment.Results)
                {
                    // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
                    Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType());
                }
            } while (token != null);

            // Read from a page blob
            //Console.WriteLine("4. Read from a Page Blob");
            int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Count());

            // Clean up after the demo
            Console.WriteLine("6. Delete page Blob");
            await pageBlob.DeleteIfExistsAsync();

            Console.WriteLine("7. Delete Container");
            await container.DeleteIfExistsAsync();
        }
Beispiel #7
0
        /// <summary>
        /// Asynchronously invoke create on the given pageBlob.
        /// </summary>
        /// <param name="size">maximum size of the blob</param>
        /// <param name="pageBlob">The page blob to create</param>
        public async Task CreateAsync(long size, CloudPageBlob pageBlob)
        {
            if (this.waitingCount != 0)
            {
                this.azureStorageDevice.BlobManager?.HandleStorageError(nameof(CreateAsync), "expect to be called on blobs that don't already exist and exactly once", pageBlob?.Name, null, false, false);
            }

            await this.azureStorageDevice.BlobManager.PerformWithRetriesAsync(
                BlobManager.AsynchronousStorageReadMaxConcurrency,
                true,
                "CloudPageBlob.CreateAsync",
                "CreateDevice",
                "",
                pageBlob.Name,
                3000,
                true,
                async (numAttempts) =>
            {
                await pageBlob.CreateAsync(
                    size,
                    accessCondition: null,
                    options: BlobManager.BlobRequestOptionsDefault,
                    operationContext: null,
                    this.azureStorageDevice.PartitionErrorHandler.Token);

                return(1);
            });

            // At this point the blob is fully created. After this line all consequent writers will write immediately. We just
            // need to clear the queue of pending writers.
            this.PageBlob = pageBlob;

            // Take a snapshot of the current waiting count. Exactly this many actions will be cleared.
            // Swapping in -1 will inform any stragglers that we are not taking their actions and prompt them to retry (and call write directly)
            int waitingCountSnapshot = Interlocked.Exchange(ref this.waitingCount, -1);
            Action <CloudPageBlob> action;

            // Clear actions
            for (int i = 0; i < waitingCountSnapshot; i++)
            {
                // inserts into the queue may lag behind the creation thread. We have to wait until that happens.
                // This is so rare, that we are probably okay with a busy wait.
                while (!this.pendingWrites.TryDequeue(out action))
                {
                }
                action(pageBlob);
            }

            // Mark for deallocation for the GC
            this.pendingWrites = null;
        }
Beispiel #8
0
        /// <summary>
        /// Basic operations to work with page blobs
        /// </summary>
        /// <returns>Task</returns>
        private static async Task BasicStoragePageBlobOperations()
        {
            const string PageBlobName = "samplepageblob";

            // Retrieve storage account information from connection string
            // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
            CloudStorageAccount storageAccount = storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a blob client for interacting with the blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Create a container for organizing blobs within the storage account.
            Console.WriteLine("1. Creating Container");
            CloudBlobContainer container = blobClient.GetContainerReference("democontainerpageblob");
            await container.CreateIfNotExistsAsync();

            // Create a page blob to the newly creating container. Page blob needs to be
            Console.WriteLine("2. Creating Page Blob");
            CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName);
            await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes

            // Write to a page blob
            Console.WriteLine("2. Write to a Page Blob");
            byte[] samplePagedata = new byte[512];
            Random random         = new Random();

            random.NextBytes(samplePagedata);
            await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length);

            // List all blobs in this container
            Console.WriteLine("3. List Blobs in Container");
            foreach (IListBlobItem blob in container.ListBlobs())
            {
                // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
                // Use blob.GetType() and cast to appropriate type to gain access to properties specific to each type
                Console.WriteLine("{0} (type: {1})\n", blob.Uri, blob.GetType());
            }

            // Read from a page blob
            Console.WriteLine("4. Read from a Page Blob");
            int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Count());

            // Clean up after the demo
            Console.WriteLine("5. Delete page Blob and container");
            await pageBlob.DeleteAsync();

            await container.DeleteAsync();
        }
Beispiel #9
0
        private async Task CreateBlobAsync(CancellationToken token)
        {
            try
            {
                await leaseBlob.Container.CreateIfNotExistsAsync();

                if (!await leaseBlob.ExistsAsync())
                {
                    await leaseBlob.CreateAsync(0);
                }
            }
            catch (StorageException storageException)
            {
                logger.LogError($"Error creating a mutex blob. Details: {storageException}");
                throw;
            }
        }
Beispiel #10
0
        private static async Task ManageBlob(CloudBlobContainer container, string pageBlobName)
        {
            CloudPageBlob pageBlob = container.GetPageBlobReference(pageBlobName);
            await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes

            byte[] samplePagedata = new byte[512];
            Random random         = new Random();

            random.NextBytes(samplePagedata);
            await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length);

            int bytesRead = await
                            pageBlob.DownloadRangeToByteArrayAsync(samplePagedata,
                                                                   0, 0, samplePagedata.Length);

            System.Console.WriteLine("Read:" + bytesRead + " bytes");
        }
Beispiel #11
0
        private static async Task SaveObjectInBlob <T>(CloudBlobContainer container, string pageBlobName, T obj)
        {
            CloudPageBlob pageBlob = container.GetPageBlobReference(pageBlobName);
            await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes

            byte[] samplePagedata   = new byte[2048];
            var    serializedObject = ObjectToByteArray <T>(obj);

            serializedObject.CopyTo(samplePagedata, 0);
            await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length);

            int bytesRead = await
                            pageBlob.DownloadRangeToByteArrayAsync(samplePagedata,
                                                                   0, 0, samplePagedata.Length);

            System.Console.WriteLine("Read:" + bytesRead + " bytes");
        }
Beispiel #12
0
        public async Task <IActionResult> Index()
        {
            TelemetryClient telemetry = new TelemetryClient();

            telemetry.TrackEvent("StartofAnewFlow");

            var sample = new MetricTelemetry();

            sample.Name = "usercomputertime";
            sample.Sum  = 45;
            telemetry.TrackMetric(sample);

            CloudStorageAccount storageAccount;

            // Get information about a storage account from a connection string.
            storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=myjumlstorage;AccountKey=JlEoLSkdSvL0z4S73JPjp+AdMipXwt3sMYKB7hW6Kl4Kp3IYVa6Kaf9C9uFI6kdZh6vJx7D3gj5N73cWEk5rCg==;EndpointSuffix=core.windows.net");

            // Create a local client for working with the storage account.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Get a reference to a container in the storage account.
            CloudBlobContainer container = blobClient.GetContainerReference("jums2");
            await container.CreateIfNotExistsAsync();

            // Create a page blob in the newly created container.
            Guid          g        = Guid.NewGuid();
            string        name     = "myblob-" + g.ToString() + ".jpg";
            CloudPageBlob pageBlob = container.GetPageBlobReference(name);


            await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes

            // Write data to the new blob.
            byte[] samplePagedata = new byte[512];
            Random random         = new Random();

            random.NextBytes(samplePagedata);
            await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length);



            return(View());
        }
Beispiel #13
0
        /// <summary>
        /// Asynchronously invoke create on the given pageBlob.
        /// </summary>
        /// <param name="size">maximum size of the blob</param>
        /// <param name="pageBlob">The page blob to create</param>
        public async Task CreateAsync(long size, CloudPageBlob pageBlob)
        {
            try
            {
                if (this.waitingCount != 0)
                {
                    this.blobManager.HandleBlobError(nameof(CreateAsync), "expect to be called on blobs that don't already exist and exactly once", pageBlob?.Name, null, false);
                }

                await pageBlob.CreateAsync(size,
                                           accessCondition : null, options : this.blobManager.GetBlobRequestOptionsWithRetry(), operationContext : null, this.blobManager.CancellationToken).ConfigureAwait(false);

                // At this point the blob is fully created. After this line all consequent writers will write immediately. We just
                // need to clear the queue of pending writers.
                this.PageBlob = pageBlob;

                // Take a snapshot of the current waiting count. Exactly this many actions will be cleared.
                // Swapping in -1 will inform any stragglers that we are not taking their actions and prompt them to retry (and call write directly)
                int waitingCountSnapshot = Interlocked.Exchange(ref waitingCount, -1);
                Action <CloudPageBlob> action;
                // Clear actions
                for (int i = 0; i < waitingCountSnapshot; i++)
                {
                    // inserts into the queue may lag behind the creation thread. We have to wait until that happens.
                    // This is so rare, that we are probably okay with a busy wait.
                    while (!pendingWrites.TryDequeue(out action))
                    {
                    }
                    action(pageBlob);
                }

                // Mark for deallocation for the GC
                pendingWrites = null;
            }
            catch (Exception e)
            {
                this.blobManager.HandleBlobError(nameof(CreateAsync), "could not create page blob", pageBlob?.Name, e, true);
                throw;
            }
        }
        async Task CreateBlobAsync(CancellationToken token)
        {
            await _leaseBlob.Container.CreateIfNotExistsAsync(token);

            if (!await _leaseBlob.ExistsAsync(token))
            {
                try {
                    await _leaseBlob.CreateAsync(_size, token);
                }
                catch (StorageException e) {
                    if (e.InnerException is WebException)
                    {
                        var webException = e.InnerException as WebException;
                        var response     = webException.Response as HttpWebResponse;

                        if (response == null || response.StatusCode != HttpStatusCode.PreconditionFailed)
                        {
                            throw;
                        }
                    }
                }
            }
        }
        private async Task CreateBlobAsync(CancellationToken token)
        {
            logger.InfoFormat("Creating container {0} if it does not exist.", leaseBlob.Container.Name);
            await leaseBlob.Container.CreateIfNotExistsAsync(token);

            if (!await leaseBlob.ExistsAsync(token))
            {
                try
                {
                    logger.InfoFormat("Creating blob {0}.", leaseBlob.Name);
                    await leaseBlob.CreateAsync(0, token);
                }
                catch (StorageException e)
                {
                    if (e.InnerException is WebException webException)
                    {
                        if (!(webException.Response is HttpWebResponse response) || response.StatusCode != HttpStatusCode.PreconditionFailed)
                        {
                            throw;
                        }
                    }
                }
            }
        }
Beispiel #16
0
 /// <summary>
 /// Create a page blob.
 /// </summary>
 /// <param name="blob">The page blob.</param>
 /// <param name="size">The maximum size of the blob, in bytes.</param>
 /// <returns>The async task.</returns>
 public async Task CreateAsync(CloudPageBlob blob, long size)
 {
     await blob.CreateAsync(size);
 }
    private async Task BasicStoragePageBlobOperationsAsync()
    {
        WriteLine("-- Testing PageBlob --");

        const string PageBlobName = "samplepageblob";

        // Create a blob client for interacting with the blob service.
        CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

        // Create a container for organizing blobs within the storage account.
        WriteLine("1. Creating Container");
        CloudBlobContainer container = blobClient.GetContainerReference("democontainerpageblob");
        await container.CreateIfNotExistsAsync();

        // Create a page blob in the newly created container.
        WriteLine("2. Creating Page Blob");
        CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName);
        await pageBlob.CreateAsync(512 * 2 /*size*/);         // size needs to be multiple of 512 bytes

        // Write to a page blob
        WriteLine("2. Write to a Page Blob");
        byte[] samplePagedata = new byte[512];
        Random random         = new Random();

        random.NextBytes(samplePagedata);
        await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length);

        // List all blobs in this container. Because a container can contain a large number of blobs the results
        // are returned in segments (pages) with a maximum of 5000 blobs per segment. You can define a smaller size
        // using the maxResults parameter on ListBlobsSegmentedAsync.
        WriteLine("3. List Blobs in Container");
        BlobContinuationToken token = null;

        do
        {
            BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token);

            token = resultSegment.ContinuationToken;
            foreach (IListBlobItem blob in resultSegment.Results)
            {
                // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
                WriteLine(string.Format("{0} (type: {1}", blob.Uri, blob.GetType()));
            }
        } while (token != null);

        // Read from a page blob
        WriteLine("4. Read from a Page Blob");
        int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Length);

        // Clean up after the demo
        WriteLine("5. Delete page Blob");
        await pageBlob.DeleteAsync();

        // When you delete a container it could take several seconds before you can recreate a container with the same
        // name - hence to enable you to run the demo in quick succession the container is not deleted. If you want
        // to delete the container uncomment the line of code below.
        WriteLine("6. Delete Container");
        await container.DeleteAsync();

        WriteLine("-- Test Complete --");
    }
Beispiel #18
0
        static async Task BlobAsync()
        {
            try
            {
                CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString);
                CloudBlobClient     bc = sa.CreateCloudBlobClient();

                const string       CON_NAME = "smart-blob-container";
                CloudBlobContainer con      = bc.GetContainerReference(CON_NAME);
                bool rr = await con.CreateIfNotExistsAsync();

                const string  blobName = "my.txt";
                CloudPageBlob blob     = con.GetPageBlobReference(blobName);

                bool del = await blob.DeleteIfExistsAsync();

                await blob.CreateAsync(64 *1024, AccessCondition.GenerateIfNotExistsCondition(), new BlobRequestOptions(), new OperationContext());

                using (CloudBlobStream blobStream = await blob.OpenWriteAsync(null, AccessCondition.GenerateEmptyCondition(), new BlobRequestOptions(), new OperationContext()))
                {
                    byte[] sector = new byte[512];
                    for (int ii = 0; ii < sector.Length; ++ii)
                    {
                        sector[ii] = (byte)(ii % 26 + 'A');
                    }
                    MemoryStream mm = new MemoryStream(sector);
                    //byte[] buffer = Encoding.UTF8.GetBytes("zzz宏发科技恢复健康哈尔月UI风雅颂的法尔加zzz----");
                    //await blobStream.WriteAsync(buffer, 0, buffer.Length);
                    //await blobStream.WriteAsync(sector, 0, sector.Length - buffer.Length);
                    await blobStream.WriteAsync(sector, 0, sector.Length);

                    if (blobStream.CanSeek)
                    {
                        blobStream.Seek(1024, System.IO.SeekOrigin.Begin);
                    }

                    //buffer = Encoding.UTF8.GetBytes("this is some test");
                    //await blobStream.WriteAsync(buffer, 0, buffer.Length);
                    //await blobStream.WriteAsync(sector, 0, sector.Length - buffer.Length);
                    await blobStream.WriteAsync(sector, 0, sector.Length);

                    await blobStream.FlushAsync();
                }


                using (Stream blobStream = await blob.OpenReadAsync())
                {
                    byte[] buffer = new byte[2048];

                    const int firstReadCount = 8;
                    int       rc             = await blobStream.ReadAsync(buffer, 0, firstReadCount);

                    if (blobStream.CanSeek)
                    {
                        blobStream.Seek(rc, SeekOrigin.Begin);
                    }
                    rc = await blobStream.ReadAsync(buffer, 8, buffer.Length - firstReadCount);

                    string text = Encoding.UTF8.GetString(buffer);
                    Console.WriteLine(text);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex}");
            }
        }
Beispiel #19
0
 /// <summary>
 /// Create a page blob.
 /// </summary>
 /// <param name="blob">The page blob.</param>
 /// <param name="size">The maximum size of the blob, in bytes.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The async task.</returns>
 public async Task CreateAsync(CloudPageBlob blob, long size, CancellationToken cancellationToken)
 {
     await blob.CreateAsync(size, cancellationToken);
 }
Beispiel #20
0
        /// <summary>
        /// Basic operations to work with page blobs
        /// </summary>
        /// <returns>Task</returns>
        private static async Task BasicStoragePageBlobOperationsAsync()
        {
            Stopwatch timer = new Stopwatch();

            const string PageBlobName = "samplepageblob";

            // Retrieve storage account information from connection string
            // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a blob client for interacting with the blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Create a container for organizing blobs within the storage account.
            Console.WriteLine("1. Creating Container");
            CloudBlobContainer container = blobClient.GetContainerReference("democontainerpageblob");
            await container.CreateIfNotExistsAsync();

            Console.WriteLine("Starting Timer..");
            timer.Start();

            // Create a page blob in the newly created container.
            Console.WriteLine("2. Creating Page Blob");
            CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName);
            await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes

            // Write to a page blob
            Console.WriteLine("2. Write to a Page Blob");
            byte[] samplePagedata = new byte[512];
            Random random         = new Random();

            random.NextBytes(samplePagedata);
            await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length);

            Console.WriteLine("Stopping Timer..");
            timer.Stop();


            TimeSpan ts = timer.Elapsed;

            Console.WriteLine("Time to upload the page blob(milliseconds): " + ts.Milliseconds.ToString());
            timer.Reset();

            // List all blobs in this container. Because a container can contain a large number of blobs the results
            // are returned in segments (pages) with a maximum of 5000 blobs per segment. You can define a smaller size
            // using the maxResults parameter on ListBlobsSegmentedAsync.
            Console.WriteLine("3. List Blobs in Container");
            BlobContinuationToken token = null;

            do
            {
                BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token);

                token = resultSegment.ContinuationToken;
                foreach (IListBlobItem blob in resultSegment.Results)
                {
                    // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
                    Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType());
                }
            } while (token != null);

            // Read from a page blob
            //Console.WriteLine("4. Read from a Page Blob");
            int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Count());

            // Clean up after the demo
            Console.WriteLine("5. Delete page Blob");
            await pageBlob.DeleteAsync();

            // When you delete a container it could take several seconds before you can recreate a container with the same
            // name - hence to enable you to run the demo in quick succession the container is not deleted. If you want
            // to delete the container uncomment the line of code below.
            //Console.WriteLine("6. Delete Container");
            //await container.DeleteAsync();
        }
 public static void Create(this CloudPageBlob blob, long size, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
 {
     blob.CreateAsync(size, accessCondition, options, operationContext).GetAwaiter().GetResult();
 }
Beispiel #22
0
        static async Task HeartBeat(ILogger logger, PersistentProcessManager server, Task <int> exited, string jobName)
        {
            try
            {
                DateTime startTime = DateTime.UtcNow;

                if (jobDirectoryUri.Scheme != "azureblob")
                {
                    logger.Log("Can't send heartbeat to non-Azure dfs " + jobDirectoryUri.AbsoluteUri);
                    return;
                }

                string account, key, container, directoryName;
                Azure.Utils.FromAzureUri(jobDirectoryUri, out account, out key, out container, out directoryName);

                // get the full name of the blob from the job directory
                string heartbeatBlobName = directoryName + "heartbeat";
                string killBlobName      = directoryName + "kill";

                using (Azure.AzureDfsClient client = new Azure.AzureDfsClient(account, key, container))
                {
                    CloudPageBlob killBlob      = client.Container.GetPageBlobReference(killBlobName);
                    CloudPageBlob heartbeatBlob = client.Container.GetPageBlobReference(heartbeatBlobName);

                    await heartbeatBlob.CreateAsync(512);

                    heartbeatBlob.Metadata["status"]    = "running";
                    heartbeatBlob.Metadata["starttime"] = startTime.ToString();
                    if (jobName != null)
                    {
                        jobName = jobName.Replace('\r', ' ').Replace('\n', ' ');
                        try
                        {
                            heartbeatBlob.Metadata["jobname"] = jobName;
                        }
                        catch (Exception e)
                        {
                            logger.Log("Got exception trying to set jobname metadata to '" + jobName + "': " + e.ToString());
                            heartbeatBlob.Metadata["jobname"] = "[got exception setting job name]";
                        }
                    }

                    while (true)
                    {
                        logger.Log("Uploading heartbeat properties");
                        heartbeatBlob.Metadata["heartbeat"] = DateTime.UtcNow.ToString();
                        await heartbeatBlob.SetMetadataAsync();

                        logger.Log("Heartbeat sleeping");

                        Task <int> t = await Task.WhenAny(exited, Task.Delay(1000).ContinueWith((d) => { return(259); }));

                        int    exitCode = t.Result;
                        string status   = null;

                        if (t == exited)
                        {
                            status = (exitCode == 0) ? "success" : "failure";
                        }
                        else if (killBlob.Exists())
                        {
                            exitCode = 1;
                            status   = "killed";
                            server.TriggerFullShutdown(1, "job was cancelled");
                        }

                        if (status != null)
                        {
                            logger.Log("Uploading final heartbeat properties " + exitCode + " " + status);
                            heartbeatBlob.Metadata["status"] = status;
                            await heartbeatBlob.SetMetadataAsync();

                            logger.Log("Heartbeat exiting");
                            return;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logger.Log("Heartbeat got exception " + e.ToString());
            }
        }
Beispiel #23
0
 private static async void CreatePageBlob(CloudPageBlob pageBlob, int amountOfBlobs = 1)
 {
     await pageBlob.CreateAsync(512 *amountOfBlobs);
 }
        /// <summary>
        /// Demonstrates how to do backup using incremental snapshots. See this article on
        /// backing up Azure virtual machine disks with incremental snapshots:
        /// https://azure.microsoft.com/en-us/documentation/articles/storage-incremental-snapshots/
        /// The article also describes how to restore a disk from incremental snapshots.
        /// </summary>
        /// <returns>Task</returns>
        public static async Task IncrementalSnapshotBackupAsync()
        {
            const int TotalBlobSize     = 512 * 6;              // The total amount of data in the blob
            const int UpdateWriteSize   = 512 * 2;              // The amount of data to update in the blob
            const int UpdateWriteOffset = 512 * 2;              // The offset at which to write updated data
            const int ClearPagesOffset  = 512 * 5;              // The offset at which to begin clearing page data
            const int ClearPagesSize    = 512;                  // The amount of data to clear in the blob

            string SimulationID  = Guid.NewGuid().ToString();   // The simulation ID
            string ContainerName = "container-" + SimulationID; // The name of the primary container
            string PageBlobName  = "binary-data";               // The name of the primary page blob

            // Retrieve storage account information from connection strings
            // How to create storage connection strings - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("BackupStorageConnectionString"));

            // Create blob clients for interacting with the blob service.
            CloudBlobClient blobClient       = storageAccount.CreateCloudBlobClient();
            CloudBlobClient backupBlobClient = backupStorageAccount.CreateCloudBlobClient();

            // Create containers for organizing blobs within the storage accounts.
            container       = blobClient.GetContainerReference(ContainerName);
            backupContainer = backupBlobClient.GetContainerReference("copy-of-" + ContainerName);
            try
            {
                // The call below will fail if the sample is configured to use the storage emulator
                // in the connection string, but the emulator is not running.
                BlobRequestOptions requestOptions = new BlobRequestOptions()
                {
                    RetryPolicy = new NoRetry()
                };
                await container.CreateIfNotExistsAsync(requestOptions, null);

                await backupContainer.CreateIfNotExistsAsync(requestOptions, null);
            }
            catch (StorageException)
            {
                Console.WriteLine("If you are running with the default connection string, please make " +
                                  "sure you have started the storage emulator. Press the Windows key and type Azure " +
                                  "Storage to select and run it from the list of applications - then restart the sample.");
                Console.ReadLine();
                throw;
            }

            // Create, write to, and snapshot a page blob in the newly created container.
            CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName);
            await pageBlob.CreateAsync(TotalBlobSize);

            byte[] samplePagedata = new byte[TotalBlobSize];
            Random random         = new Random();

            random.NextBytes(samplePagedata);
            await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length);

            CloudPageBlob pageBlobSnap = await pageBlob.CreateSnapshotAsync();

            // Create a blob in the backup account
            CloudPageBlob pageBlobBackup = backupContainer.GetPageBlobReference("copy-of-" + PageBlobName);

            // Copy to the blob in the backup account. Notice that the <isServiceCopy> flag here is
            // set to 'false'. This forces data to be first downloaded from the source, then uploaded
            // to the destination. If <isServiceCopy> were set to 'true' we could avoid the I/O of
            // downloading and then uploading, but could not guarantee  that the copy completed even
            // after the local task completed. Since the remainder of this sample depends on the
            // successful completion of this operation, we must set <isServiceCopy> to 'false.'
            await TransferManager.CopyAsync(pageBlobSnap, pageBlobBackup, false);

            // Snapshot the backup copy
            CloudPageBlob pageBlobBackupSnap = await pageBlob.CreateSnapshotAsync();

            // Change the contents of the original page blob (note: we must convert the byte array to a
            // stream in order to write to a non-zero offset in the page blob) and snapshot.
            byte[] updatePageData = new byte[UpdateWriteSize];
            random.NextBytes(updatePageData);
            Stream updatePageDataStream = new MemoryStream(updatePageData);
            await pageBlob.WritePagesAsync(updatePageDataStream, UpdateWriteOffset, null);

            await pageBlob.ClearPagesAsync(ClearPagesOffset, ClearPagesSize);

            CloudPageBlob newPageBlobSnap = await pageBlob.CreateSnapshotAsync();

            // Get the incremental changes and write to the backup.
            IEnumerable <PageDiffRange> changedPages =
                await newPageBlobSnap.GetPageRangesDiffAsync(pageBlobSnap.SnapshotTime.Value);

            foreach (PageDiffRange pageRange in changedPages)
            {
                // If this page range is cleared, remove the old data in the backup.
                if (pageRange.IsClearedPageRange)
                {
                    await pageBlobBackup.ClearPagesAsync(
                        pageRange.StartOffset, pageRange.EndOffset - pageRange.StartOffset + 1);
                }

                // If this page range is not cleared, write the new data to the backup.
                else
                {
                    byte[] toWrite = new byte[pageRange.EndOffset - pageRange.StartOffset + 1];
                    await newPageBlobSnap.DownloadRangeToByteArrayAsync(
                        toWrite, 0, pageRange.StartOffset, pageRange.EndOffset - pageRange.StartOffset + 1);

                    Stream toWriteStream = new MemoryStream(toWrite);
                    await pageBlobBackup.WritePagesAsync(toWriteStream, pageRange.StartOffset, null);
                }
            }
            // Snapshot the backup blob and delete the old snapshot from the primary account
            CloudPageBlob pageBlobBackupSnapv2 = await pageBlobBackup.CreateSnapshotAsync();

            await pageBlobSnap.DeleteAsync();
        }