/// <summary> /// Upload workspace package /// </summary> internal virtual async Task UploadWorkspacePackage(long taskId, string filePath, string packageName) { // Check if the package already exists bool exists = await this.SynapseAnalyticsClient.TestPackageAsync(packageName); if (exists) { throw new AzPSInvalidOperationException(string.Format(Resources.WorkspacePackageExists, packageName, this.WorkspaceName)); } // Prepare progress handler long fileSize = new FileInfo(filePath).Length; string activity = String.Format(Resources.UploadWorkspacePackageActivity, filePath); string status = Resources.PrepareUploadingPackage; ProgressRecord pr = new ProgressRecord(OutputStream.GetProgressId(taskId), activity, status); IProgress <long> progressHandler = new Progress <long>((finishedBytes) => { if (pr != null) { // Size of the source file might be 0, when it is, directly treat the progress as 100 percent. pr.PercentComplete = 0 == fileSize ? 100 : (int)(finishedBytes * 100 / fileSize); pr.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.FileTransmitStatus, pr.PercentComplete); this.OutputStream.WriteProgress(pr); } }); using (FileStream stream = System.IO.File.OpenRead(filePath)) { // Create Package await this.SynapseAnalyticsClient.CreatePackageAsync(packageName); // Upload package content byte[] uploadcache4MB = null; byte[] uploadcache = null; progressHandler.Report(0); long offset = 0; while (offset < fileSize) { // Get chunk size and prepare cache int chunksize = size4MB; if (chunksize <= (fileSize - offset)) // Chunk size will be 4MB { if (uploadcache4MB == null) { uploadcache4MB = new byte[size4MB]; } uploadcache = uploadcache4MB; } else // last chunk can < 4MB { chunksize = (int)(fileSize - offset); if (uploadcache4MB == null) { uploadcache = new byte[chunksize]; } else { uploadcache = uploadcache4MB; } } // Get content to upload for the chunk int readoutcount = await stream.ReadAsync(uploadcache, 0, (int)chunksize).ConfigureAwait(false); MemoryStream chunkContent = new MemoryStream(uploadcache, 0, readoutcount); // Upload content await this.SynapseAnalyticsClient.AppendPackageAsync(packageName, chunkContent); // Update progress offset += readoutcount; progressHandler.Report(offset); } } // Call Flush API as a completion signal await this.SynapseAnalyticsClient.FlushPackageAsync(packageName); }