public async Task <ITransferClient> CreateClientAsync(IRelativityTransferHost host, CancellationToken token) { Console2.WriteStartHeader("Create Client"); ITransferClient client; if (this._clientConfiguration.Client == WellKnownTransferClient.Unassigned) { // The CreateClientAsync method chooses the best client at runtime. Console2.WriteLine("TAPI is choosing the best transfer client..."); client = await host.CreateClientAsync(this._clientConfiguration, token).ConfigureAwait(false); } else { // The CreateClient method creates the specified client. Console2.WriteLine("The API caller specified the {0} transfer client.", this._clientConfiguration.Client); client = host.CreateClient(this._clientConfiguration); } if (client == null) { throw new InvalidOperationException("This operation cannot be performed because a transfer client could not be created."); } Console2.WriteLine("TAPI created the {0} transfer client.", client.DisplayName); Console2.WriteEndHeader(); return(client); }
public async Task TransferDocumentsAsync() { Console2.WriteDisplayStartLine("Transferring Documents"); try { InitializeGlobalSettings(); using (ITransferLog transferLog = CreateTransferLog()) { using (IRelativityTransferHost relativityTransferHost = CreateRelativityTransferHost(transferLog)) { using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) { CancellationToken cancellationToken = cancellationTokenSource.Token; await UploadMultipleDocumentsAsync(relativityTransferHost, cancellationToken).ConfigureAwait(false); } } } Console2.WriteDisplayEndLine("Transferred Documents!"); } catch (TransferException e) { if (e.Fatal) { Console2.WriteDebugLine(ConsoleColor.Red, "A fatal transfer failure has occurred. Error: " + e); } else { Console2.WriteDebugLine(ConsoleColor.Red, "A non-fatal transfer failure has occurred. Error: " + e); } } catch (ApplicationException e) { // No need to include the stacktrace. Console2.WriteDebugLine(ConsoleColor.Red, e.Message); } catch (Exception e) { Console2.WriteDebugLine(ConsoleColor.Red, "An unexpected error has occurred. Error: " + e); } }
public async Task <RelativityFileShare> GetFileShareAsync(IRelativityTransferHost host, int number, CancellationToken token) { Console2.WriteStartHeader("Get Specified file share"); IFileStorageSearch fileStorageSearch = host.CreateFileStorageSearch(); // Admin rights are required but this allows searching for all possible file shares within the instance. FileStorageSearchContext context = new FileStorageSearchContext { WorkspaceId = Workspace.AdminWorkspaceId }; FileStorageSearchResults results = await fileStorageSearch.SearchAsync(context, token).ConfigureAwait(false); // Specify the cloud-based logical file share number - or just the 1st file share when all else fails. RelativityFileShare fileShare = results.GetRelativityFileShare(number) ?? results.FileShares.FirstOrDefault(); if (fileShare == null) { throw new InvalidOperationException("This operation cannot be performed because there are no file shares available."); } this._consolePrinter.DisplayFileShare(fileShare); Console2.WriteEndHeader(); return(fileShare); }
private static async Task DemoTransferAsync(IRelativityTransferHost host, CancellationToken token, SampleRunner sampleRunner) { // Search for the first logical file share. const int LogicalFileShareNumber = 1; RelativityFileShare fileShare = await sampleRunner.GetFileShareAsync(host, LogicalFileShareNumber, token).ConfigureAwait(false); // Assigning the file share bypasses auto-configuration that will normally use the default workspace repository. sampleRunner.AssignFileshare(fileShare); // Prepare transfer setup common for upload and download. IList <TransferPath> localSourcePaths = await sampleRunner.SearchLocalSourcePathsAsync(token).ConfigureAwait(false); string uploadTargetPath = sampleRunner.GetUniqueRemoteTargetPath(fileShare); TransferContext context = sampleRunner.CreateTransferContext(); using (ITransferClient client = await sampleRunner.CreateClientAsync(host, token).ConfigureAwait(false)) using (AutoDeleteDirectory directory = new AutoDeleteDirectory()) { await Upload(client, context, localSourcePaths, uploadTargetPath, sampleRunner, token); await Download(client, directory, context, localSourcePaths, uploadTargetPath, sampleRunner, token); } }
public static void Main(string[] args) { ClientConfigurationFactory clientConfigurationFactory = new ClientConfigurationFactory(); Console2.Initialize(); int exitCode = 1; try { //Create specific ClientConfiguration based on TransferMode in app.config ClientConfiguration clientConfiguration = clientConfigurationFactory.Create(); SampleRunner sampleRunner = new SampleRunner(clientConfiguration); sampleRunner.InitializeGlobalSettings(); Console2.WriteLine($"Relativity {sampleRunner.TransferModeName} Transfer Sample"); Task.Run( async() => { // Note: the RelativityTransferLog demonstrates how to create an ITransferLog implementation for Relativity Logging. using (ITransferLog transferLog = sampleRunner.CreateTransferLog()) using (IRelativityTransferHost host = sampleRunner.CreateRelativityTransferHost(transferLog) ) using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) { CancellationToken token = cancellationTokenSource.Token; await DemoTransferAsync(host, token, sampleRunner).ConfigureAwait(false); exitCode = 0; } }).GetAwaiter().GetResult(); } catch (TransferException e) { if (e.Fatal) { Console2.WriteLine(ConsoleColor.Red, "A fatal transfer failure has occurred. Error: " + e); } else { Console2.WriteLine(ConsoleColor.Red, "A non-fatal transfer failure has occurred. Error: " + e); } } catch (ApplicationException e) { // No need to include the stacktrace. Console2.WriteLine(ConsoleColor.Red, e.Message); } catch (ConfigurationValueInvalidException e) { // No need to include the stacktrace. Console2.WriteLine(ConsoleColor.Red, e.Message); } catch (Exception e) { Console2.WriteLine(ConsoleColor.Red, "An unexpected error has occurred. Error: " + e); } finally { Console2.WriteTerminateLine(exitCode); Environment.Exit(exitCode); } }
private async Task UploadMultipleDocumentsAsync(IRelativityTransferHost relativityTransferHost, CancellationToken cancellationToken) { // Search for the first logical file share. const int logicalFileShareNumber = 1; RelativityFileShare fileShare = await GetFileShareAsync(relativityTransferHost, logicalFileShareNumber, cancellationToken).ConfigureAwait(false); // Configure an Aspera specific transfer. AsperaClientConfiguration configuration = CreateAsperaClientConfiguration(); // Assigning the file share bypasses auto-configuration that will normally use the default workspace repository. configuration.TargetFileShare = fileShare; using (ITransferClient client = await CreateClientAsync(relativityTransferHost, configuration, cancellationToken).ConfigureAwait(false)) using (AutoDeleteDirectory directory = new AutoDeleteDirectory()) { // Create a job-based upload transfer request. Console2.WriteTapiStartHeader("Advanced Transfer - Upload"); string uploadTargetPath = GetUniqueRemoteTargetPath(fileShare); IList <TransferPath> localSourcePaths = await SearchLocalSourcePathsAsync(client, uploadTargetPath, cancellationToken).ConfigureAwait(false); TransferContext context = CreateTransferContext(); TransferRequest uploadJobRequest = TransferRequest.ForUploadJob(uploadTargetPath, context); uploadJobRequest.Application = "Github Sample"; uploadJobRequest.Name = "Advanced Upload Sample"; // Create a transfer job to upload the local sample data set to the target remote path. using (ITransferJob job = await client.CreateJobAsync(uploadJobRequest, cancellationToken).ConfigureAwait(false)) { Console2.WriteDebugLine("Advanced upload started."); // Paths added to the async job are transferred immediately. await job.AddPathsAsync(localSourcePaths, cancellationToken).ConfigureAwait(false); // Await completion of the job. ITransferResult result = await job.CompleteAsync(cancellationToken).ConfigureAwait(false); Console2.WriteDebugLine("Advanced upload completed."); DisplayTransferResult(result); Console2.WriteTapiEndHeader(); } // Create a job-based download transfer request. Console2.WriteTapiStartHeader("Advanced Transfer - Download"); string downloadTargetPath = directory.Path; TransferRequest downloadJobRequest = TransferRequest.ForDownloadJob(downloadTargetPath, context); downloadJobRequest.Application = "Github Sample"; downloadJobRequest.Name = "Advanced Download Sample"; Console2.WriteDebugLine("Advanced download started."); // Create a transfer job to download the sample data set to the target local path. using (ITransferJob job = await client.CreateJobAsync(downloadJobRequest, cancellationToken).ConfigureAwait(false)) { IEnumerable <TransferPath> remotePaths = localSourcePaths.Select(localPath => new TransferPath { SourcePath = uploadTargetPath + "\\" + Path.GetFileName(localPath.SourcePath), PathAttributes = TransferPathAttributes.File, TargetPath = downloadTargetPath }); await job.AddPathsAsync(remotePaths, cancellationToken).ConfigureAwait(false); await ChangeDataRateAsync(job, cancellationToken).ConfigureAwait(false); // Await completion of the job. ITransferResult result = await job.CompleteAsync(cancellationToken).ConfigureAwait(false); Console2.WriteDebugLine("Advanced download completed."); DisplayTransferResult(result); Console2.WriteTapiEndHeader(); } } }