/// <summary> /// Sets content disposition header. /// </summary> /// <param name="headers">The http content request header.</param> /// <param name="request">The <see cref="DownloadFileByIdRequest"/>.</param> public static void ContentDisposition(this HttpContentHeaders headers, DownloadFileByNameRequest request) { if (request.ContentDisposition != null) { headers.ContentDisposition = request.ContentDisposition; } }
/// <summary> /// Downloads a file by bucket and file name from Backblaze B2 Cloud Storage. /// </summary> /// <param name="bucketName">The name of the bucket to download from.</param> /// <param name="fileName">The name of the file to download.</param> /// <param name="localPath">The relative or absolute path to the file. This string is not case-sensitive.</param> /// <param name="progress">A progress action which fires every time the write buffer is cycled.</param> /// <param name="cancel">The cancellation token to cancel operation.</param> /// <exception cref="AuthenticationException">Thrown when authentication fails.</exception> /// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception> /// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception> async Task <IApiResults <DownloadFileResponse> > IStorageFiles.DownloadAsync (string bucketName, string fileName, string localPath, IProgress <ICopyProgress> progress, CancellationToken cancel) { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } using (var content = File.Create(localPath)) { var request = new DownloadFileByNameRequest(bucketName, fileName); var results = await _client.DownloadAsync(request, content, progress, cancel); if (results.IsSuccessStatusCode) { var lastModified = results.Response.FileInfo.GetLastModified(); if (lastModified != DateTime.MinValue) { File.SetLastWriteTime(localPath, lastModified); } } return(results); } }
/// <summary> /// Download a specific version of content by bucket and file name from Backblaze B2 Cloud Storage. /// </summary> /// <param name="bucketName">The unique name of the bucket the file is in.</param> /// <param name="fileName">The name of the file to download.</param> /// <param name="content">The download content to receive.</param> /// <exception cref="AuthenticationException">Thrown when authentication fails.</exception> /// <exception cref="CapExceededExecption">Thrown when a cap is exceeded or an account in bad standing.</exception> /// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception> /// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception> public async Task <IApiResults <DownloadFileResponse> > DownloadAsync (string bucketName, string fileName, Stream content) { var request = new DownloadFileByNameRequest(bucketName, fileName); return(await DownloadAsync(request, content, null, _cancellationToken).ConfigureAwait(false)); }
public async Task DownloadAsync() { var fileSystem = new MockFileSystem(); string fileSha1 = string.Empty; int progressCounter = 0; TimeSpan transferTime = TimeSpan.Zero; long bytesPerSecond = 0; long bytesTransferred = 0; long expectedBytes = 0; double percentComplete = 0; var progress = new NaiveProgress <ICopyProgress>(x => { progressCounter++; transferTime = x.TransferTime; bytesPerSecond = x.BytesPerSecond; bytesTransferred = x.BytesTransferred; expectedBytes = x.ExpectedBytes; percentComplete = x.PercentComplete; }); using (var content = fileSystem.File.Create(_fileName)) { var request = new DownloadFileByNameRequest(BucketName, _fileName); var results = await Storage.DownloadAsync(request, content, progress, CancellationToken.None); if (results.IsSuccessStatusCode) { fileSha1 = results.Response.ContentSha1; } } var downloadSha1 = fileSystem.File.OpenRead(_fileName).ToSha1(); if (!downloadSha1.Equals(fileSha1)) { throw new InvalidOperationException(); } Assert.Single(fileSystem.AllFiles); Assert.True(progressCounter > 0); Assert.True(transferTime > TimeSpan.Zero); Assert.True(bytesPerSecond > 0); Assert.Equal(524288, bytesTransferred); Assert.Equal(524288, expectedBytes); Assert.Equal(1, percentComplete); }
/// <summary> /// Downloads the most recent version of a large file in chunked parts. /// </summary> /// <param name="request">The <see cref="DownloadFileByIdRequest"/> content to send.</param> /// <param name="results">The <see cref="DownloadFileResponse"/> results.</param> /// <param name="content">The download content to receive.</param> /// <param name="progress">A progress action which fires every time the write buffer is cycled.</param> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> private async Task <IApiResults <DownloadFileResponse> > DownloadLargeFileAsync (DownloadFileByNameRequest request, IApiResults <DownloadFileResponse> results, Stream content, IProgress <ICopyProgress> progress, CancellationToken cancellationToken) { var parts = GetContentParts(results.Response.ContentLength, Options.DownloadPartSize); foreach (var part in parts) { var mmultiStream = new MultiStream(content, part.Position, part.Length); var partReqeust = new DownloadFileByNameRequest(request.BucketName, request.FileName) { Range = part.RangeHeader }; var partResults = await DownloadFileByNameAsync(partReqeust, mmultiStream, progress, cancellationToken).ConfigureAwait(false); if (!partResults.IsSuccessStatusCode) { return(new ApiResults <DownloadFileResponse>(partResults.HttpResponse, partResults.Error)); } } return(results); }
/// <summary> /// Downloads the most recent version of content by bucket and file name. /// </summary> /// <param name="request">The <see cref="DownloadFileByIdRequest"/> to send.</param> /// <param name="content">The download content to receive.</param> /// <param name="progress">A progress action which fires every time the write buffer is cycled.</param> /// <param name="cancel">The cancellation token to cancel operation.</param> public async Task <IApiResults <DownloadFileResponse> > DownloadAsync (DownloadFileByNameRequest request, Stream content, IProgress <ICopyProgress> progress, CancellationToken cancel) { return(await _policy.InvokeDownload.ExecuteAsync(async() => { var fileRequest = new DownloadFileByNameRequest(request.BucketName, request.FileName); var fileResults = await DownloadFileByNameAsync(fileRequest, null, null, cancel); if (fileResults.IsSuccessStatusCode) { if (fileResults.Response.ContentLength < Options.DownloadCutoffSize) { return await DownloadFileByNameAsync(request, content, progress, cancel).ConfigureAwait(false); } else { return await DownloadLargeFileAsync(fileRequest, fileResults, content, progress, cancel).ConfigureAwait(false); } } return fileResults; }).ConfigureAwait(false)); }
/// <summary> /// Downloads the most recent version of a file by name. /// </summary> /// <param name="request">The <see cref="DownloadFileByIdRequest"/> to send.</param> /// <param name="content">The download content to receive.</param> /// <param name="progress">A progress action which fires every time the write buffer is cycled.</param> /// <exception cref="AuthenticationException">Thrown when authentication fails.</exception> /// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception> /// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception> async Task <IApiResults <DownloadFileResponse> > IStorageFiles.DownloadAsync (DownloadFileByNameRequest request, Stream content, IProgress <ICopyProgress> progress) { return(await _client.DownloadFileByNameAsync(request, content, progress, _cancellationToken)); }
/// <summary> /// Download a specific version of content by bucket and file name from Backblaze B2 Cloud Storage. /// </summary> /// <param name="request">The <see cref="DownloadFileByIdRequest"/> to send.</param> /// <param name="content">The download content to receive.</param> /// <param name="progress">A progress action which fires every time the write buffer is cycled.</param> /// <param name="cancel">The cancellation token to cancel operation.</param> /// <exception cref="AuthenticationException">Thrown when authentication fails.</exception> /// <exception cref="CapExceededExecption">Thrown when a cap is exceeded or an account in bad standing.</exception> /// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception> /// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception> public async Task <IApiResults <DownloadFileResponse> > DownloadAsync (DownloadFileByNameRequest request, Stream content, IProgress <ICopyProgress> progress, CancellationToken cancel) { return(await _client.DownloadAsync(request, content, progress, cancel).ConfigureAwait(false)); }