geckos
geckos

Reputation: 6299

Using AWSSDK.S3 to upload files to IBM COS (S3 compatible) with .net-core

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

Answers (2)

Alessandro R
Alessandro R

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:

  • implement all these features (read, write, delete) manually via REST API of IBM COS service (which should be S3 compliant)
  • try to use AWSSDK.S3 package S3 compliant

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.

  1. In the IBM Cloud platform, create Service credentials including HMAC Credentials. This is an important step which allows you to create credentials with an AccessKeyId and a SecretAccessKey, otherwise you wont see that.
  2. Now in the appsettings.json add a JSON like this
{
   "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

  1. In Startup.cs add the following
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>();
  1. Use it on your classes using the DI. Example:
/// <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:

  • NetCore 3.1.1x
  • AWSSDK.S3 3.7.1.5
  • AWSSDK.Extensions.NETCore.Setup 3.7.0.1

Upvotes: 2

Nick Lange
Nick Lange

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

Related Questions