예제 #1
0
        private string UploadPackage()
        {
            if (!string.IsNullOrEmpty(this.PackageFileStorageLocation))
            {
                this.LogDebug("Using stored file at location {0}", this.PackageFileStorageLocation);
                return(this.PackageFileStorageLocation);
            }

            try
            {
                this.LogDebug("Preparing to upload file...");
                var account    = new CloudStorageAccount(new StorageCredentials(this.StorageAccountName, this.StorageAccessKey), true);
                var blobClient = account.CreateCloudBlobClient();
                blobClient.SingleBlobUploadThresholdInBytes = 63 * 1024 * 1024;
                var container = blobClient.GetContainerReference(BlobContainer);
                this.LogDebug("Creating container \"{0}\" if it doesn't already exist...", BlobContainer);
                container.CreateIfNotExists();
                string package = this.ResolveDirectory(this.PackageFile);
                if (!File.Exists(package))
                {
                    this.LogError("UploadPackage unable to locate package file at: {0}", package);
                    return(null);
                }
                string blobFileName = Path.GetFileNameWithoutExtension(package) + Guid.NewGuid().ToString() + (Path.HasExtension(package) ? Path.GetExtension(package) : "");
                var    blob         = container.GetBlockBlobReference(blobFileName);
                blob.StreamWriteSizeInBytes = 64 * 1024;
                this.LogInformation("Uploading package {0} to blob file {1} for deployment.", package, blobFileName);

                var transfer = new BlobTransfer(blob);
                transfer.TransferProgressChanged += (s, e) =>
                {
                    this.LogDebug("Upload Progress: {0}/{1} ({2}%) - Est. Time Remaining: {3}", e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage, e.TimeRemaining);
                };
                transfer.TransferCompleted += (s, e) =>
                {
                    this.LogInformation("Package {0} uploaded successfully.", package);
                };

                var result = transfer.UploadBlobAsync(package);

                int handled = WaitHandle.WaitAny(new[] { result.AsyncWaitHandle, this.Context.CancellationToken.WaitHandle });
                if (handled == 1)
                {
                    result.Cancel();
                    this.ThrowIfCanceledOrTimeoutExpired();
                }

                return(blob.Uri.ToString());
            }
            catch (Exception ex)
            {
                this.LogError("UploadPackage error: {0}", ex.ToString());
                return(null);
            }
        }
예제 #2
0
        private string UploadPackage()
        {
            if (!string.IsNullOrEmpty(this.PackageFileStorageLocation))
            {
                this.LogDebug("Using stored file at location {0}", this.PackageFileStorageLocation);
                return this.PackageFileStorageLocation;
            }

            try
            {
                this.LogDebug("Preparing to upload file...");
                var account = new CloudStorageAccount(new StorageCredentials(this.StorageAccountName, this.StorageAccessKey), true);
                var blobClient = account.CreateCloudBlobClient();
                blobClient.SingleBlobUploadThresholdInBytes = 63 * 1024 * 1024;
                var container = blobClient.GetContainerReference(BlobContainer);
                this.LogDebug("Creating container \"{0}\" if it doesn't already exist...", BlobContainer);
                container.CreateIfNotExists();
                string package = this.ResolveDirectory(this.PackageFile);
                if (!File.Exists(package))
                {
                    this.LogError("UploadPackage unable to locate package file at: {0}", package);
                    return null;
                }
                string blobFileName = Path.GetFileNameWithoutExtension(package) + Guid.NewGuid().ToString() + (Path.HasExtension(package) ? Path.GetExtension(package) : "");
                var blob = container.GetBlockBlobReference(blobFileName);
                blob.StreamWriteSizeInBytes = 64 * 1024;
                this.LogInformation("Uploading package {0} to blob file {1} for deployment.", package, blobFileName);

                var transfer = new BlobTransfer(blob);
                transfer.TransferProgressChanged += (s, e) =>
                {
                    this.LogDebug("Upload Progress: {0}/{1} ({2}%) - Est. Time Remaining: {3}", e.BytesSent, e.TotalBytesToSend, e.ProgressPercentage, e.TimeRemaining);
                };
                transfer.TransferCompleted += (s, e) =>
                {
                    this.LogInformation("Package {0} uploaded successfully.", package);
                };

                var result = transfer.UploadBlobAsync(package);

                int handled = WaitHandle.WaitAny(new[] { result.AsyncWaitHandle, this.Context.CancellationToken.WaitHandle });
                if (handled == 1)
                {
                    result.Cancel();
                    this.ThrowIfCanceledOrTimeoutExpired();
                }

                return blob.Uri.ToString();
            }
            catch (Exception ex)
            {
                this.LogError("UploadPackage error: {0}", ex.ToString());
                return null;
            }
        }