示例#1
0
        /// <summary>
        /// Upload a local picture to azure storage.
        ///   1. Upload a local picture as a block blob.
        ///   2. Set its content type to "image/png".
        /// </summary>
        private static async Task BlobUploadSample()
        {
            // When transfer large file to block blob, set TransferManager.Configurations.BlockSize to specify the size of the blocks.
            // It must be between 4MB and 100MB and be multiple of 4MB. Default value is 4MB.
            //
            // Currently, the max block count of a block blob is limited to 50000.
            // When transfering a big file and the BlockSize provided is smaller than the minimum value - (size/50000),
            // it'll be reset to a value which is greater than the minimum value and multiple of 4MB for this file.
            TransferManager.Configurations.BlockSize = 4 * 1024 * 1024; //4MB

            string sourceFileName      = "azure.png";
            string destinationBlobName = "azure_blockblob.png";

            // Create the destination CloudBlob instance
            CloudBlob destinationBlob = await Util.GetCloudBlobAsync(ContainerName, destinationBlobName, BlobType.BlockBlob);

            // Use UploadOptions to set ContentType of destination CloudBlob
            UploadOptions options = new UploadOptions();

            SingleTransferContext context = new SingleTransferContext();

            context.SetAttributesCallbackAsync = async(destination) =>
            {
                CloudBlob destBlob = destination as CloudBlob;
                destBlob.Properties.ContentType = "image/png";
            };

            // Start the upload
            await TransferManager.UploadAsync(sourceFileName, destinationBlob, options, context);

            Console.WriteLine("File {0} is uploaded to {1} successfully.", sourceFileName, destinationBlob.Uri.ToString());
        }
示例#2
0
        /// <summary>
        /// Download data from Azure storage.
        ///   1. Download a CloudBlob to a Stream instance
        ///   2. Download the same CloudBlob with step #1 to a different Stream instance
        ///   3. Download another CloudBlob to a different stream with content MD5 validation disabled
        ///   4. Show the overall progress of all transfers
        /// </summary>
        private static async Task BlobDownloadToStreamSample()
        {
            string sourceBlobName1 = "azure_blockblob.png";
            string sourceBlobName2 = "azure_blockblob2.png";

            // Create the source CloudBlob instances
            CloudBlob sourceBlob1 = await Util.GetCloudBlobAsync(ContainerName, sourceBlobName1, BlobType.BlockBlob);

            CloudBlob sourceBlob2 = await Util.GetCloudBlobAsync(ContainerName, sourceBlobName2, BlobType.BlockBlob);

            // Create a TransferContext shared by both transfers
            SingleTransferContext sharedTransferContext = new SingleTransferContext();

            // Record the overall progress
            ProgressRecorder recorder = new ProgressRecorder();

            sharedTransferContext.ProgressHandler = recorder;

            MemoryStream memoryStream1_1 = new MemoryStream();
            MemoryStream memoryStream1_2 = new MemoryStream();
            MemoryStream memoryStream2   = new MemoryStream();

            try
            {
                // Start the blob download
                Task task1 = TransferManager.DownloadAsync(sourceBlob1, memoryStream1_1, null /* options */, sharedTransferContext);

                // Start to download the same blob to another Stream
                // Please note, DataMovement Library will download blob once for each of downloads.
                // For example, if you start two downloads from the same source blob to two different Stream instance,
                // DataMovement Library will download the blob content twice.
                Task task2 = TransferManager.DownloadAsync(sourceBlob1, memoryStream1_2, null /* options */, sharedTransferContext);

                // Create a DownloadOptions to disable md5 check after data is downloaded. Otherwise, data movement
                // library will check the md5 checksum stored in the ContentMD5 property of the source CloudFile/CloudBlob
                // You can uncomment following codes, enable ContentMD5Validation and have a try.
                //   sourceBlob2.Properties.ContentMD5 = "WrongMD5";
                //   sourceBlob2.SetProperties();
                DownloadOptions options = new DownloadOptions();
                options.DisableContentMD5Validation = true;

                // Start the download
                Task task3 = TransferManager.DownloadAsync(sourceBlob2, memoryStream2, options, sharedTransferContext);

                // Wait for all transfers to finish
                await task1;
                await task2;
                await task3;

                // Print out the final transfer state
                Console.WriteLine("Final transfer state: {0}", recorder.ToString());
            }
            finally
            {
                memoryStream1_1.Dispose();
                memoryStream1_2.Dispose();
                memoryStream2.Dispose();
            }
        }
