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

[RFC] Storage/FileStorage interface #3

Open
egorio opened this issue May 13, 2016 · 6 comments
Open

[RFC] Storage/FileStorage interface #3

egorio opened this issue May 13, 2016 · 6 comments
Labels

Comments

@egorio
Copy link

egorio commented May 13, 2016

I think we could design some interfaces for working with different file storages, such as local file system, ftp, sftp, S3, RackSpace, etc.

Each storage can have several 'root directories' (containers, buckets).

It could be one interface, for instance (here $bucket is a string):

interface FileStorage
{
    public function exists($bucketName, $fileName);
    public function upload($bucketName, $sourceFile, $fileName);
    public function download($bucketName, $fileName, $destinationFile);
    public function write($bucketName, $fileName, $content);
    public function read($bucketName, $fileName);
    public function delete($bucketName, $fileName);
    ...
    public function getUrl($bucketName, $fileName);
}

Now let think we have S3FileStorage implements FileStorage.

$storage = new S3FileStorage();
$storage->configure([
    'images' => ['baseUrl' => '...', ...]  // Configure buckets, for example
]);
$storage->connect(...); // Connect to S3 server

$url = $storage->getUrl('images', 'logo.png');

Or 2 interfaces (the first one is for connection, the second one is for working with files), like:

interface FileStorage
{
    public function addBucket($name, FileStorageBucket $bucket);
    public function getBucket($name);
    public function getBuckets();
}

and

interface FileStorageBucket
{
    public function exists($fileName);
    public function upload($sourceFile, $fileName);
    public function download($fileName, $destinationFile);
    public function write($fileName, $content);
    public function read($fileName);
    public function delete($fileName);
    ...
    public function getUrl($fileName);
}

Now let think we have S3FileStorage implements FileStorage and S3FileStorageBucket ...

$bucket = new S3FileStorageBucket();
$bucket->setBaseUrl(...);
$bucket->...

$storage = new S3FileStorage();
$storage->connect(...); // Connect to S3 server
$storage->addBucket('images', $bucket);

$url = $storage->getBucket('images')->getUrl('logo.png');
@saji89
Copy link

saji89 commented May 13, 2016

Does this relate to the PHP League Flysystem ?

@shadowhand
Copy link

As @saji89 said, we already have Flysystem, which is framework agnostic. Why do we need a specific interface?

@egorio
Copy link
Author

egorio commented May 13, 2016

@saji89, @shadowhand The Flysystem is just the one of file system abstractions, like Gaufrette and Filicious.

Here we are talking about standard that other developers can use.

Sure we can take Flysystem (as it the most popular package) as the basis of the standard.

@m-r-r
Copy link

m-r-r commented May 13, 2016

Hello,
What is the purpose of the $bucket parameter ?
Also, what would happen in case of error ? Which methods can throw an exception ?

@xyNNN xyNNN changed the title Storage/FileStorage interface [RFC] Storage/FileStorage interface May 13, 2016
@geggleto geggleto added the [RFC] label May 16, 2016
@egorio
Copy link
Author

egorio commented May 16, 2016

@m-r-r, just an idea - one storage has one connection, but it can have several buckets (containers or root folder aliases). Also each bucket can have it's own url.

This is why I proposed to separate "connection" and "bucket" somehow.

In the first interface I proposed, bucket logic can be implemented 'inside' of FileStorage class (we just describe main interface), if we'll decided to make 2 interfaces then 'bucket' logic can be described in specific class.

See SDK for Amazon S3 or RackSpace.

@wshafer
Copy link

wshafer commented Sep 19, 2017

Just found my way in here, and have a couple of thoughts.

First $bucketName has no meaning outside things like Asure, S3, etc. That seems like it should be an adapter issue rather then an interfaces problem. Of course I'm assuming an instance of this would already be pre-configured long before it was passed into the consumer... Someone coding against the interface probably shouldn't really care about the bucket, root path. prefix, etc. I think that'd be best configured for the instance long before we used the interface to write/read the files.

The next issue is that this does not address streams at all. It seems this interface is an all or nothing, which would make it pretty useless in batch processing. It's fopen + fwrite or file_put_contents issue.

Just my .02

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants