Skip to content

Getting Started with Hubtel AWS S3 SDK

This SDK helps you to interact with AWS S3. Helpful methods include:

  • Upload file to aws s3
  • Delete file from aws s3
  • Get metadata about a file
  • Get file from aws s3
  • Get profile image
  • Check if object exists

In order to get started with Hubtel.AwsS3.Sdk, do the following activities

Install-Package Hubtel.AwsS3.Sdk

The provided code snippet is a command that is used to install Hubtel.AwsS3.Sdk package in a .NET project.

C#
    Install-Package Hubtel.AwsS3.Sdk

Add to appsettings.json

The S3ServiceConfig object contains several properties that are used to configure the Amazon S3 integration:

  • AccessKey: This is the access key used for authentication with Amazon S3. You would replace _sample_key_here with your actual access key.

  • Secret: This is the secret key used for authentication with Amazon S3. You would replace _secret_key_here with your actual secret key.

  • Url: This is the URL of the Amazon S3 endpoint. In this case, it's set to https://s3.eu-west-1.amazonaws.com, which is the endpoint for the EU (Ireland) region.

  • BaseUrl: This is the base URL of the CloudFront distribution associated with the S3 bucket. You would replace _cloudfront_base_url with your actual CloudFront base URL.

  • BucketName: This is the name of the S3 bucket where files will be stored. You would replace _bucket_name with your actual bucket name.

  • FolderName: This is the name of the folder within the S3 bucket where files will be stored. You would replace _folder_name with your actual folder name.

  • HasResizeFolders: This is a boolean value that indicates whether images will be resized before upload. If you will be resizing your images, set this to true; otherwise, set it to false.

  • ImageSizes: This is an array of strings that specify the sizes to which images will be resized. Each string should be in the format "widthxheight". This property can be omitted if HasResizeFolders is false.

These values are typically sensitive and should not be hard-coded into your application's source code. Instead, they should be stored securely and retrieved from the configuration at runtime.

c#
"S3ServiceConfig": {
    "AccessKey": "_sample_key_here",
    "Secret": "_secret_key_here",
    "Url": "https://s3.eu-west-1.amazonaws.com",
    "BaseUrl": "_cloudfront_base_url",
    "BucketName": "_bucket_name",
    "FolderName": "_folder_name",
    "HasResizeFolders": false, // Set to true if you will be resizing your images before upload
    "ImageSizes": ["300x300", "1024x1024"] // Can be ommited when there is no resizing
}

Add to IServiceCollection in Startup.cs

  • The AddHubtelAwsS3Sdk method takes a lambda expression as an argument, which is used to configure the options for the HubtelAwsS3Sdk service. The config parameter in the lambda expression is an object that has properties corresponding to the configuration options for the service.

  • Before this, the S3ServiceConfig section of the application's configuration is retrieved and bound to an instance of the S3ServiceConfig class. This is done using the Configuration.GetSection("S3ServiceConfig").Get<S3ServiceConfig>() line. The S3ServiceConfig object contains the configuration options for the HubtelAwsS3Sdk service.

  • Each property of the config object is then set to a corresponding property from the s3ServiceConfig object as indicated by the code snippet below.

c#
var s3ServiceConfig = Configuration.GetSection("S3ServiceConfig").Get<S3ServiceConfig>();
services.AddHubtelAwsS3Sdk(config =>
{
    config.AccessKey = s3ServiceConfig.AccessKey;
    config.Secret = s3ServiceConfig.Secret;
    config.Url = s3ServiceConfig.Url;
    config.BaseUrl = s3ServiceConfig.BaseUrl;
    config.BucketName = s3ServiceConfig.BucketName;
    config.FolderName = s3ServiceConfig.FolderName;
    config.HasResizeFolders = s3ServiceConfig.HasResizeFolders;
    config.ImageSizes = s3ServiceConfig.ImageSizes;
});

Inject and consume service methods

c#
private readonly IS3Api _s3Api;

# Upload file to AWS S3
var response = await _s3Api.UploadFormBase64Async(base64String, folderName, filename);
NB: filename can be ommitted if you need an autogenerated filename

# Delete file from AWS S3
var response = await _s3Api.DeleteObject(folderName, fileName);

# Get metadata about a file
var response = await _s3Api.GetFileObjectMetadata(fileName, folderName);

# Get file from AWS S3
var response = await _s3Api.GetFileObject(fileName, folderName);

# Get profile image
var response = await _s3Api.GetProfileImageAsync(imageUrl, folderName);

# Check if object exists
var response = await _s3Api.ObjectExists(folderName, fileName);

Was this page helpful?

Happy React is loading...