示例#3
0
        /// <summary>
        /// Copy data between Azure storage.
        ///   1. Copy a CloudBlob
        ///   2. Cancel the transfer before it finishes with a CancellationToken
        ///   3. Store the transfer checkpoint after transfer being cancelled
        ///   4. Resume the transfer with the stored checkpoint
        /// </summary>
        private static async Task BlobCopySample()
        {
            string sourceBlobName      = "azure_blockblob.png";
            string destinationBlobName = "azure_blockblob2.png";

            // Create the source CloudBlob instance
            CloudBlob sourceBlob = await Util.GetCloudBlobAsync(ContainerName, sourceBlobName, BlobType.BlockBlob);

            // Create the destination CloudBlob instance
            CloudBlob destinationBlob = await Util.GetCloudBlobAsync(ContainerName, destinationBlobName, BlobType.BlockBlob);

            // Create CancellationTokenSource used to cancel the transfer
            CancellationTokenSource cancellationSource = new CancellationTokenSource();

            TransferCheckpoint    checkpoint = null;
            SingleTransferContext context    = new SingleTransferContext();

            // Start the transfer
            try
            {
                // With the CopyMethod parameter, you can indicate how the content would be copied to destination blob.
                // SyncCopy is to download source blob content to local memory and then upload to destination blob.
                // ServiceSideAsyncCopy is to send a start-copy request to Azure Storage Sever, and Azure Storage Server will do the actual copy.
                // ServiceSideSyncCopy will leverage REST API of Put Block From URL, Append Block From URL and Put Page From URL in Azure Storage Server.
                // Please see <c>https://docs.microsoft.com/en-us/rest/api/storageservices/put-block-from-url</c> for Put Block From URL,
                // <c>https://docs.microsoft.com/en-us/rest/api/storageservices/append-block-from-url</c> for Append Block From URL,
                // <c>https://docs.microsoft.com/en-us/rest/api/storageservices/put-page-from-url</c> for Put Page From URL.

                // Following will use ServiceSideSyncCopy to copy a blob.
                Task task = TransferManager.CopyAsync(sourceBlob, destinationBlob, CopyMethod.ServiceSideSyncCopy, null /* options */, context, cancellationSource.Token);

                // Sleep for 1 seconds and cancel the transfer.
                // It may fail to cancel the transfer if transfer is done in 1 second. If so, no file will tranferred after resume.
                Thread.Sleep(1000);
                Console.WriteLine("Cancel the transfer.");
                cancellationSource.Cancel();

                await task;
            }
            catch (Exception e)
            {
                Console.WriteLine("The transfer is cancelled: {0}", e.Message);
            }

            // Store the transfer checkpoint
            checkpoint = context.LastCheckpoint;

            // Create a new TransferContext with the store checkpoint
            SingleTransferContext resumeContext = new SingleTransferContext(checkpoint);

            // Resume transfer from the stored checkpoint
            Console.WriteLine("Resume the cancelled transfer.");
            await TransferManager.CopyAsync(sourceBlob, destinationBlob, CopyMethod.ServiceSideSyncCopy, null /* options */, resumeContext);

            Console.WriteLine("CloudBlob {0} is copied to {1} successfully.", sourceBlob.Uri.ToString(), destinationBlob.Uri.ToString());
        }
示例#4
0
        /// <summary>
        /// Copy data between Azure storage.
        ///   1. Copy a CloudBlob
        ///   2. Cancel the transfer before it finishes with a CancellationToken
        ///   3. Store the transfer checkpoint after transfer being cancelled
        ///   4. Resume the transfer with the stored checkpoint
        /// </summary>
        private static async Task BlobCopySample()
        {
            string sourceBlobName      = "application-user-photos/azure_blockblob.png";
            string destinationBlobName = "application-user-photos/azure_blockblob-2.png";

            // Create the source CloudBlob instance
            CloudBlob sourceBlob = await Util.GetCloudBlobAsync(ContainerName, sourceBlobName, BlobType.BlockBlob);

            // Create the destination CloudBlob instance
            CloudBlob destinationBlob = await Util.GetCloudBlobAsync(ContainerName, destinationBlobName, BlobType.BlockBlob);

            // Create CancellationTokenSource used to cancel the transfer
            CancellationTokenSource cancellationSource = new CancellationTokenSource();

            TransferCheckpoint    checkpoint = null;
            SingleTransferContext context    = new SingleTransferContext();

            // Start the transfer
            try
            {
                Task task = TransferManager.CopyAsync(sourceBlob, destinationBlob, false /* isServiceCopy */, null /* options */, context, cancellationSource.Token);

                // Sleep for 1 seconds and cancel the transfer.
                // It may fail to cancel the transfer if transfer is done in 1 second. If so, no file will tranferred after resume.
                Thread.Sleep(1000);
                Console.WriteLine("Cancel the transfer.");
                cancellationSource.Cancel();

                await task;
            }
            catch (Exception e)
            {
                Console.WriteLine("The transfer is cancelled: {0}", e.Message);
            }

            // Store the transfer checkpoint
            checkpoint = context.LastCheckpoint;

            // Create a new TransferContext with the store checkpoint
            SingleTransferContext resumeContext = new SingleTransferContext(checkpoint);

            // Resume transfer from the stored checkpoint
            Console.WriteLine("Resume the cancelled transfer.");
            await TransferManager.CopyAsync(sourceBlob, destinationBlob, false /* isServiceCopy */, null /* options */, resumeContext);

            Console.WriteLine("CloudBlob {0} is copied to {1} successfully.", sourceBlob.Uri.ToString(), destinationBlob.Uri.ToString());
        }
        /// <summary>
        /// Upload a local picture to azure storage.
        ///   1. Upload a local picture as a block blob.
        ///   2. Set its content type to "image/png".
        /// </summary>
        private static async Task BlobUploadSample()
        {
            string sourceFileName      = "azure.png";
            string destinationBlobName = "azure_blockblob.png";

            // Create the destination CloudBlob instance
            CloudBlob destinationBlob = await Util.GetCloudBlobAsync(ContainerName, destinationBlobName, BlobType.BlockBlob);

            // Use UploadOptions to set ContentType of destination CloudBlob
            UploadOptions options = new UploadOptions();

            SingleTransferContext context = new SingleTransferContext();

            context.SetAttributesCallback = (destination) =>
            {
                CloudBlob destBlob = destination as CloudBlob;
                destBlob.Properties.ContentType = "image/png";
            };

            // Start the upload
            await TransferManager.UploadAsync(sourceFileName, destinationBlob, options, context);

            Console.WriteLine("File {0} is uploaded to {1} successfully.", sourceFileName, destinationBlob.Uri.ToString());
        }
