Issues

ZF-10035: S3_Stream implementation broken

Description

fread from a s3 stream always downloads the whole file. This is due to outdated API implementation. Check here for the latest:

http://docs.amazonwebservices.com/AmazonS3/latest/…

Here is the new stream_read function which resolve the 1) wrong range header and 2) wrong check for 200 http code:

{{ public function stream_read($count) { if (!$this->_objectName) { return false; }

    $range_start = $this->_position;
    $range_end = $this->_position+$count;

    // 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
    // buffer AND if the range end position is less than or equal to the object's
    // size returned by S3
    if (($this->_position == 0) || (($range_end > strlen($this->_objectBuffer)) && ($range_end <= $this->_objectSize))) {

        $headers = "Range:bytes=$range_start-$range_end";

        $response = $this->_s3->_makeRequest('GET', $this->_objectName, null, $headers);
        $status = (string)$response->getStatus();
        if (strpos($status, "20") === 0) {
            $this->_objectBuffer .= $response->getBody();
        }
    }

    $data = substr($this->_objectBuffer, $this->_position, $count);
    $this->_position += strlen($data);
    return $data;
}

}}

Comments

Due to http://bugs.php.net/21641 stream_read() always gets 8292 bytes as count, so although on client side (fread) everything works fine, actual stream read step is fixed. But within this step, partial data is requested and returned by Amazon as expected.