1

I am wanting to create a WinForm application to upload selected files to digital ocean space. You can imagine it works similar to Cyberduck and written in C#. Thanks a lot

1 Answers1

2

This worked for me to upload files with AWS SDK to Digital Ocean Space :

public static string UploadFile(HttpPostedFileBase file, string filepath)
    {
        if (file != null)
        {
            try
            {
                string accessKey = "xxxxxxxxxxxxxxxxx";
                string secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                AmazonS3Config config = new AmazonS3Config();
                config.ServiceURL = "https://abc1.digitaloceanspaces.com";
                AmazonS3Client s3Client = new AmazonS3Client(
                        accessKey,
                        secretKey,
                        config
                        );
                // Create a client
                AmazonS3Client client = new AmazonS3Client(RegionEndpoint.USEast1); //according to your prefered Region 
                try
                {
                    var fileTransferUtility = new TransferUtility(s3Client);
                    var fileTransferUtilityRequest = new TransferUtilityUploadRequest
                    {
                        BucketName = bucketName + @"/" + filepath, // filepath is your folder name in digital ocean space leave empty if not any.
                        InputStream = file.InputStream,
                        StorageClass = S3StorageClass.StandardInfrequentAccess,
                        Key = file.FileName,
                        CannedACL = S3CannedACL.PublicRead
                    };
                    fileTransferUtility.Upload(fileTransferUtilityRequest);
                }
                catch (AmazonS3Exception e)
                {
                    // Exception code here
                }
            }
            catch (Exception ex)
            {
                // Exception code here
            }
            return file.FileName;
        }
        else
        {
            // else code here 
        }
    }
Raven
  • 31
  • 8
  • 1
    Did you ever get the error where they said The remote certificate is invalid according to the validation procedure – JianYA Feb 04 '23 at 12:46
  • @JainYA No, I didn't get this error. – Raven Feb 06 '23 at 11:48
  • @JianYA I facing same issues too. Did you resolve it? run on IIS Express with self-signed SSL – Azri Zakaria Mar 21 '23 at 14:57
  • were you finally able to upload the file? ? they dont have documentation on its web site – Andres Guillen Hernandez Mar 31 '23 at 03:45
  • 1
    Looks like the Digital Ocean cert is invalid. I think you can can set the `HttpClientFactory` on the `AmazonS3Config` object and ignore SSL errors https://stackoverflow.com/questions/12553277/allowing-untrusted-ssl-certificates-with-httpclient @JianYA if you want to create a separate question I'm happy to provide a more substantal answer. – Benjineer May 27 '23 at 01:44