Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stream feature to AwsS3 adapter. #334

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Gaufrette/Adapter/AwsS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Gaufrette\Adapter;
use Aws\S3\S3Client;
use Gaufrette\Stream\Http;

/**
* Amazon S3 adapter using the AWS SDK for PHP v2.x
Expand Down Expand Up @@ -221,6 +222,20 @@ public function isDirectory($key)
return count($result['Contents']) > 0;
}

/**
* {@inheritDoc}
*/
public function createStream($key) {
$context = stream_context_create(array(
's3' => array(
'seekable' => true
)
));

$this->service->registerStreamWrapper();
return new Http("s3://$this->bucket/$key", $context);
}

/**
* Ensures the specified bucket exists. If the bucket does not exists
* and the create option is set to true, it will try to create the
Expand Down
107 changes: 107 additions & 0 deletions src/Gaufrette/Stream/Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
namespace Gaufrette\Stream;

use Gaufrette\Stream;
use Gaufrette\StreamMode;

/**
* Http stream.
* Similar to Local stream, but the HTTP protocol
* does not allow simultaneous reading and writing.
*
* @author Jon Skarpeteig <[email protected]>
*/
class Http extends Local implements Stream
{

protected $context;

/**
* Constructor
*
* @param string $path
*/
public function __construct($path, $context = null)
{
$this->context = ($context === null) ? stream_context_create() : $context;
$this->path = $path;
}

/**
* {@inheritDoc}
*/
public function open(StreamMode $mode)
{
if ($mode->allowsRead() && $mode->allowsWrite()) {
throw new \RuntimeException(sprintf('File "%s" cannot be opened with read and write mode simultaneously', $this->path));
}

try {
$fileHandle = @fopen($this->path, $mode->getMode(), false, $this->context);
} catch (\Exception $e) {
$fileHandle = false;
}

if (false === $fileHandle) {
throw new \RuntimeException(sprintf('File "%s" cannot be opened', $this->path));
}

$this->mode = $mode;
$this->fileHandle = $fileHandle;

return true;
}

/**
* {@inheritDoc}
*/
public function stat()
{
if ($this->fileHandle) {

/**
* 0 dev device number
* 1 ino inode number *
* 2 mode inode protection mode
* 3 nlink number of links
* 4 uid userid of owner *
* 5 gid groupid of owner *
* 6 rdev device type, if inode device
* 7 size size in bytes
* 8 atime time of last access (Unix timestamp)
* 9 mtime time of last modification (Unix timestamp)
* 10 ctime time of last inode change (Unix timestamp)
* 11 blksize blocksize of filesystem IO **
* 12 blocks number of 512-byte blocks allocated **
* On Windows this will always be 0.
* * Only valid on systems supporting the st_blksize type - other systems (e.g. Windows) return -1.
*/

/**
* Directories must be a mode like 040777 (octal), and files a mode like 0100666.
*
* If you wish the file to be executable, use 7s instead of 6s. The last 3 digits
* are exactly the same thing as what you pass to chmod. 040000 defines a directory,
* and 0100000 defines a file.
*/

return array(
'dev' => 0,
'ino' => 0,
'mode' => 0100666,
'nlink' => 0,
'uid' => 0,
'gid' => 0,
'rdev ' => 0,
'size' => filesize($this->path),
'atime' => 0,
'mtime' => filemtime($this->path),
'ctime' => 0,
'blksize' => - 1,
'blocks' => - 1
);
}

return false;
}
}
6 changes: 3 additions & 3 deletions src/Gaufrette/Stream/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*/
class Local implements Stream
{
private $path;
private $mode;
private $fileHandle;
protected $path;
protected $mode;
protected $fileHandle;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks wrong to me

Copy link
Author

@jskarpe jskarpe Sep 20, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making things protected means they become extension points which need to be taken into account for backward compatibility. And extension points by inheritance are the worse ones regarding BC layers (we implement BC layers in Symfony since years, and the ones involving inheritance and BC for protected things have always been the worse ones to implement)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what's the fix? Copy everything over?

On Sun, Sep 20, 2015 at 8:32 AM, Christophe Coevoet <
[email protected]> wrote:

In src/Gaufrette/Stream/Local.php
#334 (comment):

@@ -12,9 +12,9 @@
*/
class Local implements Stream
{

  • private $path;
  • private $mode;
  • private $fileHandle;
  • protected $path;
  • protected $mode;
  • protected $fileHandle;

Making things protected means they become extension points which need to
be taken into account for backward compatibility. And extension points by
inheritance are the worse ones regarding BC layers (we implement BC layers
in Symfony since years, and the ones involving inheritance and BC for
protected things have always been the worse ones to implement)


Reply to this email directly or view it on GitHub
https://github.com/KnpLabs/Gaufrette/pull/334/files#r39927186.


/**
* Constructor
Expand Down