|
7 | 7 | using Amazon.S3; |
8 | 8 | using Amazon.S3.Model; |
9 | 9 | using SharpGrip.FileSystem.Exceptions; |
| 10 | +using SharpGrip.FileSystem.Extensions; |
10 | 11 | using SharpGrip.FileSystem.Models; |
11 | 12 | using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException; |
12 | 13 | using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException; |
@@ -41,7 +42,7 @@ public override async Task<IFile> GetFileAsync(string virtualPath, CancellationT |
41 | 42 | { |
42 | 43 | using var response = await client.GetObjectAsync(bucketName, path, cancellationToken); |
43 | 44 |
|
44 | | - return ModelFactory.CreateFile(response, virtualPath); |
| 45 | + return ModelFactory.CreateFile(response, path, virtualPath); |
45 | 46 | } |
46 | 47 | catch (AmazonS3Exception exception) |
47 | 48 | { |
@@ -69,15 +70,12 @@ public override async Task<IDirectory> GetDirectoryAsync(string virtualPath, Can |
69 | 70 |
|
70 | 71 | try |
71 | 72 | { |
72 | | - var prefix = ""; |
73 | | - var pathParts = GetPathParts(path); |
74 | | - |
75 | | - if (pathParts.Length > 1) |
| 73 | + if (path == "/") |
76 | 74 | { |
77 | | - prefix = string.Join("/", pathParts.Take(pathParts.Length - 1)) + "/"; |
| 75 | + return ModelFactory.CreateDirectory(new S3Object {Key = "/"}, virtualPath); |
78 | 76 | } |
79 | 77 |
|
80 | | - var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = prefix}; |
| 78 | + var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path}; |
81 | 79 | var response = await client.ListObjectsV2Async(request, cancellationToken); |
82 | 80 |
|
83 | 81 | if (response.KeyCount == 0) |
@@ -106,6 +104,11 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string virtualPath |
106 | 104 | await GetDirectoryAsync(virtualPath, cancellationToken); |
107 | 105 | var path = GetPath(virtualPath); |
108 | 106 |
|
| 107 | + if (!path.EndsWith("/")) |
| 108 | + { |
| 109 | + path += "/"; |
| 110 | + } |
| 111 | + |
109 | 112 | try |
110 | 113 | { |
111 | 114 | var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path}; |
@@ -145,7 +148,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string v |
145 | 148 |
|
146 | 149 | foreach (var item in response.S3Objects) |
147 | 150 | { |
148 | | - var itemName = item.Key.Substring(0, item.Key.Length - path.Length); |
| 151 | + var itemName = item.Key.Substring(path.Length).RemoveLeadingForwardSlash(); |
149 | 152 |
|
150 | 153 | if (item.Key.EndsWith("/") && itemName.Count(c => c.Equals('/')) == 1) |
151 | 154 | { |
@@ -192,9 +195,9 @@ public override async Task DeleteDirectoryAsync(string virtualPath, Cancellation |
192 | 195 | try |
193 | 196 | { |
194 | 197 | var deleteObjectsRequest = new DeleteObjectsRequest {BucketName = bucketName}; |
195 | | - var listObjectsRequest = new ListObjectsRequest {BucketName = bucketName, Prefix = path}; |
| 198 | + var listObjectsRequest = new ListObjectsV2Request {BucketName = bucketName, Prefix = path}; |
196 | 199 |
|
197 | | - var response = await client.ListObjectsAsync(listObjectsRequest, cancellationToken); |
| 200 | + var response = await client.ListObjectsV2Async(listObjectsRequest, cancellationToken); |
198 | 201 |
|
199 | 202 | foreach (S3Object entry in response.S3Objects) |
200 | 203 | { |
@@ -252,8 +255,6 @@ public override async Task WriteFileAsync(string virtualPath, Stream contents, b |
252 | 255 |
|
253 | 256 | try |
254 | 257 | { |
255 | | - contents.Seek(0, SeekOrigin.Begin); |
256 | | - |
257 | 258 | var request = new PutObjectRequest |
258 | 259 | { |
259 | 260 | InputStream = contents, |
|
0 commit comments