|
1 |
| -using Amazon.S3; |
2 |
| -using Amazon.S3.Model; |
3 |
| -using AmazonS3_Backend.Providers; |
4 |
| -using Microsoft.AspNetCore.Mvc; |
5 |
| - |
6 |
| -namespace AmazonS3Backend.Controllers { |
7 |
| - [Route("api/[controller]")] |
8 |
| - [ApiController] |
9 |
| - public class AmazonS3Controller : ControllerBase { |
10 |
| - AmazonS3Provider provider; |
11 |
| - public AmazonS3Controller(IAmazonS3 client) { |
12 |
| - provider = new AmazonS3Provider(client, Program.BucketName); |
13 |
| - } |
14 |
| - |
15 |
| - [HttpGet("getItems")] |
16 |
| - public async Task<IActionResult> GetItems(string? path) { |
17 |
| - try { |
18 |
| - var items = await provider.GetItemsAsync(path); |
19 |
| - return Ok(items); |
20 |
| - } catch (Exception ex) { |
21 |
| - throw new Exception(ex.Message); |
22 |
| - } |
23 |
| - } |
24 |
| - |
25 |
| - [HttpPut("createDirectory")] |
26 |
| - public async Task<IActionResult> CreateDirectory(string? path, string name) { |
27 |
| - try { |
28 |
| - await provider.CreateDirectoryAsync(path, name); |
29 |
| - return Ok(); |
30 |
| - } catch (AmazonS3Exception ex) { |
31 |
| - return StatusCode(500, ex.Message); |
32 |
| - } |
33 |
| - } |
34 |
| - [HttpPut("renameItem")] |
35 |
| - public async Task<IActionResult> RenameItem(string key, string? directory, string newName) { |
36 |
| - try { |
37 |
| - await provider.RenameItemAsync(key, directory, newName); |
38 |
| - return Ok(); |
39 |
| - } catch (Exception ex) { |
40 |
| - return StatusCode(500, ex.Message); |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - [HttpPost("moveItem")] |
45 |
| - public async Task<IActionResult> MoveItem(string sourceKey, string destinationKey) { |
46 |
| - try { |
47 |
| - await provider.MoveItemAsync(sourceKey, destinationKey); |
48 |
| - return Ok(); |
49 |
| - } catch (Exception ex) { |
50 |
| - return StatusCode(500, ex.Message); |
51 |
| - } |
52 |
| - } |
53 |
| - |
54 |
| - [HttpPost("deleteItem")] |
55 |
| - public async Task<IActionResult> DeleteItem(string item) { |
56 |
| - try { |
57 |
| - await provider.DeleteItemAsync(item); |
58 |
| - return Ok(); |
59 |
| - } catch (Exception ex) { |
60 |
| - return StatusCode(500, ex.Message); |
61 |
| - } |
62 |
| - } |
63 |
| - [HttpPut("copyItem")] |
64 |
| - public async Task<IActionResult> CopyItem(string sourceKey, string destinationKey) { |
65 |
| - try { |
66 |
| - await provider.CopyItemAsync(sourceKey, destinationKey); |
67 |
| - return Ok(); |
68 |
| - } catch (Exception ex) { |
69 |
| - return StatusCode(500, ex.Message); |
70 |
| - } |
71 |
| - } |
72 |
| - [HttpPost("downloadItems")] |
73 |
| - public async Task<IActionResult> DownloadItems([FromBody] string[] keys) { |
74 |
| - try { |
75 |
| - var response = await provider.DownloadItemsAsync(keys); |
76 |
| - // returning file with Content-Disposition header exposed |
77 |
| - return response; |
78 |
| - } catch (Exception ex) { |
79 |
| - return StatusCode(500, ex.Message); |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - [HttpPost("initUpload")] |
84 |
| - public async Task<IActionResult> InitUpload(string key) { |
85 |
| - try { |
86 |
| - var uploadId = await provider.InitUploadAsync(key); |
87 |
| - return Ok(uploadId); |
88 |
| - } catch (Exception ex) { |
89 |
| - return StatusCode(500, ex.Message); |
90 |
| - } |
91 |
| - } |
92 |
| - |
93 |
| - [HttpPost("getPresignedUrl")] |
94 |
| - public async Task<IActionResult> GetPresignedUrl(string uploadId, string key, int partNumber) { |
95 |
| - try { |
96 |
| - var presignedUrl = await provider.GetPresignedUrlAsync(uploadId, key, partNumber); |
97 |
| - return Ok(presignedUrl); |
98 |
| - } catch (Exception ex) { |
99 |
| - return StatusCode(500, ex.Message); |
100 |
| - } |
101 |
| - } |
102 |
| - |
103 |
| - [HttpPost("getPresignedDownloadUrl")] |
104 |
| - public async Task<IActionResult> GetPresignedDownloadUrl(string key) { |
105 |
| - try { |
106 |
| - var presignedDownloadUrl = await provider.GetPresignedDownloadUrlAsync(key); |
107 |
| - return Ok(presignedDownloadUrl); |
108 |
| - } catch (Exception ex) { |
109 |
| - return StatusCode(500, ex.Message); |
110 |
| - } |
111 |
| - } |
112 |
| - |
113 |
| - [HttpPost("uploadPart")] |
114 |
| - public async Task<IActionResult> UploadPart([FromForm] IFormFile part, [FromForm] int partNumber, [FromForm] long partSize, [FromForm] string fileName, [FromForm] string uploadId) { |
115 |
| - try { |
116 |
| - using (var filePart = part.OpenReadStream()) { |
117 |
| - var response = await provider.UploadPartAsync(filePart, partNumber, partSize, fileName, uploadId); |
118 |
| - // returning result with ETag header exposed |
119 |
| - return Ok(response.ETag); |
120 |
| - } |
121 |
| - } catch (Exception ex) { |
122 |
| - return StatusCode(500, ex.Message); |
123 |
| - } |
124 |
| - } |
125 |
| - |
126 |
| - [HttpPost("completeUpload")] |
127 |
| - public async Task<IActionResult> CompleteUpload([FromBody] List<PartETag> parts, string key, string uploadId) { |
128 |
| - try { |
129 |
| - var response = await provider.CompleteUploadAsync(key, uploadId, parts); |
130 |
| - // use `response` if you need to pass ETag or something else when upload is finished |
131 |
| - return Ok(response.ETag); |
132 |
| - } catch (AmazonS3Exception ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { |
133 |
| - // abort is requested when upload is being completed |
134 |
| - return Ok(); |
135 |
| - } catch (Exception ex) { |
136 |
| - return StatusCode(500, ex.Message); |
137 |
| - } |
138 |
| - } |
139 |
| - |
140 |
| - [HttpPost("abortUpload")] |
141 |
| - public async Task<IActionResult> AbortUpload(string uploadId, string key) { |
142 |
| - try { |
143 |
| - var response = await provider.AbortUploadAsync(uploadId, key); |
144 |
| - // use `response` if you need to pass something to the client after aborting upload |
145 |
| - return Ok(response.HttpStatusCode); |
146 |
| - } catch (Exception ex) { |
147 |
| - return StatusCode(500, ex.Message); |
148 |
| - } |
149 |
| - } |
150 |
| - } |
151 |
| -} |
| 1 | +using Amazon.S3; |
| 2 | +using Amazon.S3.Model; |
| 3 | +using AmazonS3_Backend.Providers; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | + |
| 6 | +namespace AmazonS3Backend.Controllers { |
| 7 | + [Route("api/[controller]")] |
| 8 | + [ApiController] |
| 9 | + public class AmazonS3Controller : ControllerBase { |
| 10 | + AmazonS3Provider provider; |
| 11 | + public AmazonS3Controller(IAmazonS3 client) { |
| 12 | + provider = new AmazonS3Provider(client, Program.BucketName); |
| 13 | + } |
| 14 | + |
| 15 | + [HttpGet("getItems")] |
| 16 | + public async Task<IActionResult> GetItems(string? path) { |
| 17 | + try { |
| 18 | + var items = await provider.GetItemsAsync(path); |
| 19 | + return Ok(items); |
| 20 | + } catch (Exception ex) { |
| 21 | + throw new Exception(ex.Message); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + [HttpPut("createDirectory")] |
| 26 | + public async Task<IActionResult> CreateDirectory(string? path, string name) { |
| 27 | + try { |
| 28 | + await provider.CreateDirectoryAsync(path, name); |
| 29 | + return Ok(); |
| 30 | + } catch (AmazonS3Exception ex) { |
| 31 | + return StatusCode(500, ex.Message); |
| 32 | + } |
| 33 | + } |
| 34 | + [HttpPut("renameItem")] |
| 35 | + public async Task<IActionResult> RenameItem(string key, string? directory, string newName) { |
| 36 | + try { |
| 37 | + await provider.RenameItemAsync(key, directory, newName); |
| 38 | + return Ok(); |
| 39 | + } catch (Exception ex) { |
| 40 | + return StatusCode(500, ex.Message); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + [HttpPost("moveItem")] |
| 45 | + public async Task<IActionResult> MoveItem(string sourceKey, string destinationKey) { |
| 46 | + try { |
| 47 | + await provider.MoveItemAsync(sourceKey, destinationKey); |
| 48 | + return Ok(); |
| 49 | + } catch (Exception ex) { |
| 50 | + return StatusCode(500, ex.Message); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [HttpPost("deleteItem")] |
| 55 | + public async Task<IActionResult> DeleteItem(string item) { |
| 56 | + try { |
| 57 | + await provider.DeleteItemAsync(item); |
| 58 | + return Ok(); |
| 59 | + } catch (Exception ex) { |
| 60 | + return StatusCode(500, ex.Message); |
| 61 | + } |
| 62 | + } |
| 63 | + [HttpPut("copyItem")] |
| 64 | + public async Task<IActionResult> CopyItem(string sourceKey, string destinationKey) { |
| 65 | + try { |
| 66 | + await provider.CopyItemAsync(sourceKey, destinationKey); |
| 67 | + return Ok(); |
| 68 | + } catch (Exception ex) { |
| 69 | + return StatusCode(500, ex.Message); |
| 70 | + } |
| 71 | + } |
| 72 | + [HttpPost("downloadItems")] |
| 73 | + public async Task<IActionResult> DownloadItems([FromBody] string[] keys) { |
| 74 | + try { |
| 75 | + var response = await provider.DownloadItemsAsync(keys); |
| 76 | + // returning file with Content-Disposition header exposed |
| 77 | + return response; |
| 78 | + } catch (Exception ex) { |
| 79 | + return StatusCode(500, ex.Message); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + [HttpPost("initUpload")] |
| 84 | + public async Task<IActionResult> InitUpload(string key) { |
| 85 | + try { |
| 86 | + var uploadId = await provider.InitUploadAsync(key); |
| 87 | + return Ok(uploadId); |
| 88 | + } catch (Exception ex) { |
| 89 | + return StatusCode(500, ex.Message); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + [HttpPost("getPresignedUrl")] |
| 94 | + public async Task<IActionResult> GetPresignedUrl(string uploadId, string key, int partNumber) { |
| 95 | + try { |
| 96 | + var presignedUrl = await provider.GetPresignedUrlAsync(uploadId, key, partNumber); |
| 97 | + return Ok(presignedUrl); |
| 98 | + } catch (Exception ex) { |
| 99 | + return StatusCode(500, ex.Message); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [HttpPost("getPresignedDownloadUrl")] |
| 104 | + public async Task<IActionResult> GetPresignedDownloadUrl(string key) { |
| 105 | + try { |
| 106 | + var presignedDownloadUrl = await provider.GetPresignedDownloadUrlAsync(key); |
| 107 | + return Ok(presignedDownloadUrl); |
| 108 | + } catch (Exception ex) { |
| 109 | + return StatusCode(500, ex.Message); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + [HttpPost("uploadPart")] |
| 114 | + public async Task<IActionResult> UploadPart([FromForm] IFormFile part, [FromForm] int partNumber, [FromForm] long partSize, [FromForm] string fileName, [FromForm] string uploadId) { |
| 115 | + try { |
| 116 | + using (var filePart = part.OpenReadStream()) { |
| 117 | + var response = await provider.UploadPartAsync(filePart, partNumber, partSize, fileName, uploadId); |
| 118 | + // returning result with ETag header exposed |
| 119 | + return Ok(response.ETag); |
| 120 | + } |
| 121 | + } catch (Exception ex) { |
| 122 | + return StatusCode(500, ex.Message); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + [HttpPost("completeUpload")] |
| 127 | + public async Task<IActionResult> CompleteUpload([FromBody] List<PartETag> parts, string key, string uploadId) { |
| 128 | + try { |
| 129 | + var response = await provider.CompleteUploadAsync(key, uploadId, parts); |
| 130 | + // use `response` if you need to pass ETag or something else when upload is finished |
| 131 | + return Ok(response.ETag); |
| 132 | + } catch (AmazonS3Exception ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { |
| 133 | + // abort is requested when upload is being completed |
| 134 | + return Ok(); |
| 135 | + } catch (Exception ex) { |
| 136 | + return StatusCode(500, ex.Message); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + [HttpPost("abortUpload")] |
| 141 | + public async Task<IActionResult> AbortUpload(string uploadId, string key) { |
| 142 | + try { |
| 143 | + var response = await provider.AbortUploadAsync(uploadId, key); |
| 144 | + // use `response` if you need to pass something to the client after aborting upload |
| 145 | + return Ok(response.HttpStatusCode); |
| 146 | + } catch (Exception ex) { |
| 147 | + return StatusCode(500, ex.Message); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments