ZF-10299: S3 Stream wrapper incorrectly reads data due to incorrect Range headers


The range headers should read

0-8191 for the first 8192 bytes, instead we send 0-8192

To test:


define('AWS_KEY', 'a key');
define('AWS_SECRET_KEY', 'a secret');

require_once 'Zend/Service/Amazon/S3.php';

$s3 = new Zend_Service_Amazon_S3(AWS_KEY, AWS_SECRET_KEY);


copy('./test.pdf', 's3://bucket-name/test.pdf');
copy('s3://bucket-name/test.pdf', 'test2.pdf');

var_dump(file_get_contents('test.pdf') == file_get_contents('test2.pdf'));

Simple patch

Index: Zend/Service/Amazon/S3/Stream.php
--- Zend/Service/Amazon/S3/Stream.php   (revision 22829)
+++ Zend/Service/Amazon/S3/Stream.php   (working copy)
@@ -194,7 +194,7 @@
         $range_start = $this->_position;
-        $range_end = $this->_position+$count;
+        $range_end = $this->_position+$count-1;
         // Only fetch more data from S3 if we haven't fetched any data yet (postion=0)
         // OR, the range end position is greater than the size of the current object


This solved my problem. If you stream out a jpeg image that is large enough (950px x 750px) the bottom half will be distorted. Adding this fix solved the problem.

Thank you. Patch fixed the issue for me too.

Given that this bug leads to data loss, perhaps someone with commit privileges can get it resolved?

Checked fix to be complaint to HTTP RFC, added minor correction to the fix.