示例#6
0
        /// <summary>
        /// Download data from Azure storage.
        ///   1. Download a CloudBlob to an exsiting local file
        ///   2. Query the user to overwrite the local file or not in the OverwriteCallback
        ///   3. Download another CloudBlob to local with content MD5 validation disabled
        ///   4. Show the overall progress of both transfers
        /// </summary>
        private static async Task BlobDownloadSample()
        {
            string sourceBlobName1      = "azure_blockblob.png";
            string sourceBlobName2      = "azure_blockblob2.png";
            string destinationFileName1 = "azure.png";
            string destinationFileName2 = "azure_new.png";

            // Create the source CloudBlob instances
            CloudBlob sourceBlob1 = await Util.GetCloudBlobAsync(ContainerName, sourceBlobName1, BlobType.BlockBlob);

            CloudBlob sourceBlob2 = await Util.GetCloudBlobAsync(ContainerName, sourceBlobName2, BlobType.BlockBlob);

            // Create a TransferContext shared by both transfers
            SingleTransferContext sharedTransferContext = new SingleTransferContext();

            // Show overwrite prompt in console when OverwriteCallback is triggered
            sharedTransferContext.ShouldOverwriteCallbackAsync = async(source, destination) =>
            {
                Console.WriteLine("{0} already exists. Do you want to overwrite it with {1}? (Y/N)", destination, source);

                while (true)
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey(true);
                    char           key     = keyInfo.KeyChar;

                    if (key == 'y' || key == 'Y')
                    {
                        Console.WriteLine("User choose to overwrite the destination.");
                        return(true);
                    }
                    else if (key == 'n' || key == 'N')
                    {
                        Console.WriteLine("User choose NOT to overwrite the destination.");
                        return(false);
                    }

                    Console.WriteLine("Please press 'y' or 'n'.");
                }
            };

            // Record the overall progress
            ProgressRecorder recorder = new ProgressRecorder();

            sharedTransferContext.ProgressHandler = recorder;

            // Start the blob download
            Task task1 = TransferManager.DownloadAsync(sourceBlob1, destinationFileName1, null /* options */, sharedTransferContext);

            // Create a DownloadOptions to disable md5 check after data is downloaded. Otherwise, data movement
            // library will check the md5 checksum stored in the ContentMD5 property of the source CloudFile/CloudBlob
            // You can uncomment following codes, enable ContentMD5Validation and have a try.
            //   sourceBlob2.Properties.ContentMD5 = "WrongMD5";
            //   sourceBlob2.SetProperties();
            DownloadOptions options = new DownloadOptions();

            options.DisableContentMD5Validation = true;

            // Start the download
            Task task2 = TransferManager.DownloadAsync(sourceBlob2, destinationFileName2, options, sharedTransferContext);

            // Wait for both transfers to finish
            try
            {
                await task1;
            }
            catch (Exception e)
            {
                // Data movement library will throw a TransferException when user choose to not overwrite the existing destination
                Console.WriteLine(e.Message);
            }

            await task2;

            // Print out the final transfer state
            Console.WriteLine("Final transfer state: {0}", recorder.ToString());
        }