-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHPORM-186 GridFS adapter for Filesystem #2985
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
Changes from 4 commits
49dd91a
59129f9
04e9c86
0780822
9c7b6ba
3815890
e633d9a
e67b592
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,144 @@ | ||||||
.. _laravel-queues: | ||||||
|
||||||
====== | ||||||
Queues | ||||||
====== | ||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
.. facet:: | ||||||
:name: genre | ||||||
:values: tutorial | ||||||
|
||||||
.. meta:: | ||||||
:keywords: php framework, gridfs, code example | ||||||
|
||||||
Overview | ||||||
-------- | ||||||
|
||||||
Using MongoDB to store files can be done using the | ||||||
`GridFS Adapter for Flysystem <https://flysystem.thephpleague.com/docs/adapter/gridfs/>`__. | ||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
GridFS lets you store files of unlimited size in the same database as your data. | ||||||
This ensures the integrity of transactions and backups. | ||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
|
||||||
Configuration | ||||||
------------- | ||||||
|
||||||
Before using the GridFS driver, you will need to install the Flysystem GridFS package via the Composer package manager: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: we try to avoid the future tense and "via" in the docs. Also, I'd edit this sentence slightly so it introduces the code example:
Suggested change
|
||||||
|
||||||
.. code-block:: bash | ||||||
|
||||||
composer require league/flysystem-gridfs | ||||||
|
||||||
Configure `Laravel File Storage <https://laravel.com/docs/{+laravel-docs-version+}/filesystem>`__, | ||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
to use the ``gridfs`` driver in ``config/filesystems.php``: | ||||||
|
||||||
.. code-block:: php | ||||||
|
||||||
'disks' => [ | ||||||
'gridfs' => [ | ||||||
'driver' => 'gridfs', | ||||||
'connection' => 'mongodb', | ||||||
'database' => 'files', | ||||||
'bucket' => 'fs', | ||||||
'prefix' => '', | ||||||
'read-only' => false, | ||||||
'throw' => false, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the complete list of supported options, with default values repeated just so users know what they can set? Since you have a table below with all options, I'd consider reducing this code example to just the necessary options. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not, but personally I find the complete example more synthetic and quicker to read, or even copy and paste. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Same. If this example was annotated with comments to call out default values (and thus assist user's with reducing their own config) I think it'd stand well on its own and we wouldn't even need the table that follows. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added optional settings in comments. Keeping the table for details. |
||||||
], | ||||||
], | ||||||
|
||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
.. list-table:: | ||||||
:header-rows: 1 | ||||||
:widths: 25 75 | ||||||
|
||||||
* - Setting | ||||||
- Description | ||||||
|
||||||
* - ``driver`` | ||||||
- **Required**. Specifies the filesystem driver to use. Must be ``gridfs`` for MongoDB. | ||||||
|
||||||
* - ``connection`` | ||||||
- The database connection used to store jobs. It must be a ``mongodb`` connection. The driver uses the default connection if a connection is not specified. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: remove the article to match the other descriptions
Suggested change
|
||||||
|
||||||
* - ``database`` | ||||||
- Name of the MongoDB database fot the GridFS bucket. Use the database of the connection if not specified. | ||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
* - ``bucket`` | ||||||
- Name of the GridFS bucket. A database can contain multiple buckets identified by their name. Defaults to ``fs``. | ||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
* - ``prefix`` | ||||||
- Specifies a prefix for the name of the files that are stored in the bucket. Using a distinct bucket is recommended | ||||||
in order to store the files in a different collection. | ||||||
|
||||||
* - ``read-only`` | ||||||
- If ``true``, writing to the GridFS bucket is disabled. Write operations will return ``false`` or throw exceptions | ||||||
depending on the configuration of ``throw``. Defaults to ``false``. | ||||||
|
||||||
* - ``throw`` | ||||||
- If ``true``, exceptions are thrown when an operation cannot be performed. Defaults to ``false``, the operations | ||||||
return ``true`` in case of succes, ``false`` in case of error. | ||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
If you have specific requirements, you can use a factory or a service name to define the bucket. The options | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: what do you mean by "specific requirements"? I think it would be helpful to clarify them here |
||||||
``connection`` and ``database`` are ignored when a ``MongoDB\GridFS\Bucket`` is provided: | ||||||
|
||||||
.. code-block:: php | ||||||
|
||||||
use Illuminate\Foundation\Application; | ||||||
use MongoDB\GridFS\Bucket; | ||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
'disks' => [ | ||||||
'gridfs' => [ | ||||||
'driver' => 'gridfs', | ||||||
'bucket' => static function (Application $app): Bucket { | ||||||
return $app['db']->connection('mongodb') | ||||||
->getMongoDB() | ||||||
->selectGridFSBucket([ | ||||||
'bucketName' => 'avatars', | ||||||
'chunkSizeBytes' => 261120, | ||||||
]); | ||||||
}, | ||||||
], | ||||||
], | ||||||
|
||||||
Usage | ||||||
----- | ||||||
|
||||||
The benefits of using Laravel File Storage facade, is that it provides a common | ||||||
interface for all the supported file systems. Use the ``gridfs`` disk in the | ||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
same way as the ``local`` disk. | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: introduce the code
Suggested change
|
||||||
.. code-block:: php | ||||||
|
||||||
$disk = Storage::disk('gridfs'); | ||||||
|
||||||
// Write the file "hello.txt" into GridFS | ||||||
$disk->put('hello.txt', 'Hello World!'); | ||||||
|
||||||
// Read the file | ||||||
echo $disk->get('hello.txt'); // Hello World! | ||||||
|
||||||
To learn more Laravel File Storage, see | ||||||
`Laravel File Storage <https://laravel.com/docs/{+laravel-docs-version+}/filesystem>`__. | ||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Versioning | ||||||
---------- | ||||||
|
||||||
In GridFS, file names are metadata to file objects identified by unique MongoDB ObjectID. | ||||||
There may be more than one file with the same name, they are called "revisions": | ||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
- Reading a file reads the last revision of this file name | ||||||
- Writing to a file name creates a new revision for this file name | ||||||
- Renaming a file renames all the revisions of this file name | ||||||
- Deleting a file deletes all the revisions of this file name | ||||||
|
||||||
The GridFS Adapter for Flysystem does not provide access to a specific revision | ||||||
of a filename, you must use the :manual:`GridFS API </tutorial/gridfs/>` if you | ||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
need to work with revisions. | ||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
.. code-block:: php | ||||||
|
||||||
// Create a bucket service from the MongoDB connection | ||||||
/** @var \MongoDB\GridFS\Bucket $bucket */ | ||||||
$bucket = $app['db']->connection('mongodb')->getMongoDB()->selectGridFSBucket(); | ||||||
|
||||||
// Download the last but one version of a file | ||||||
$bucket->openDownloadStreamByName('hello.txt', ['revision' => -2]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since PathPrefixer doesn't apply here, would users need to write There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use the leading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no prefixing done in PHPLIB, so the filename is used exactly as provided. My question here is: would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps this is answered by #2985 (comment). In that case, I suppose the note would be to remind users that prefixing is entirely handled by Flysystem. So if they're using a non-empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flysystem always adds the prefix to the file name stored in MongoDB. |
Uh oh!
There was an error while loading. Please reload this page.