You are here

public function AmazonS3StreamWrapper::stream_read in AmazonS3 7

Support for fread(), file_get_contents() etc.

Parameters

int $count: Maximum number of bytes to be read.

Return value

string The string that was read, or FALSE in case of an error.

Overrides StreamWrapperInterface::stream_read

See also

http://php.net/manual/en/streamwrapper.stream-read.php

File

./AmazonS3StreamWrapper.inc, line 492
Drupal stream wrapper implementation for Amazon S3

Class

AmazonS3StreamWrapper
@file Drupal stream wrapper implementation for Amazon S3

Code

public function stream_read($count) {

  // Make sure that count doesn't exceed object size.
  if ($count + $this->position > $this->objectSize) {
    $count = $this->objectSize - $this->position;
  }
  $data = '';
  if ($count > 0) {
    $range_end = $this->position + $count - 1;
    if ($range_end > $this->bufferLength) {
      $opts = array(
        'range' => $this->position . '-' . $range_end,
      );
      try {
        $response = $this
          ->getS3()
          ->get_object($this->bucket, $this
          ->getLocalPath($this->uri), $opts);
        if ($response
          ->isOK()) {
          $this->buffer .= $response->body;
          $this->bufferLength += strlen($response->body);
          $data = substr($response->body, 0, min($count, $this->objectSize));
          $this->position += strlen($data);
        }
      } catch (Exception $e) {
        watchdog('amazons3', 'Unable to read @uri', array(
          '@uri' => $this->uri,
        ), WATCHDOG_NOTICE);
      }
    }
  }
  return $data;
}