简体   繁体   English

Amazon S3 .NET:上传base64图像数据?

[英]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#. 我正在尝试将图像转换为base64,并尝试使用C#将其上传到AWS S3。 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"). Key = string.Format(“FolderName / {0}”,“foo.jpg”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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