Uploading file AWS S3 using Low-level API

Amazon AWS offers two types of API’s for uploading a file to Amazon Simple Storage Service (S3). Low-Level API and High-Level API. Both the API’s provide various options to easily upload a file to S3 storage. These API’s also take care of Timeout issues while uploading large files. The steps in uploading a file to amazon s3 might differ in both the API’s. In this article I will show on how to upload a file to amazon AWS S3 using Low-level API class. As this procedure is implemented using Dot Net, make sure your project is integrated with AWS SDK for Dot Net.

Uploading process to be followed while using low-level API:

Initially, we need to create an instance of Amazon S3 client class by providing the AWS Credentials. Here we use AmazonS3Client class.
For Eg:

IAmazonS3 s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); //Enter Your RegionEnd point

In the next step, initiate the multipart upload request. Syntax goes as follow:

InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest
    {
        BucketName = existingBucketName,
        Key = keyName
    };

InitiateMultipartUploadResponse initResponse = 
    s3Client.InitiateMultipartUpload(initRequest);

Here, BucketName is any bucket available in your S3 storage and key name represents the Folder name. The multipartupload method returns the upload ID. InitResponse object stores the Upload ID.
Next, upload parts and save the request. It continues the process until all parts are uploaded and finally call AmazonS3Client.CompleteMultipartUpload method to complete the multipart upload.

Now Let’s collate all the steps to an example

using System;
using System.Collections.Generic;
using System.IO;
using Amazon.S3;
using Amazon.S3.Model;

namespace aws.example
{
    class uploadfiletoS3
    {
        static string existingBucketName = "XYZ";
        static string keyName            = "abc";
        static string filePath           = "*** file path ***";//Give filepath here using server.mappath.

        static void Main(string[] args)
        {
            IAmazonS3 s3Client = new AmazonS3Client(Enter region end point here);

            // List to store upload part responses.
            List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();

            // 1. Initializing the multipartupload.
            InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest
                {
                    BucketName = xyz,    //bucketname
                    Key = abc  //folder name
                };

            InitiateMultipartUploadResponse initResponse =
                s3Client.InitiateMultipartUpload(initiateRequest);

            // 2. Upload Parts using multipartupload request.
            long contentLength = new FileInfo(filePath).Length;
            long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB

            try
            {
                long filePosition = 0;
                for (int i = 1; filePosition < contentLength; i++)
                {
                    UploadPartRequest uploadRequest = new UploadPartRequest
                        {
                            BucketName = xyz,
                            Key = abc,
                            UploadId = initResponse.UploadId,
                            PartNumber = i,
                            PartSize = partSize,
                            FilePosition = filePosition,
                            FilePath = filePath
                        };

                    // Uploading parts and adding responses to list.
                    uploadResponses.Add(s3Client.UploadPart(uploadRequest));

                    filePosition += partSize;
                }

                // Step 3: Ending the multipartupload.
                CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest
                    {
                        BucketName = xyz,
                        Key = abc,
                        UploadId = initResponse.UploadId,
                        //PartETags = new List<PartETag>(uploadResponses)

                    };
                completeRequest.AddPartETags(uploadResponses);

                CompleteMultipartUploadResponse completeUploadResponse =
                    s3Client.CompleteMultipartUpload(completeRequest);

            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception occurred: {0}", exception.Message);
                AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest
                {
                    BucketName = xyz,
                    Key = abc,
                    UploadId = initResponse.UploadId
                };
                s3Client.AbortMultipartUpload(abortMPURequest);
            }
        }
    }
}

Make sure, you have key in the AWS credentials already to the SDK to get this syntax work. In the next Article I’ll show you how to upload file to AWS s3 using High-level API.

Thank You.