Write Options.

This commit is contained in:
David Lebee 2020-10-27 16:04:07 -04:00
parent 21cb1d11bf
commit f7bfdc18e8
7 changed files with 205 additions and 46 deletions

View File

@ -146,40 +146,28 @@ namespace PoweredSoft.Storage.Azure.Blob
return ret; return ret;
} }
public async Task<IFileInfo> WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true) public Task<IFileInfo> WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true)
{ {
if (!overrideIfExists && await FileExistsAsync(path)) return WriteFileAsync(sourcePath, path, new DefaultWriteOptions
throw new FileAlreadyExistsException(path); {
OverrideIfExists = overrideIfExists
var container = GetContainer(); });
var blob = container.GetBlockBlobReference(path);
await blob.UploadFromFileAsync(sourcePath);
return new AzureBlobFileInfo(blob);
} }
public async Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true) public Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true)
{ {
if (!overrideIfExists && await FileExistsAsync(path)) return WriteFileAsync(bytes, path, new DefaultWriteOptions
throw new FileAlreadyExistsException(path); {
OverrideIfExists = overrideIfExists
var container = GetContainer(); });
var blob = container.GetBlockBlobReference(path);
await blob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);
return new AzureBlobFileInfo(blob);
} }
public async Task<IFileInfo> WriteFileAsync(Stream stream, string path, bool overrideIfExists = true) public Task<IFileInfo> WriteFileAsync(Stream stream, string path, bool overrideIfExists = true)
{ {
if (!overrideIfExists && await FileExistsAsync(path)) return WriteFileAsync(stream, path, new DefaultWriteOptions
throw new FileAlreadyExistsException(path); {
OverrideIfExists = overrideIfExists
if (stream.CanSeek && stream.Position != 0) });
stream.Seek(0, SeekOrigin.Begin);
var container = GetContainer();
var blob = container.GetBlockBlobReference(path);
await blob.UploadFromStreamAsync(stream);
return new AzureBlobFileInfo(blob);
} }
public async Task<Stream> GetFileStreamAsync(string path) public async Task<Stream> GetFileStreamAsync(string path)
@ -222,5 +210,56 @@ namespace PoweredSoft.Storage.Azure.Blob
{ {
return key; return key;
} }
public async Task<IFileInfo> WriteFileAsync(string sourcePath, string path, IWriteFileOptions options)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
if (!options.OverrideIfExists && await FileExistsAsync(path))
throw new FileAlreadyExistsException(path);
var container = GetContainer();
var blob = container.GetBlockBlobReference(path);
await blob.UploadFromFileAsync(sourcePath);
return new AzureBlobFileInfo(blob);
}
public async Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, IWriteFileOptions options)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
if (!options.OverrideIfExists && await FileExistsAsync(path))
throw new FileAlreadyExistsException(path);
var container = GetContainer();
var blob = container.GetBlockBlobReference(path);
await blob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);
return new AzureBlobFileInfo(blob);
}
public async Task<IFileInfo> WriteFileAsync(Stream stream, string path, IWriteFileOptions options)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
if (!options.OverrideIfExists && await FileExistsAsync(path))
throw new FileAlreadyExistsException(path);
if (stream.CanSeek && stream.Position != 0)
stream.Seek(0, SeekOrigin.Begin);
var container = GetContainer();
var blob = container.GetBlockBlobReference(path);
await blob.UploadFromStreamAsync(stream);
return new AzureBlobFileInfo(blob);
}
} }
} }

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
namespace PoweredSoft.Storage.Core namespace PoweredSoft.Storage.Core
{ {
public interface IStorageProvider public interface IStorageProvider
{ {
Task<List<IDirectoryOrFile>> GetListAsync(string path); Task<List<IDirectoryOrFile>> GetListAsync(string path);
@ -13,6 +14,9 @@ namespace PoweredSoft.Storage.Core
Task<IFileInfo> WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true); Task<IFileInfo> WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true);
Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true); Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true);
Task<IFileInfo> WriteFileAsync(Stream stream, string path, bool overrideIfExists = true); Task<IFileInfo> WriteFileAsync(Stream stream, string path, bool overrideIfExists = true);
Task<IFileInfo> WriteFileAsync(string sourcePath, string path, IWriteFileOptions options);
Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, IWriteFileOptions options);
Task<IFileInfo> WriteFileAsync(Stream stream, string path, IWriteFileOptions options);
Task<Stream> GetFileStreamAsync(string path); Task<Stream> GetFileStreamAsync(string path);
Task<byte[]> GetFileBytesAsync(string path); Task<byte[]> GetFileBytesAsync(string path);
Task<string> GetFileContentAsync(string path, Encoding encoding); Task<string> GetFileContentAsync(string path, Encoding encoding);

