Ejemplo n.º 1
0
        private static async Task BlobDirectoryCopySample()
        {
            var sourceBlobDir = await Util.GetCloudBlobDirectoryAsync("sourcecontainer", "dir1");

            var destBlobDir = await Util.GetCloudBlobDirectoryAsync("targetcontainer", "dir2");

            var options = new CopyDirectoryOptions()
            {
                Recursive = true,
            };

            var context = new DirectoryTransferContext();

            context.FileTransferred += FileTransferredCallback;
            context.FileFailed      += FileFailedCallback;
            context.FileSkipped     += FileSkippedCallback;

            TransferManager.Configurations.ParallelOperations = 50;
            Console.WriteLine("Transfer started");

            try
            {
                Task  task = TransferManager.CopyDirectoryAsync(sourceBlobDir, destBlobDir, false, options, context);
                await task;
            }
            catch (Exception e)
            {
                Console.WriteLine("The transfer is cancelled: {0}", e.Message);
            }

            Console.WriteLine("The transfer is completed.");
        }
        /// <summary>
        /// Upload local pictures to azure storage.
        ///   1. Upload png files starting with "azure" in the source directory as block blobs, not including the sub-directory
        ///   2. Set their content type to "image/png".
        /// </summary>
        private static async Task BlobDirectoryUploadSample()
        {
            string             sourceDirPath = ".";
            CloudBlobDirectory destDir       = await Util.GetCloudBlobDirectoryAsync(ContainerName, "blobdir");

            // SearchPattern and Recuresive can be used to determine the files to be transferred from the source directory. The behavior of
            // SearchPattern and Recuresive varies for different source directory types.
            // See https://azure.github.io/azure-storage-net-data-movement for more details.
            //
            // When source is local directory, data movement library matches source files against the SearchPattern as standard wildcards. If
            // recuresive is set to false, only files directly under the source directory will be matched. Otherwise, all files in the
            // sub-directory will be matched as well.
            //
            // In the following case, data movement library will upload png files starting with "azure" in the source directory as block blobs,
            // not including the sub-directory.
            UploadDirectoryOptions options = new UploadDirectoryOptions()
            {
                SearchPattern = "azure*.png",
                Recursive     = false,
                BlobType      = BlobType.BlockBlob
            };

            // Register for transfer event.
            DirectoryTransferContext context = new DirectoryTransferContext();

            context.FileTransferred += FileTransferredCallback;
            context.FileFailed      += FileFailedCallback;
            context.FileSkipped     += FileSkippedCallback;

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

            context.ShouldTransferCallback = (source, destination) =>
            {
                // Can add more logic here to evaluate whether really need to transfer the target.
                return(true);
            };

            // Start the upload
            var trasferStatus = await TransferManager.UploadDirectoryAsync(sourceDirPath, destDir, options, context);

            Console.WriteLine("Final transfer state: {0}", TransferStatusToString(trasferStatus));
            Console.WriteLine("Files in directory {0} uploading to {1} is finished.", sourceDirPath, destDir.Uri.ToString());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Copy data between Azure storage.
        ///   1. Copy a CloudBlobDirectory
        ///   2. Cancel the transfer before it finishes with a CancellationToken
        ///   3. Store the transfer checkpoint into a file after transfer being cancelled
        ///   4. Reload checkpoint from the file
        ///   4. Resume the transfer with the loaded checkpoint
        /// </summary>
        private static async Task BlobDirectoryCopySample()
        {
            CloudBlobDirectory sourceBlobDir = await Util.GetCloudBlobDirectoryAsync(ContainerName, "blobdir");

            CloudBlobDirectory destBlobDir = await Util.GetCloudBlobDirectoryAsync(ContainerName, "blobdir2");

            // When source is CloudBlobDirectory:
            //   1. If recursive is set to true, data movement library matches the source blob name against SearchPattern as prefix.
            //   2. Otherwise, data movement library matches the blob with the exact name specified by SearchPattern.
            //
            // You can also replace the source directory with a CloudFileDirectory instance to copy data from Azure File Storage. If so:
            //   1. If recursive is set to true, SearchPattern is not supported. Data movement library simply transfer all azure files
            //      under the source CloudFileDirectory and its sub-directories.
            //   2. Otherwise, data movement library matches the azure file with the exact name specified by SearchPattern.
            //
            // In the following case, data movement library will copy all blobs with the prefix "azure" in source blob directory.
            CopyDirectoryOptions options = new CopyDirectoryOptions()
            {
                SearchPattern = "azure",
                Recursive     = true,
            };

            DirectoryTransferContext context = new DirectoryTransferContext();

            context.FileTransferred += FileTransferredCallback;
            context.FileFailed      += FileFailedCallback;
            context.FileSkipped     += FileSkippedCallback;

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

            TransferCheckpoint checkpoint    = null;
            TransferStatus     trasferStatus = null;

            try
            {
                Task <TransferStatus> task = TransferManager.CopyDirectoryAsync(sourceBlobDir, destBlobDir, false /* isServiceCopy */, 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 be copied after resume.
                Thread.Sleep(1000);
                Console.WriteLine("Cancel the transfer.");
                cancellationSource.Cancel();

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

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

            // Serialize the checkpoint into a file
#if DOTNET5_4
            var formatter = new DataContractSerializer(typeof(TransferCheckpoint));
#else
            IFormatter formatter = new BinaryFormatter();
#endif

            string tempFileName = Guid.NewGuid().ToString();
            using (var stream = new FileStream(tempFileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                formatter.Serialize(stream, checkpoint);
            }

            // Deserialize the checkpoint from the file
            using (var stream = new FileStream(tempFileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                checkpoint = formatter.Deserialize(stream) as TransferCheckpoint;
            }

            File.Delete(tempFileName);

            // Create a new TransferContext with the store checkpoint
            DirectoryTransferContext resumeContext = new DirectoryTransferContext(checkpoint);
            resumeContext.FileTransferred += FileTransferredCallback;
            resumeContext.FileFailed      += FileFailedCallback;
            resumeContext.FileSkipped     += FileSkippedCallback;

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

            // Resume transfer from the stored checkpoint
            Console.WriteLine("Resume the cancelled transfer.");
            trasferStatus = await TransferManager.CopyDirectoryAsync(sourceBlobDir, destBlobDir, false /* isServiceCopy */, options, resumeContext);

            // Print out the final transfer state
            Console.WriteLine("Final transfer state: {0}", TransferStatusToString(trasferStatus));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Upload local pictures to azure storage.
        ///   1. Upload png files starting with "azure" in the source directory as block blobs, not including the sub-directory
        ///      and store transfer context in a streamed journal.
        ///   2. Set their content type to "image/png".
        ///   3. Cancel the transfer before it finishes with a CancellationToken
        ///   3. Reload the transfer context from the streamed journal.
        ///   4. Resume the transfer with the loaded transfer context
        /// </summary>
        private static async Task BlobDirectoryUploadSample()
        {
            string             sourceDirPath = ".";
            CloudBlobDirectory destDir       = await Util.GetCloudBlobDirectoryAsync(ContainerName, "blobdir");

            // SearchPattern and Recuresive can be used to determine the files to be transferred from the source directory. The behavior of
            // SearchPattern and Recuresive varies for different source directory types.
            // See https://azure.github.io/azure-storage-net-data-movement for more details.
            //
            // When source is local directory, data movement library matches source files against the SearchPattern as standard wildcards. If
            // recuresive is set to false, only files directly under the source directory will be matched. Otherwise, all files in the
            // sub-directory will be matched as well.
            //
            // In the following case, data movement library will upload png files starting with "azure" in the source directory as block blobs,
            // not including the sub-directory.
            UploadDirectoryOptions options = new UploadDirectoryOptions()
            {
                SearchPattern = "azure*.png",
                Recursive     = false,
                BlobType      = BlobType.BlockBlob
            };

            using (MemoryStream journalStream = new MemoryStream())
            {
                // Store the transfer context in a streamed journal.
                DirectoryTransferContext context = new DirectoryTransferContext(journalStream);

                // Register for transfer event.
                context.FileTransferred += FileTransferredCallback;
                context.FileFailed      += FileFailedCallback;
                context.FileSkipped     += FileSkippedCallback;

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

                context.ShouldTransferCallbackAsync = async(source, destination) =>
                {
                    // Can add more logic here to evaluate whether really need to transfer the target.
                    return(true);
                };

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

                TransferStatus trasferStatus = null;

                try
                {
                    // Start the upload
                    Task <TransferStatus> task = TransferManager.UploadDirectoryAsync(sourceDirPath, destDir, 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 be copied after resume.
                    Thread.Sleep(1000);
                    Console.WriteLine("Cancel the transfer.");
                    cancellationSource.Cancel();

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

                journalStream.Position = 0;

                // Deserialize transfer context from the streamed journal.
                DirectoryTransferContext resumeContext = new DirectoryTransferContext(journalStream);
                resumeContext.FileTransferred += FileTransferredCallback;
                resumeContext.FileFailed      += FileFailedCallback;
                resumeContext.FileSkipped     += FileSkippedCallback;

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

                resumeContext.ShouldTransferCallbackAsync = async(source, destination) =>
                {
                    // Can add more logic here to evaluate whether really need to transfer the target.
                    return(true);
                };

                // Resume the upload
                trasferStatus = await TransferManager.UploadDirectoryAsync(sourceDirPath, destDir, options, resumeContext);

                Console.WriteLine("Final transfer state: {0}", TransferStatusToString(trasferStatus));
                Console.WriteLine("Files in directory {0} uploading to {1} is finished.", sourceDirPath, destDir.Uri.ToString());
            }
        }