Skip to content

Commit d9892e4

Browse files
committed
add initial amazon s3 unit tests
1 parent 8a6dde3 commit d9892e4

6 files changed

Lines changed: 339 additions & 30 deletions

File tree

FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Amazon.S3;
88
using Amazon.S3.Model;
99
using SharpGrip.FileSystem.Exceptions;
10+
using SharpGrip.FileSystem.Extensions;
1011
using SharpGrip.FileSystem.Models;
1112
using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException;
1213
using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException;
@@ -41,7 +42,7 @@ public override async Task<IFile> GetFileAsync(string virtualPath, CancellationT
4142
{
4243
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
4344

44-
return ModelFactory.CreateFile(response, virtualPath);
45+
return ModelFactory.CreateFile(response, path, virtualPath);
4546
}
4647
catch (AmazonS3Exception exception)
4748
{
@@ -69,15 +70,12 @@ public override async Task<IDirectory> GetDirectoryAsync(string virtualPath, Can
6970

7071
try
7172
{
72-
var prefix = "";
73-
var pathParts = GetPathParts(path);
74-
75-
if (pathParts.Length > 1)
73+
if (path == "/")
7674
{
77-
prefix = string.Join("/", pathParts.Take(pathParts.Length - 1)) + "/";
75+
return ModelFactory.CreateDirectory(new S3Object {Key = "/"}, virtualPath);
7876
}
7977

80-
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = prefix};
78+
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
8179
var response = await client.ListObjectsV2Async(request, cancellationToken);
8280

8381
if (response.KeyCount == 0)
@@ -106,6 +104,11 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string virtualPath
106104
await GetDirectoryAsync(virtualPath, cancellationToken);
107105
var path = GetPath(virtualPath);
108106

107+
if (!path.EndsWith("/"))
108+
{
109+
path += "/";
110+
}
111+
109112
try
110113
{
111114
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
@@ -145,7 +148,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string v
145148

146149
foreach (var item in response.S3Objects)
147150
{
148-
var itemName = item.Key.Substring(0, item.Key.Length - path.Length);
151+
var itemName = item.Key.Substring(path.Length).RemoveLeadingForwardSlash();
149152

150153
if (item.Key.EndsWith("/") && itemName.Count(c => c.Equals('/')) == 1)
151154
{
@@ -192,9 +195,9 @@ public override async Task DeleteDirectoryAsync(string virtualPath, Cancellation
192195
try
193196
{
194197
var deleteObjectsRequest = new DeleteObjectsRequest {BucketName = bucketName};
195-
var listObjectsRequest = new ListObjectsRequest {BucketName = bucketName, Prefix = path};
198+
var listObjectsRequest = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
196199

197-
var response = await client.ListObjectsAsync(listObjectsRequest, cancellationToken);
200+
var response = await client.ListObjectsV2Async(listObjectsRequest, cancellationToken);
198201

199202
foreach (S3Object entry in response.S3Objects)
200203
{

FileSystem.Adapters.AmazonS3/src/ModelFactory.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
using System;
22
using System.Linq;
33
using Amazon.S3.Model;
4+
using SharpGrip.FileSystem.Extensions;
45
using SharpGrip.FileSystem.Models;
56

67
namespace SharpGrip.FileSystem.Adapters.AmazonS3
78
{
89
public static class ModelFactory
910
{
10-
public static FileModel CreateFile(GetObjectResponse file, string virtualPath)
11+
public static FileModel CreateFile(GetObjectResponse file, string path, string virtualPath)
1112
{
1213
return new FileModel
1314
{
1415
Name = file.Key.Split('/').Last(),
15-
Path = file.Key,
16+
Path = path,
1617
VirtualPath = virtualPath,
1718
Length = file.ContentLength,
1819
LastModifiedDateTime = file.LastModified
@@ -34,7 +35,12 @@ public static FileModel CreateFile(S3Object file, string virtualPath)
3435
public static DirectoryModel CreateDirectory(S3Object directory, string virtualPath)
3536
{
3637
var pathParts = directory.Key.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
37-
var name = pathParts.Last();
38+
var name = pathParts.LastOrDefault();
39+
40+
if (name == null)
41+
{
42+
name = "/";
43+
}
3844

3945
if (pathParts.Length == 1)
4046
{
@@ -43,8 +49,8 @@ public static DirectoryModel CreateDirectory(S3Object directory, string virtualP
4349

4450
return new DirectoryModel
4551
{
46-
Name = name.Substring(0, name.Length - 1),
47-
Path = directory.Key.Substring(0, name.Length - 1),
52+
Name = name,
53+
Path = directory.Key.RemoveTrailingForwardSlash(),
4854
VirtualPath = virtualPath,
4955
LastModifiedDateTime = directory.LastModified
5056
};

FileSystem/src/Utilities/PathUtilities.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public static string GetPrefix(string virtualPath)
2727
/// <returns>The path.</returns>
2828
public static string GetPath(string virtualPath, string rootPath)
2929
{
30+
if (string.IsNullOrWhiteSpace(rootPath))
31+
{
32+
return string.Join(PathSeparator, ResolvePrefixAndPath(virtualPath)[1]);
33+
}
34+
3035
return string.Join(PathSeparator, rootPath, ResolvePrefixAndPath(virtualPath)[1]);
3136
}
3237

0 commit comments

Comments
 (0)