View File

@ -0,0 +1,12 @@
namespace PoweredSoft.Storage.Core
{
public interface IWriteFileOptions
{
bool OverrideIfExists { get; }
}
public class DefaultWriteOptions : IWriteFileOptions
{
public bool OverrideIfExists { get; set; }
}
}

View File

@ -92,22 +92,33 @@ namespace PoweredSoft.Storage.Physical
return result; return result;
} }
public async Task<IFileInfo> WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true) public async Task<IFileInfo> WriteFileAsync(string sourcePath, string path, IWriteFileOptions options)
{ {
if (!overrideIfExists && await FileExistsAsync(path)) if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
if (!options.OverrideIfExists && await FileExistsAsync(path))
throw new FileAlreadyExistsException(path); throw new FileAlreadyExistsException(path);
CreateDirectoryIfNotExisting(path); CreateDirectoryIfNotExisting(path);
System.IO.File.Copy(sourcePath, path, overrideIfExists); System.IO.File.Copy(sourcePath, path, options.OverrideIfExists);
var fileInfo = new FileInfo(path); var fileInfo = new FileInfo(path);
var ret = new PhysicalFileInfo(fileInfo); var ret = new PhysicalFileInfo(fileInfo);
return ret; return ret;
} }
public async Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true) public async Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, IWriteFileOptions options)
{ {
if (!overrideIfExists && await FileExistsAsync(path)) if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
if (!options.OverrideIfExists && await FileExistsAsync(path))
throw new FileAlreadyExistsException(path); throw new FileAlreadyExistsException(path);
CreateDirectoryIfNotExisting(path); CreateDirectoryIfNotExisting(path);
@ -118,9 +129,14 @@ namespace PoweredSoft.Storage.Physical
return physicalinfo; return physicalinfo;
} }
public async Task<IFileInfo> WriteFileAsync(Stream stream, string path, bool overrideIfExists = true) public async Task<IFileInfo> WriteFileAsync(Stream stream, string path, IWriteFileOptions options)
{ {
if (!overrideIfExists && await FileExistsAsync(path)) if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
if (!options.OverrideIfExists && await FileExistsAsync(path))
throw new FileAlreadyExistsException(path); throw new FileAlreadyExistsException(path);
CreateDirectoryIfNotExisting(path); CreateDirectoryIfNotExisting(path);
@ -155,5 +171,29 @@ namespace PoweredSoft.Storage.Physical
{ {
return key; return key;
} }
public Task<IFileInfo> WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true)
{
return WriteFileAsync(sourcePath, path, new DefaultWriteOptions
{
OverrideIfExists = overrideIfExists
});
}
public Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true)
{
return WriteFileAsync(bytes, path, new DefaultWriteOptions
{
OverrideIfExists = overrideIfExists
});
}
public Task<IFileInfo> WriteFileAsync(Stream stream, string path, bool overrideIfExists = true)
{
return WriteFileAsync(stream, path, new DefaultWriteOptions
{
OverrideIfExists = overrideIfExists
});
}
} }
} }

View File

@ -0,0 +1,17 @@
using PoweredSoft.Storage.Core;
using System;
using System.Collections.Generic;
using System.Text;
namespace PoweredSoft.Storage.S3
{
public interface IS3FileWriteOptions
{
public string Acl { get; }
}
public class S3FileWriteOptions : DefaultWriteOptions, IS3FileWriteOptions
{
public string Acl { get; set; }
}
}

View File

