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

October 31, 2019 119 views
DigitalOcean Spaces

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 = "https://fra1.digitaloceanspaces.com";
        public static IAmazonS3 s3Client;

        public static  bool UploadFile(string filePath, string fileName, string folderName)
        {
            var s3ClientConfig = new AmazonS3Config
            {
                ServiceURL = endpoingURL
            };
            s3Client = new AmazonS3Client(s3ClientConfig);
            try
            {
                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
                };
                fileTransferUtility.Upload(fileTransferUtilityRequest);
                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;
         }
    }

appsettings.json

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

Startup.cs

        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")));
            services.Configure<HostingDetails>(Configuration.GetSection("Hosting"));
            services.Configure<AppSetting>(Configuration.GetSection("AppSetting"));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", Configuration["AWS:AccessKey"]);
            Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", Configuration["AWS:SecretKey"]);

        }

I hope it is useful!

1 Answer

The answer is above.

Have another answer? Share your knowledge.