Skip to content

Commit

Permalink
Merge pull request #11 from lreb/aws-s3
Browse files Browse the repository at this point in the history
Aws s3 - enhancements
  • Loading branch information
lreb authored Nov 4, 2020
2 parents b409ed7 + 2286930 commit 2197c40
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 148 deletions.
13 changes: 8 additions & 5 deletions FacwareBase.API/Controllers/Document/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<ActionResult<AddFileResponse>> AddFiles(string bucketName, str
return BadRequest("The request doesn't contain any files to be uploaded.");
}

var response = await _filesRepository.UploadFiles(bucketName, formFiles, key);
var response = await _filesRepository.UploadFiles(bucketName, key, formFiles[0]);

if (response == null)
{
Expand Down Expand Up @@ -77,13 +77,16 @@ public async Task<ActionResult<IEnumerable<ListFilesResponse>>> ListFiles(string
[Route("{bucketName}/download/{fileName}")]
public async Task<IActionResult> DownloadFile(string bucketName, string fileName)
{
var temporalPath = _fileStorageOptions.TemporalStorage;
//var temporalPath = _fileStorageOptions.TemporalStorage;

await _filesRepository.DownloadFile(bucketName, fileName, temporalPath);
// await _filesRepository.DownloadFile(bucketName, fileName, temporalPath);

var memoryFile = _fileManagement.ReadFileAsync(temporalPath, fileName).Result;
// var memoryFile = _fileManagement.ReadFileAsync(temporalPath, fileName).Result;

_fileManagement.RemoveFile(temporalPath, fileName);
// _fileManagement.RemoveFile(temporalPath, fileName);

var key = "test123/";
var memoryFile = await _filesRepository.DownloadMemoryStreamAsync(bucketName, key + fileName);

var mimeType = _fileManagement.GetMimeType(fileName);

Expand Down
2 changes: 1 addition & 1 deletion FacwareBase.API/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Local"
}
},
"FacwareBase.API": {
Expand Down
21 changes: 21 additions & 0 deletions FacwareBase.API/Services/Amazon/S3/Core/File/AddFileResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@

namespace FacwareBase.API.Services.Amazon.S3.Core.File
{
/// <summary>
/// New file response
/// </summary>
public class AddFileResponse
{
/// <summary>
/// Uri
/// </summary>
public IList<string> PreSignedUrl { get; set; }
}

/// <summary>
/// file data
/// </summary>
public class AddSingleFileResponse
{
/// <summary>
/// url pre signed
/// </summary>
public string PreSignedUrl { get; set; }
/// <summary>
/// s3 key
/// </summary>
public string Key { get; set; }
}
}
10 changes: 6 additions & 4 deletions FacwareBase.API/Services/Amazon/S3/Core/File/AddItemRequest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace FacwareBase.API.Services.Amazon.S3.Core.File
{
public class AddItemRequest
{

}
/// <summary>
/// New item request
/// </summary>
public class AddItemRequest
{
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
namespace FacwareBase.API.Services.Amazon.S3.Core.File
{
/// <summary>
/// Deleted objects summary
/// </summary>
public class DeleteFileResponse
{
/// <summary>
/// delete object number
/// </summary>
public int NumberOfDeletedObjects { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ namespace FacwareBase.API.Services.Amazon.S3.Core.File
{
public class GetJsonObjectResponse
{
/// <summary>
/// Id
/// </summary>
public Guid Id { get; set; }
/// <summary>
/// Last update
/// </summary>
public DateTime TimeSent { get; set; }
/// <summary>
/// Json data
/// </summary>
public string Data { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
using FacwareBase.API.Services.Amazon.S3.Core.File;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace FacwareBase.API.Services.Amazon.S3.Core.Interfaces
{
public interface IFilesRepository
{
{
/// <summary>
/// Add json object
/// </summary>
/// <param name="bucketName">bucket name</param>
/// <param name="request">object request</param>
Task AddJsonObject(string bucketName, AddJsonObjectRequest request);

/// <summary>
/// Upload files
/// Copy a file or key to another location
/// </summary>
/// <param name="bucketName">bucket name</param>
/// <param name="formFiles">files</param>
/// <param name="key">s3 key</param>
/// <returns>AddFileResponse<see cref="AddFileResponse"/></returns>
Task<AddFileResponse> UploadFiles(string bucketName, IList<IFormFile> formFiles, string key = "");
/// <param name="sourceBucket">source bucket name</param>
/// <param name="sourceKey">source key</param>
/// <param name="targetBucket">target bucket name</param>
/// <param name="targetKey">traget key</param>
/// <returns>target key</returns>
Task<string> CopyKey(string sourceBucket, string sourceKey, string targetBucket, string targetKey);

/// <summary>
/// List all key/files in a s3 bucket
/// Delete file from s3
/// </summary>
/// <param name="bucketName">bucket name</param>
/// <returns>ListFilesResponse<see cref="ListFilesResponse"/></returns>
Task<IEnumerable<ListFilesResponse>> ListFiles(string bucketName);
/// <param name="key">s3 key /key</param>
/// <returns>DeleteFileResponse<see cref="DeleteFileResponse"/></returns>
Task<DeleteFileResponse> DeleteFile(string bucketName, string key);

/// <summary>
/// Download file from s3 to local temp folder
/// </summary>
/// <param name="bucketName">bucket name</param>
/// <param name="fileName">file name</param>
/// <param name="temporalPath">temporal path to download</param>
/// <param name="key">s3 key: path</param>
/// <param name="key">key = path + file name</param>
/// <param name="temporalPath">temporal path to download + file name</param>
/// <returns>task just to download file</returns>
Task DownloadFile(string bucketName, string fileName, string temporalPath, string key = "");
Task DownloadFile(string bucketName, string key, string temporalPath);

/// <summary>
/// Delete file from s3
/// </summary>
/// <param name="bucketName">bucket name</param>
/// <param name="fileName">file name /key</param>
/// <returns>DeleteFileResponse<see cref="DeleteFileResponse"/></returns>
Task<DeleteFileResponse> DeleteFile(string bucketName, string fileName);

/// <summary>
/// Add json object
/// Get file from s3 bucket
/// </summary>
/// <param name="bucketName">bucket name</param>
/// <param name="request">object request</param>
Task AddJsonObject(string bucketName, AddJsonObjectRequest request);
/// <param name="key">s3 key - path + file name</param>
/// <returns><see cref="MemoryStream"/>Memory file object</returns>
Task<MemoryStream> DownloadMemoryStreamAsync(string bucketName, string key);

/// <summary>
/// Get json object
Expand All @@ -55,5 +57,21 @@ public interface IFilesRepository
/// <param name="fileName">file name</param>
/// <returns>GetJsonObjectResponse<see cref="GetJsonObjectResponse"/></returns>
Task<GetJsonObjectResponse> GetJsonObject(string bucketName, string fileName);

/// <summary>
/// List all key/files in a s3 bucket
/// </summary>
/// <param name="bucketName">bucket name</param>
/// <returns>ListFilesResponse<see cref="ListFilesResponse"/></returns>
Task<IEnumerable<ListFilesResponse>> ListFiles(string bucketName);

/// <summary>
/// Upload files
/// </summary>
/// <param name="bucketName">bucket name</param>
/// <param name="formFiles">files</param>
/// <param name="key">s3 key</param>
/// <returns>AddFileResponse<see cref="AddFileResponse"/></returns>
Task<AddFileResponse> UploadFiles(string bucketName, string key, params IFormFile[] formFiles);
}
}
Loading

0 comments on commit 2197c40

Please sign in to comment.