I plan to use S3 bucket for file storage. And plan to use it for Upload, Delete, versioning etc. In order to achieve that this is what I did :
services.AddDefaultAWSOptions(_appConfiguration.GetAWSOptions()); services.AddAWSService<IAmazonS3>(); services.AddSingleton<IS3Services, S3Services>(); services.AddAWSService<IAmazonKeyManagementService>();
I created a folder in XXX.XXXX.Web.Host called Services and created a class called S3Services :
public class S3Services : IS3Services { private readonly IAmazonS3 _S3client; private readonly IAmazonKeyManagementService _KMSclient;
` public S3Services(IAmazonS3 S3client, IAmazonKeyManagementService KMSclient)
{
_S3client = S3client;
_KMSclient = KMSclient;
}
public async Task CreateBucketAsync(string bucketname)
{
try
{
if (await AmazonS3Util.DoesS3BucketExistAsync(_S3client, bucketname) == false)
{
var response = _KMSclient.CreateKeyAsync(new CreateKeyRequest());
string keyId = response.Result.KeyMetadata.KeyId;
var createAliasRequest = new CreateAliasRequest
{
AliasName = "etcplus-TenancyName-Env",
TargetKeyId = keyId
};
CreateAliasResponse x = await _KMSclient.CreateAliasAsync(createAliasRequest);
var putBucketRequest = new PutBucketRequest
{
BucketName = bucketname,
UseClientRegion = true
};
PutBucketResponse putBucketResponse = await _S3client.PutBucketAsync(putBucketRequest);
var getBucketPolicyRequest = new GetBucketPolicyRequest
{
BucketName = bucketname
};
// Get the existing policy from the Bucket
GetBucketPolicyResponse getBucketPolicyResponse = await _S3client.GetBucketPolicyAsync(getBucketPolicyRequest);
//Modify the policy and then put the new policy
string existingPolicy = getBucketPolicyResponse.Policy;
var putBucketPolicyRequest = new PutBucketPolicyRequest
{
BucketName = bucketname,
Policy = existingPolicy
};
PutBucketPolicyResponse putBucketPolicyResponse = await _S3client.PutBucketPolicyAsync(putBucketPolicyRequest);
}
}
catch (AmazonS3Exception ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
}`
Now I want to inject this into the controller class in Web.Core and .Application classes. When I attempt that it's not showing up.
Am I missing any refrence or am I doing the worng way.
The whole purpose is to Inject the AmazonS3 client and handle file operations in the application.
If you can guide me in teh right direction that would be very helpful
7 Answer(s)
-
0
What do you mean by not showing up?
-
0
My bad. Poor choice of words.
I am trying to inject in the DemoComponentUiServices :
[AbpAuthorize(AppPermissions.Pages_DemoUiComponents)] public class DemoUiComponentsAppService : ETCAppServiceBase, IDemoUiComponentsAppService { private readonly IS3Services _iS3Services; I am unable to do so.
-
0
Check the error in Logs.txt.
-
0
It's a namespace issue I am encounterin. So can't even compile:
using System; using System.Collections.Generic; using System.Linq; using Abp; using Abp.Authorization; using CASTANDCREW.ETC.Authorization; using CASTANDCREW.ETC.DemoUiComponents.Dto; using CASTANDCREW.ETC.Web;
namespace CASTANDCREW.ETC.DemoUiComponents { [AbpAuthorize(AppPermissions.Pages_DemoUiComponents)] public class DemoUiComponentsAppService : ETCAppServiceBase, IDemoUiComponentsAppService { private readonly IS3Services _iS3Services;
Below is my code for SService and IS3ervice :
using Amazon.KeyManagementService; using Amazon.KeyManagementService.Model; using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Util; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;
namespace CASTANDCREW.ETC.Web.Services { public class S3Services : IS3Services { private readonly IAmazonS3 _S3client; private readonly IAmazonKeyManagementService _KMSclient;
public S3Services(IAmazonS3 S3client, IAmazonKeyManagementService KMSclient) { _S3client = S3client; _KMSclient = KMSclient; } public async Task CreateBucketAsync(string bucketname) { try { if (await AmazonS3Util.DoesS3BucketExistAsync(_S3client, bucketname) == false) { var response = _KMSclient.CreateKeyAsync(new CreateKeyRequest()); string keyId = response.Result.KeyMetadata.KeyId; var createAliasRequest = new CreateAliasRequest { AliasName = "etcplus-TenancyName-Env", TargetKeyId = keyId }; CreateAliasResponse x = await _KMSclient.CreateAliasAsync(createAliasRequest); var putBucketRequest = new PutBucketRequest { BucketName = bucketname, UseClientRegion = true }; PutBucketResponse putBucketResponse = await _S3client.PutBucketAsync(putBucketRequest); var getBucketPolicyRequest = new GetBucketPolicyRequest { BucketName = bucketname }; // Get the existing policy from the Bucket GetBucketPolicyResponse getBucketPolicyResponse = await _S3client.GetBucketPolicyAsync(getBucketPolicyRequest); //Modify the policy and then put the new policy string existingPolicy = getBucketPolicyResponse.Policy; var putBucketPolicyRequest = new PutBucketPolicyRequest { BucketName = bucketname, Policy = existingPolicy }; PutBucketPolicyResponse putBucketPolicyResponse = await _S3client.PutBucketPolicyAsync(putBucketPolicyRequest); } } catch (AmazonS3Exception ex) { throw ex; } catch (Exception ex) { throw ex; } } }
}
namespace CASTANDCREW.ETC.Web.Services { public interface IS3Services { } }
-
0
using CASTANDCREW.ETC.Web.Services;
-
0
Severity Code Description Project File Line Suppression State Error CS0234 The type or namespace name 'Services' does not exist in the namespace 'CASTANDCREW.ETC.Web' (are you missing an assembly reference?) CASTANDCREW.ETC.Application C:\CASTANDCREW\ETC\aspnet-core\src\CASTANDCREW.ETC.Application\DemoUiComponents\DemoUiComponentsAppService.cs 9 Active
using CASTANDCREW.ETC.Web.Services; this doesn't show up in intellisense as well
-
0
If you want to use it in Application project, define
IS3Services
and a null implementation in Application project.