Search code examples
phpencodingamazon-s3base64php-stream-wrappers

Missing last character while base64 encoding a file from S3 using stream filter


I have a very weird issue with PHP's stream conversion filter (base64_encode). My goal is to read file from s3 bucket using AWS S3 SDK's streamWrapper and base64 encode it, echo it to response. For this I am using following code,

@ob_flush();
@flush();
$fh = fopen(<FILE_PATH>, 'r');
stream_filter_append($fh, 'convert.base64-encode');
fpassthru($fh);
fclose($fh);

This works for all types of file, but for a text file it drops last character. When we decode the base64 response, last character is missing.

For example,

Hello, world! which encodes to SGVsbG8sIHdvcmxkIQ==

But the above function omits last character while encoding, producing the following

SGVsbG8sIHdvcmxk which decodes to Hello, world

Any thoughts guys?

Thank you so much for helping me out.


Solution

  • I am able to get this sorted out with AWS Engineers support.

    The solution can be found here https://github.com/aws/aws-sdk-php/issues/540.

    Thanks.