Uploading File to DO Spaces using AWS SDK .NET/.NET Core

I was searching for a code snippet on how to upload a file to DO Spaces but cloud not found anything. After many attempts, I found the solution.

After downloading AWS SDK from nuget manager,

Upload Class:

public static  class UploadFileMPUHighLevelAPITest
        public static  string bucketName = "your spaces name";
        //public static string filePath = "d:\\test upload.txt";
        public static string endpoingURL = "";
        public static IAmazonS3 s3Client;

        public static  bool UploadFile(string filePath, string fileName, string folderName)
            var s3ClientConfig = new AmazonS3Config
                ServiceURL = endpoingURL
            s3Client = new AmazonS3Client(s3ClientConfig);
                var fileTransferUtility = new TransferUtility(s3Client);
                var fileTransferUtilityRequest = new TransferUtilityUploadRequest
                    BucketName = bucketName+ @"/" + folderName,
                    FilePath = filePath,
                    StorageClass = S3StorageClass.StandardInfrequentAccess,
                    PartSize = 6291456, // 6 MB
                    Key = fileName,
                    CannedACL = S3CannedACL.PublicRead
                return true;
            catch (AmazonS3Exception e)
                Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message);
            catch (Exception e)
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
                if (e.Message.Contains("disposed"))
                    return true;
            return false;


  "AWS": {
    "AccessKey": "your access key",
    "SecretKey": "your secret key"


        public void ConfigureServices(IServiceCollection services)
            //enable cors
            services.AddCors(c =>
                c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());

            services.AddDbContext<onusshoppingContext>(options => options.UseMySql(Configuration.GetConnectionString("myContext")));

            Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", Configuration["AWS:AccessKey"]);
            Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", Configuration["AWS:SecretKey"]);

I hope it is useful!

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

The answer is above.

public class ImageController : ControllerBase
    private const int TIMEOUT = 2500;

    // Digital Ocean settings
    private static readonly string S3LoginRoot = "";
    private static readonly string S3BucketName = "";
    private static readonly string AccessKey = "";
    private static readonly string AccessKeySecret = "";
    private static readonly string S3FolderName = "";

    public IActionResult Post([FromForm] IFormFile file)
        bool Sucesso = false;

            AmazonS3Client? s3Client = new(new BasicAWSCredentials(AccessKey, AccessKeySecret), new AmazonS3Config
                ServiceURL = S3LoginRoot,
                Timeout = TimeSpan.FromSeconds(TIMEOUT),
                MaxErrorRetry = 8,

            TransferUtility fileTransferUtility = new(s3Client);

            TransferUtilityUploadRequest? fileTransferUtilityRequest = new()
                BucketName = S3BucketName + @"/" + S3FolderName,
                Key = file.FileName,
                InputStream = file.OpenReadStream(),
                ContentType = file.ContentType,
                StorageClass = S3StorageClass.StandardInfrequentAccess,
                PartSize = 6291456,
                CannedACL = S3CannedACL.PublicRead,


            Sucesso = true;
        catch (Exception Ex)

        return Ok(new { Sucesso });



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel