Amazon S3 .NET: Upload base64 image data?
Asked Answered
C

1

6

I am trying to convert images to base64, and trying to upload that to AWS S3 using C#. I keep getting a remote server not found exception. But I am able to log in programmatically and list the buckets I have. Can you please identify whats wrong.

    static void Main(string[] args)
    {

        string configaccess = ConfigurationManager.AppSettings["AWSAccesskey"];
        string configsecret = ConfigurationManager.AppSettings["AWSSecretkey"];

        var s3Client = new AmazonS3Client(
            configaccess,
            configsecret,
            RegionEndpoint.USEast1
        );

        Byte[] bArray = File.ReadAllBytes("path/foo.jpg");
        String base64String = Convert.ToBase64String(bArray);

        try
        {
            byte[] bytes = Convert.FromBase64String(base64String);

            using (s3Client)
            {
                var request = new PutObjectRequest
                {
                    BucketName = "bucketName",
                    CannedACL = S3CannedACL.PublicRead,
                    Key = string.Format("bucketName/{0}", "foo.jpg")
                };
                using (var ms = new MemoryStream(bytes))
                {
                    request.InputStream = ms;
                    s3Client.PutObject(request);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("AWS Fail");
        }

    }
Custos answered 6/2, 2018 at 3:2 Comment(0)
R
0

I have tested with same code and working fine for me. You need not to specify the bucket name in the Key. We can specify the folder name, if want to store this file in any folder inside the bucket.

Key = string.Format("FolderName/{0}", "foo.jpg").

Retrospection answered 8/2, 2018 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.