简体   繁体   中英

Amazon S3 .NET: Upload base64 image data?

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");
        }

    }

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").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM