Skip to content

Commit 5f70633

Browse files
Merge pull request #25 from logiclrd/JDG_UploadWithoutActualFile
Add support for uploading without actually having a physical file
2 parents 49dc556 + 170f967 commit 5f70633

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed

src/Client/Client/Storage/IStorageFiles.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,25 @@ public interface IStorageFiles
186186
Task<IApiResults<UploadFileResponse>> UploadAsync(string bucketId, string fileName, string localPath, IProgress<ICopyProgress> progress, CancellationToken cancel);
187187

188188
/// <summary>
189-
/// Downloads a file by bucket and file name from Backblaze B2 Cloud Storage.
189+
/// Uploads a file by bucket id and filename to Backblaze B2 Cloud Storage, with the file content and attributes supplied explicitly by the caller.
190+
/// </summary>
191+
/// <param name="bucketId">The bucket id you want to upload to.</param>
192+
/// <param name="fileName">The name of the file to upload.</param>
193+
/// <param name="content">A stream from which the content to upload for the file can be read.</param>
194+
/// <param name="lastModified">The DateTime to register as the file's last-modified date/time.</param>
195+
/// <param name="isReadOnly">True if the file should be marked read-only.</param>
196+
/// <param name="isHidden">True if the file should be marked hidden.</param>
197+
/// <param name="isArchive">True if the file should be marked for archiving.</param>
198+
/// <param name="isCompressed">True if the file should be marked compressed.</param>
199+
/// <param name="progress">A progress action which fires every time the write buffer is cycled.</param>
200+
/// <param name="cancel">The cancellation token to cancel operation.</param>
201+
/// <returns></returns>
202+
Task<IApiResults<UploadFileResponse>> UploadAsync
203+
(string bucketId, string fileName, Stream content, DateTime lastModified, bool isReadOnly, bool isHidden, bool isArchive, bool isCompressed,
204+
IProgress<ICopyProgress> progress, CancellationToken cancel);
205+
206+
/// <summary>
207+
/// Downloads a file by bucket and file name from Backblaze B2 Cloud Storage.
190208
/// </summary>
191209
/// <param name="bucketName">The name of the bucket to download from.</param>
192210
/// <param name="fileName">The name of the file to download.</param>

src/Client/Client/Storage/Storage.Files.cs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -271,45 +271,56 @@ async Task<IApiResults<ListUnfinishedLargeFilesResponse>> IStorageFiles.ListUnfi
271271
/// <exception cref="CapExceededExecption">Thrown when a cap is exceeded or an account in bad standing.</exception>
272272
/// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception>
273273
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
274-
async Task<IApiResults<UploadFileResponse>> IStorageFiles.UploadAsync
274+
Task<IApiResults<UploadFileResponse>> IStorageFiles.UploadAsync
275275
(string bucketId, string fileName, string localPath, IProgress<ICopyProgress> progress, CancellationToken cancel)
276276
{
277277
using (var content = File.OpenRead(localPath))
278278
{
279-
var fileInfo = new Models.FileInfo();
280-
281-
// get last modified date
282279
DateTime lastModified = File.GetLastWriteTime(localPath);
283280

284-
// check whether a file is read only
285281
var isReadOnly = ((File.GetAttributes(localPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
286-
fileInfo.Add("src_file_readonly", isReadOnly.ToString().ToLower());
287-
288-
// check whether a file is hidden
289282
var isHidden = ((File.GetAttributes(localPath) & FileAttributes.Hidden) == FileAttributes.Hidden);
290-
fileInfo.Add("src_file_hidden", isHidden.ToString().ToLower());
291-
292-
// check whether a file has archive attribute
293283
var isArchive = ((File.GetAttributes(localPath) & FileAttributes.Archive) == FileAttributes.Archive);
294-
fileInfo.Add("src_file_archive", isArchive.ToString().ToLower());
295-
296-
// check whether a file has compressed attribute
297284
var isCompressed = ((File.GetAttributes(localPath) & FileAttributes.Compressed) == FileAttributes.Compressed);
298-
fileInfo.Add("src_file_compressed", isCompressed.ToString().ToLower());
299285

300-
var request = new UploadFileByBucketIdRequest(bucketId, fileName) { LastModified = lastModified, FileInfo = fileInfo };
301-
var results = await _client.UploadAsync(request, content, progress, cancel);
302-
if (results.IsSuccessStatusCode)
303-
{
304-
_logger.LogInformation($"Successfully uploaded '{localPath}' file to '{bucketId}' bucket id");
305-
}
306-
else
307-
{
308-
_logger.LogError($"Failed uploading '{localPath}' file with error: {results.Error?.Message}");
309-
}
286+
return ((IStorageFiles)this).UploadAsync(
287+
bucketId,
288+
fileName,
289+
content,
290+
lastModified,
291+
isReadOnly,
292+
isHidden,
293+
isArchive,
294+
isCompressed,
295+
progress,
296+
cancel);
297+
}
298+
}
310299

311-
return results;
300+
async Task<IApiResults<UploadFileResponse>> IStorageFiles.UploadAsync
301+
(string bucketId, string fileName, Stream content, DateTime lastModified, bool isReadOnly, bool isHidden, bool isArchive, bool isCompressed,
302+
IProgress<ICopyProgress> progress, CancellationToken cancel)
303+
{
304+
var fileInfo = new Models.FileInfo();
305+
306+
fileInfo.Add("src_file_readonly", isReadOnly ? "true" : "false");
307+
fileInfo.Add("src_file_hidden", isHidden ? "true" : "false");
308+
fileInfo.Add("src_file_archive", isArchive ? "true" : "false");
309+
fileInfo.Add("src_file_compressed", isCompressed ? "true" : "false");
310+
311+
var request = new UploadFileByBucketIdRequest(bucketId, fileName) { LastModified = lastModified, FileInfo = fileInfo };
312+
var results = await _client.UploadAsync(request, content, progress, cancel);
313+
314+
if (results.IsSuccessStatusCode)
315+
{
316+
_logger.LogInformation($"Successfully uploaded in-memory file to '{bucketId}' bucket id with filename '{fileName}'");
312317
}
318+
else
319+
{
320+
_logger.LogError($"Failed uploading in-memory file '{fileName}' with error: {results.Error?.Message}");
321+
}
322+
323+
return results;
313324
}
314325

315326
/// <summary>

0 commit comments

Comments
 (0)