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?
Convert.FromBase64String?c# convert base64returns 900K hits with the first this duplicate SO questionEncoding.UTF8.GetString(imageBytes)is enough.Encoding.UTF8.GetString(imageBytes)would get you the HTML text. Even that wastes RAM by buffering the bytes. You could usevar reader=new StreamReader(getObjectResponse.ResponseStream); var html=reader.ReadToEnd();and get that string directlyimageBytes, the Base64 string, the buffer returned byFromBase64String(which is identical to imageBytes) and finally thebytesstring. If you used aStreamReaderyou'd only have a single copy