-
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 all 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,159 @@ | ||||||||||
.. _laravel-filesystems: | ||||||||||
|
||||||||||
================== | ||||||||||
GridFS Filesystems | ||||||||||
================== | ||||||||||
|
||||||||||
.. facet:: | ||||||||||
:name: genre | ||||||||||
:values: tutorial | ||||||||||
|
||||||||||
.. meta:: | ||||||||||
:keywords: php framework, gridfs, code example | ||||||||||
|
||||||||||
Overview | ||||||||||
-------- | ||||||||||
|
||||||||||
You can use the | ||||||||||
`GridFS Adapter for Flysystem <https://flysystem.thephpleague.com/docs/adapter/gridfs/>`__ | ||||||||||
to store large files in MongoDB. | ||||||||||
GridFS lets you store files of unlimited size in the same database as your data. | ||||||||||
|
||||||||||
|
||||||||||
Configuration | ||||||||||
------------- | ||||||||||
|
||||||||||
Before using the GridFS driver, install the Flysystem GridFS package through the | ||||||||||
Composer package manager by running the following command: | ||||||||||
|
||||||||||
.. code-block:: bash | ||||||||||
|
||||||||||
composer require league/flysystem-gridfs | ||||||||||
|
||||||||||
Configure `Laravel File Storage <https://laravel.com/docs/{+laravel-docs-version+}/filesystem>`__ | ||||||||||
to use the ``gridfs`` driver in ``config/filesystems.php``: | ||||||||||
|
||||||||||
.. code-block:: php | ||||||||||
|
||||||||||
'disks' => [ | ||||||||||
'gridfs' => [ | ||||||||||
'driver' => 'gridfs', | ||||||||||
'connection' => 'mongodb', | ||||||||||
// 'database' => null, | ||||||||||
// 'bucket' => 'fs', | ||||||||||
// 'prefix' => '', | ||||||||||
// 'read-only' => false, | ||||||||||
// 'throw' => false, | ||||||||||
], | ||||||||||
], | ||||||||||
|
||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
You can configure the disk the following settings in ``config/filesystems.php``: | ||||||||||
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. I: I think you can delete "the disk"
Suggested change
|
||||||||||
|
||||||||||
.. 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 for the GridFS bucket. The driver uses the database of the connection if a database is not specified. | ||||||||||
|
||||||||||
* - ``bucket`` | ||||||||||
- Name or instance of the GridFS bucket. A database can contain multiple buckets identified by their name. Defaults to ``fs``. | ||||||||||
|
||||||||||
* - ``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, instead of using a prefix. | ||||||||||
The prefix should not start with a leading slash ``/``. | ||||||||||
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: Add parentheses around the slash
Suggested change
|
||||||||||
|
||||||||||
* - ``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. If ``false``, | ||||||||||
operations return ``true`` on success and ``false`` on error. Defaults to ``false``. | ||||||||||
|
||||||||||
You can also use a factory or a service name to create an instance of ``MongoDB\GridFS\Bucket``. | ||||||||||
In this case, the options ``connection`` and ``database`` are ignored: | ||||||||||
|
||||||||||
.. code-block:: php | ||||||||||
|
||||||||||
use Illuminate\Foundation\Application; | ||||||||||
use MongoDB\GridFS\Bucket; | ||||||||||
|
||||||||||
'disks' => [ | ||||||||||
'gridfs' => [ | ||||||||||
'driver' => 'gridfs', | ||||||||||
'bucket' => static function (Application $app): Bucket { | ||||||||||
return $app['db']->connection('mongodb') | ||||||||||
->getMongoDB() | ||||||||||
->selectGridFSBucket([ | ||||||||||
'bucketName' => 'avatars', | ||||||||||
'chunkSizeBytes' => 261120, | ||||||||||
]); | ||||||||||
}, | ||||||||||
], | ||||||||||
], | ||||||||||
|
||||||||||
Usage | ||||||||||
----- | ||||||||||
|
||||||||||
A benefit of using Laravel Filesystem is that it provides a common interface | ||||||||||
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: Reword so this sentence is a little less subjective
Suggested change
|
||||||||||
for all the supported file systems. You can use the ``gridfs`` disk in the 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>`__ | ||||||||||
in the Laravel documentation. | ||||||||||
|
||||||||||
Versioning | ||||||||||
---------- | ||||||||||
|
||||||||||
File names in GridFS are metadata in file documents, which are identified by | ||||||||||
unique ObjectIDs. If multiple documents share the same file name, they are | ||||||||||
Comment on lines
+130
to
+131
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: since this is the first time that file metadata documents are mentioned, I think it would be helpful to describe them
Suggested change
|
||||||||||
considered "revisions" and further distinguished by creation timestamps. | ||||||||||
|
||||||||||
The Laravel MongoDB integration uses the GridFS Flysystem adapter. It interacts | ||||||||||
with file revisions in the following ways: | ||||||||||
|
||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
- Reading a file reads the last revision of this file name | ||||||||||
- Writing a file 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 | ||||||||||
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: I'd stay consistent with using either "filename" or "file name"
Suggested change
|
||||||||||
`GridFS API <https://www.mongodb.com/docs/php-library/current/tutorial/gridfs/>`__ | ||||||||||
if you need to work with revisions, as shown in the following code: | ||||||||||
|
||||||||||
.. 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. |
||||||||||
|
||||||||||
.. note:: | ||||||||||
|
||||||||||
If you use a prefix the Filesystem component, you will have to handle it | ||||||||||
by yourself when using the GridFS API directly. | ||||||||||
Comment on lines
+158
to
+159
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: clarify the first part of this sentence ("If you use a prefix the Filesystem component" part) Q: what do you mean by "handle it by yourself" - could you clarify that in the note?
Suggested change
|
Uh oh!
There was an error while loading. Please reload this page.