Reputation: 6299
I have some code using AWSSDK.S3 to upload data to S3, no mystery.
Since IBM claims it's Cloud Object Storage to be S3 compatible, it would be possible to use AWSSDK.S3 to upload files to IBM COS by only changing the ServiceURL at appsettings.json?
Does anybody did that before?
Upvotes: 2
Views: 1210
Reputation: 645
I was able to use the AWSSDK.S3 on a Net Core 3.1.17 backend. My aim was to use the IBM COS (Cloud Object Storage) service: read, write and delete files from it. The usage of AWSDK.S3 is due to the fact that right now there is no nuget package from IBM or from others which can help us (developers) on this, so there are 2 ways:
Thanks to the previous answer I did a little bit of research and refinement and these are the steps in order to make it working even with the Dependency Injection of Microsoft.
{
"CosLogs":{
"ServiceURL":"https://s3.eu-de.cloud-object-storage.appdomain.cloud",
"AccessKeyId":"youaccessKeyIdTakenFromCredentialServiceDetail",
"SecretAccessKey":"yourSecreatAccessKeyTakenFromCredentialServiceDetail"
}
}
Keep in mind that the ServiceUrl can be retrieved from IBMCLoud endpoint documentation and it depends on the region/regions where you decided to locate the resource. In my case since Im using EU Germany my serviceUrl is: s3.eu-de.cloud-object-storage.appdomain.cloud
var awsOptions = configuration.GetAWSOptions("CosLogs");
var accessKeyId = configuration.GetValue<string>("CosLogs:AccessKeyId");
var secretAccessKey = configuration.GetValue<string>("CosLogs:SecretAccessKey");
awsOptions.Credentials = new BasicAWSCredentials(accessKeyId,secretAccessKey);
services.AddDefaultAWSOptions(awsOptions);
services.AddAWSService<IAmazonS3>();
/// <summary>
/// The S3 Client (COS is S3 compatible)
// </summary>
private readonly IAmazonS3 s3Client;
public CosService(IAmazonS3 s3Client, ILogger<CosService> logger)
{
this.s3Client = s3Client;
this.logger = logger;
}
public async Task DoCosCallAsync(CancellationToken cancellationToken){
var bucketList= await s3Client.ListBucketsAsync(cancellationToken);
}
Relevant packages installed:
Upvotes: 2
Reputation: 867
I'm not sure about appsettings.json
but yes, if you set the ServiceURL
to the config used to create a client it should work transparently. Obviously any AWS features that aren't supported by COS wouldn't work, and any COS extensions (like API key auth, or Key Protect, etc) wouldn't be available.
Something like:
AmazonS3Config S3Config = new AmazonS3Config {ServiceURL = "https://s3.us.cloud-object-storage.appdomain.cloud"};
string accessKeyId = "<accesskey>";
string secretAccessKey = "<secretkey>";
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
AmazonS3Client client = new AmazonS3Client(credentials, S3Config);
Upvotes: 1