0

I have code that downloads HTML file from AWS S3 and convert it to base64

Here is code

 public async Task<string> DownloadFromS3(string bucketName, string fileName)
    {
        var getObjectRequest = new GetObjectRequest
        {
            BucketName = bucketName,
            Key = fileName
        };

        var getObjectResponse = await _s3Client.GetObjectAsync(getObjectRequest);

        using (var fileMemoryStream = new MemoryStream())
        {
            await getObjectResponse.ResponseStream.CopyToAsync(fileMemoryStream);

            var imageBytes = fileMemoryStream.ToArray();

            return Convert.ToBase64String(imageBytes);
        }
    }

after this, in some part of code, I need to convert it back to HTML (not file, just plain HTML)

How I can do this?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Eugene Sukh
  • 2,357
  • 4
  • 42
  • 86
  • Can't find anything about this in google @Crowcoder – Eugene Sukh Apr 28 '21 at 11:51
  • 2
    [`Convert.FromBase64String`](https://learn.microsoft.com/en-us/dotnet/api/system.convert.frombase64string) ? – David Apr 28 '21 at 11:52
  • That was a general .NET question - nothing to do with AWS – smac2020 Apr 28 '21 at 11:58
  • 1
    @EugeneSukh there are dozens of duplicate questions and thousands of Google hits. Crowcoder is right. Simply googling for `c# convert base64` returns 900K hits with the first [this duplicate SO question](https://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string) – Panagiotis Kanavos Apr 28 '21 at 12:01
  • 1
    What's the point of this code? If you already have the HTML text, use it. Don't convert it to BASE64. If you know the content is UTF8-encoded, `Encoding.UTF8.GetString(imageBytes)` is enough. – Panagiotis Kanavos Apr 28 '21 at 12:04
  • Point of this code, that at AWS S3 I have file and I downloading file, not HTML text @PanagiotisKanavos – Eugene Sukh Apr 28 '21 at 12:05
  • 1
    @EugeneSukh so there's no point to it. You have a stream, not a file. If you want to use its contents as a string, you can do so either with `UTF8.GetString` or even better, with a `StreamReader` – Panagiotis Kanavos Apr 28 '21 at 12:05
  • I have another opinion @PanagiotisKanavos – Eugene Sukh Apr 28 '21 at 12:06
  • 2
    @EugeneSukh this isn't a matter of opinion. You retrieved some bytes using a stream. You can use those bytes directly or convert them to a string. There's no point in converting them to BASE64, then back to the original bytes, then to the string you needed in the first place. `Encoding.UTF8.GetString(imageBytes)` would get you the HTML text. Even that wastes RAM by buffering the bytes. You could use `var reader=new StreamReader(getObjectResponse.ResponseStream); var html=reader.ReadToEnd();` and get that string directly – Panagiotis Kanavos Apr 28 '21 at 12:07
  • okay @PanagiotisKanavos – Eugene Sukh Apr 28 '21 at 12:08
  • 2
    What you wrote uses 5 times the size of the HTML file *at least*. There's a copy in the MemoryStream, `imageBytes`, the Base64 string, the buffer returned by `FromBase64String` (which is identical to imageBytes) and finally the `bytes` string. If you used a `StreamReader` you'd only have a single copy – Panagiotis Kanavos Apr 28 '21 at 12:12

1 Answers1

-1

First of all we need to convert from base64 string to byte[] and than get string from it

var attachmentString = await _awsService.DownloadFromS3(Environment.GetEnvironmentVariable("S3_BUCKET_EMAIL_TEMPLATES"),url);

var bytes = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(attachmentString));

Eugene Sukh
  • 2,357
  • 4
  • 42
  • 86