Articles → AWS SDK AND CSHARP → Upload File(S) And Folder(S) On S3 Bucket Using C# Code

Upload File(S) And Folder(S) On S3 Bucket Using C# Code






Steps




  1. Create s3 bucket.
  2. Create a console application and write code.
  3. Output.



Create S3 Bucket




Picture showing the S3 bucket created in AWS console

Click to Enlarge


Create A Console Application And Write Code




using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Transfer;
using System.IO;

namespace dotnetS3
{
    class Program
    {

        public static void Main(string[] args)
        {

            string bucketName = "gyansangrah-bucket";

            UploadFilesAndFolder(bucketName);
        }

        static void UploadFilesAndFolder(string bucketName)
        {
            var region = RegionEndpoint.GetBySystemName("us-east-2");
            AWSCredentials credentails = new BasicAWSCredentials("<Access Id>", "<Secret Key>");

            AmazonS3Client client = new AmazonS3Client(credentails);

            TransferUtility utility = new TransferUtility(client);
            TransferUtilityUploadDirectoryRequest transferUtilityUploadDirectoryRequest
                = new TransferUtilityUploadDirectoryRequest()
                {
                    BucketName = bucketName,
                    Directory = @"C:\temp\cluster",
                    SearchOption = SearchOption.AllDirectories
                };

            utility.UploadDirectory(transferUtilityUploadDirectoryRequest);
        }

       

    }
}




Picture showing the list of files and folders to be uploaded on S3 bucket

Click to Enlarge


Output




Picture showing the files and folders uploaded on the S3 bucket

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, February 10, 2022

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250