Skip to main content

Using AWS S3 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. Storage API is inter-operable with the AWS S3 API, meaning you can use S3 tools and libraries with Storage. A common use case is managing Storage programmatically using AWS' S3 SDKs.

Setup and Configuration#

Install the SDK#

Install the AWS SDK using tha package manager for your language choice.

C#

Install-Package AWSSDK.S3

Create Access Keys#

To use the AWS SDK you need an Access Key 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 S3 API, you must configure the “endpoint” setting to point to our Storage. The value should be blob.gjirafa.tech.

C#

var amazonS3Config = new AmazonS3Config{    ServiceURL = "https://blob.gjirafa.tech",    DisableHostPrefixInjection = true,    ForcePathStyle = true};
var client = new AmazonS3Client("AccessKey", "SecretKey", amazonS3Config);

Usage Examples#

Create a new Bucket#

These examples create a new Bucket on Storage.

Bucket name must be globally unique. If you attempt to create a Bucket with name that is already in use will fail with BucketAlreadyExists error.

C#

var putBucketRequest = new PutBucketRequest{    BucketName = "uniqueBucketName",};
var putBucketResponse = await client.PutBucketAsync(putBucketRequest);

Upload a file to a Bucket#

If you are using Storage API to upload a file into a bucket, you must specify that the file is not a folder. If you are trying to upload a file we generate you a presigned URL and you have to make a PUT request in that URL where you must add Content-Type, x-amz-acl (where it should be private or public-read),Access-Control-Allow-Origin headers.

C#

try{    var presignedURL = $"{presignedURL}";    HttpWebRequest httpRequest = WebRequest.Create(presignedURL) as HttpWebRequest;    httpRequest.Method = "PUT";
    httpRequest.Headers.Add($"x-amz-acl:{private or read}");    httpRequest.ContentType = $"{file content type}";
    using (Stream dataStream = httpRequest.GetRequestStream())    {        var filePath = $"{file path on your computer}";        var buffer = new byte[8000];        using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))        {            int bytesRead = 0;            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)            {                dataStream.Write(buffer, 0, bytesRead);            }        }    }    HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;}catch (Exception e){    Console.WriteLine(e.Message);}

If you are want to upload file using AWS S3 SDK:

C#

try{    var putObjectRequest = new PutObjectRequest    {        BucketName = "bucket name",        Key = "key",        FilePath = "path of file",        ContentType = "content type of file",        CannedACL = S3CannedACL.PublicRead // acl of object    };
    putObjectRequest.Metadata.Add("x-amz-meta-metadata", "value of metadata"); //user defined metadata shoudl start with x-amz-meta-value 
    var response2 = await client.PutObjectAsync(putObjectRequest);}catch (AmazonS3Exception e){    Console.WriteLine(e.Message);}catch (Exception e){    Console.WriteLine(e.Message);}

List all objects of a bucket#

These examples lists all bucket objects.

C#

try{    var request = new ListObjectsV2Request    {        BucketName = bucketName,    };        var response = await _client.ListObjectsV2Async(request);}catch(Exception e){    Console.WriteLine(e.Message);}

Generate a Pre-Signed URL to Download a Private File or Share a private file#

These examples generate presigned url.

C#

try{    var request = new GetPreSignedUrlRequest    {        BucketName = "bucketName",        Key = "object key",        Expires = 5, //duration of link        Verb = HttpVerb.GET, // GET,HEAD,PUT,DELETE        Protocol = Protocol.HTTPS, // HTTP or HTTPS protocol    };        var url = client.GetPreSignedURL(request);}catch(Exception e){    Console.WriteLine(e.Message);}

Delete a File from a Bucket#

These examples to delete object.

C#

try{    var request = new DeleteObjectRequest    {        BucketName = "bucket name",        Key = "object key",    };
    var result = await client.DeleteObjectAsync(request);}catch(Exception e){    Console.WriteLine(e.Message);}

Delete a Bucket from Storage#

These examples to delete bucket from Storage.

To delte object with all objects we use AmazonS3Util.

C#

try{    await AmazonS3Util.DeleteS3BucketWithObjectsAsync(client, "bucket name");}catch(Exception e){    Console.WriteLine(e.Message);}