@ -177,22 +177,25 @@ namespace PoweredSoft.Storage.S3
return files.Cast<IDirectoryOrFile>().ToList(); return files.Cast<IDirectoryOrFile>().ToList();
} }
public async Task<IFileInfo> WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true) public async Task<IFileInfo> WriteFileAsync(string sourcePath, string path, IWriteFileOptions options)
{ {
using var client = GetClient(); using var fileStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
await client.UploadObjectFromFilePathAsync(this.bucketName, path, sourcePath, null); return await WriteFileAsync(fileStream, path, options);
var file = await GetFileInfoByPath(path);
return file;
} }
public Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true) public Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, IWriteFileOptions options)
{ {
return WriteFileAsync(new MemoryStream(bytes), path, overrideIfExists: overrideIfExists); return WriteFileAsync(new MemoryStream(bytes), path, options);
} }
public async Task<IFileInfo> WriteFileAsync(Stream stream, string path, bool overrideIfExists = true) public async Task<IFileInfo> WriteFileAsync(Stream stream, string path, IWriteFileOptions options)
{ {
if (!overrideIfExists && await FileExistsAsync(path)) if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
if (!options.OverrideIfExists && await FileExistsAsync(path))
throw new FileAlreadyExistsException(path); throw new FileAlreadyExistsException(path);
using var client = GetClient(); using var client = GetClient();
@ -203,6 +206,12 @@ namespace PoweredSoft.Storage.S3
Key = path Key = path
}; };
if (options is IS3FileWriteOptions s3FileWriteOptions)
{
if (s3FileWriteOptions.Acl != null)
request.CannedACL = new S3CannedACL(s3FileWriteOptions.Acl);
}
var result = await client.PutObjectAsync(request); var result = await client.PutObjectAsync(request);
var file = await GetFileInfoByPath(path); var file = await GetFileInfoByPath(path);
return file; return file;
@ -246,5 +255,29 @@ namespace PoweredSoft.Storage.S3
var hasMatches = regex.IsMatch(fileName); var hasMatches = regex.IsMatch(fileName);
return false == hasMatches; return false == hasMatches;
} }
public Task<IFileInfo> WriteFileAsync(string sourcePath, string path, bool overrideIfExists = true)
{
return WriteFileAsync(sourcePath, path, new DefaultWriteOptions
{
OverrideIfExists = overrideIfExists
});
}
public Task<IFileInfo> WriteFileAsync(byte[] bytes, string path, bool overrideIfExists = true)
{
return WriteFileAsync(bytes, path, new DefaultWriteOptions
{
OverrideIfExists = overrideIfExists
});
}
public Task<IFileInfo> WriteFileAsync(Stream stream, string path, bool overrideIfExists = true)
{
return WriteFileAsync(stream, path, new DefaultWriteOptions
{
OverrideIfExists = overrideIfExists
});
}
} }
} }

View File

@ -1,11 +1,23 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PoweredSoft.Storage.S3; using PoweredSoft.Storage.S3;
using System.Text;
namespace PoweredSoft.Storage.Test namespace PoweredSoft.Storage.Test
{ {
[TestClass] [TestClass]
public class S3Tests public class S3Tests
{ {
[TestMethod]
public async System.Threading.Tasks.Task S3AclWriteAsync()
{
var space = GetMockS3Space();
await space.WriteFileAsync(Encoding.UTF8.GetBytes("Hello World"), "hello-world.txt", new S3FileWriteOptions
{
Acl = "public-read",
OverrideIfExists = true
});
}
[TestMethod] [TestMethod]
public void NameValidation() public void NameValidation()
{ {
@ -33,9 +45,11 @@ namespace PoweredSoft.Storage.Test
private static S3StorageProvider GetMockS3Space() private static S3StorageProvider GetMockS3Space()
{ {
var space = new S3StorageProvider("http://localhost:9000", "mybucket", "myminio", "myexample"); //var space = new S3StorageProvider("http://localhost:9000", "mybucket", "minioadmin", "minioadmin");
space.SetForcePathStyle(true); //space.SetForcePathStyle(true);
space.SetS3UsEast1RegionalEndpointValue(Amazon.Runtime.S3UsEast1RegionalEndpointValue.Legacy); //space.SetS3UsEast1RegionalEndpointValue(Amazon.Runtime.S3UsEast1RegionalEndpointValue.Legacy);
var space = new S3StorageProvider(" https://nyc3.digitaloceanspaces.com", "lveb-public", "42ZNGWW3EHQECLS7FCPT", "NKhVtWvAdUKRhoFN9QK7rsqnLarVaxpxIAwHJTCPrIA");
return space; return space;
} }
} }