Using Swift SDK
Storage is an S3-compatible object storage service that lets you store and serve large amount of data. Each Storage is a bucket for you to store and serve file. In GjirafaTech Captain Storage we use Swift API for listing Buckets.
Setup and Configuration#
Install the Swift SDK#
Install the Swift SDK using tha package manager for your language choice.
C#
Install-Package SwiftClient.AspNetCoreCreate Keys#
To use the AWS SDK you need a Username and a Secret Key which you can generate them in Storage API or using Storage UI.
To generate keys check Here.
Configure a Client#
To use Storage with tools or libraries designed for the Swift SDK, you must configure the “endpoint” setting to point to our Storage. The value should be https://blob.gjirafa.tech.
C#
var swiftClient = new Client() .WithCredentials(new SwiftCredentials { Username = "username", Password = "secretkey", Endpoints = new List<string> { "https://blob.gjirafa.tech" } //endpoint }) .SetRetryCount(6) .SetRetryPerEndpointCount(2);Usage Examples#
List all buckets#
These examples list all buckets of an account.
NOTE: In swfit sdk Bucket is same as a Container.
C#
var account = await swiftClient.GetAccount(); //gets the list of buckets (containers)List all objects of a bucket#
These examples lists all bucket objects.
C#
try{ var bucketName = "put your bucket name here";
var response = await swiftClient.GetContainer(bucketName);}catch(Exception e){ Console.WriteLine(e.Message);}Upload a file to a Bucket#
These example uploads a file to a bucket.
C#
try{ var bucketName = "put bucket name here"; var filePath = "put file path here";
var buffer = new byte[80000];
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { using (var memoryStream = new MemoryStream(buffer)) { int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) { memoryStream.Write(buffer, 0, bytesRead); } await fileStream.CopyToAsync(memoryStream);
var response = await swiftClient.PutObject(bucketName, "put file key here", memoryStream); } }}catch (Exception e){ Console.WriteLine(e.Message);}Delete a File from a Bucket#
These examples to delete object.
C#
try{ var bucketName = "put bucket name here"; var objectKey = "put bucket key here";
var response = await swiftClient.DeleteObject(bucketName, objectKey);}catch(Exception e){ Console.WriteLine(e.Message);}Delete a Bucket from Storage#
These examples to delete bucket from Storage.
C#
try{ var bucketName = "put your bucket name here";
var response = await swiftClient.DeleteContainer(bucketName);}catch(Exception e){ Console.WriteLine(e.Message);}