-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41972: GridFS guide #133
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 9 commits
f2a1dff
65e67fb
f709846
fb135d1
1389902
5a40fcb
8ed2975
c590514
5a8cd80
7ccbe17
279a5a5
3e7d5e0
6d401a0
34a6abf
af8020c
e1f1190
706a25f
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,89 @@ | ||||||
<?php | ||||||
require 'vendor/autoload.php'; // include Composer's autoloader | ||||||
|
||||||
use MongoDB\Client; | ||||||
use MongoDB\BSON\ObjectID; | ||||||
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.
Suggested change
PHP allows case-insensitive class names, but the canonical name is ObjectId. |
||||||
use MongoDB\GridFS\Bucket; | ||||||
use MongoDB\Driver\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.
Suggested change
These |
||||||
|
||||||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||||||
$client = new MongoDB\Client($uri); | ||||||
|
||||||
// start-to-json | ||||||
function toJSON(object $document): string | ||||||
{ | ||||||
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON(); | ||||||
} | ||||||
// end-to-json | ||||||
|
||||||
// Creates a GridFS bucket or references an existing one | ||||||
// start-create-bucket | ||||||
$bucket = $client->db->selectGridFSBucket(); | ||||||
// end-create-bucket | ||||||
|
||||||
// Creates or references a GridFS bucket with a custom name | ||||||
// start-create-custom-bucket | ||||||
$custom_bucket = $client->db->selectGridFSBucket( | ||||||
['bucketName' => 'myCustomBucket'] | ||||||
); | ||||||
// end-create-custom-bucket | ||||||
|
||||||
// Uploads a file called "my_file" to the GridFS bucket and writes data to it | ||||||
// start-open-upload-stream | ||||||
$stream = $bucket->openUploadStream('my_file', [ | ||||||
'metadata' => ['contentType' => 'text/plain'] | ||||||
]); | ||||||
fwrite($stream, 'Data to store'); | ||||||
fclose($stream); | ||||||
norareidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// end-open-upload-stream | ||||||
|
||||||
// Uploads data to a stream, then writes the stream to a GridFS file | ||||||
// start-upload-from-stream | ||||||
$stream = fopen('php://temp', 'w+b'); | ||||||
fwrite($stream, 'Data to store'); | ||||||
rewind($stream); | ||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
$bucket->uploadFromStream('new_file', $stream); | ||||||
// end-upload-from-stream | ||||||
|
||||||
// Prints information about each file in the bucket | ||||||
// start-retrieve-file-info | ||||||
$files = $bucket->find(); | ||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
foreach ($files as $file_doc) { | ||||||
echo toJSON($file_doc), PHP_EOL; | ||||||
} | ||||||
// end-retrieve-file-info | ||||||
|
||||||
// Downloads the "my_file" file from the GridFS bucket and prints its contents | ||||||
// start-open-download-stream-name | ||||||
$stream = $bucket->openDownloadStreamByName('my_file'); | ||||||
$contents = stream_get_contents($stream); | ||||||
echo $contents, PHP_EOL; | ||||||
fclose($stream); | ||||||
// end-open-download-stream-name | ||||||
|
||||||
// Downloads a file from the GridFS bucket by referencing its ObjectID value | ||||||
// start-download-files-id | ||||||
$stream = $bucket->openDownloadStream(new ObjectID('66e0a5487c880f844c0a32b1')); | ||||||
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.
Suggested change
|
||||||
$contents = stream_get_contents($stream); | ||||||
fclose($stream); | ||||||
// end-download-files-id | ||||||
|
||||||
// Downloads an entire GridFS file to a download stream | ||||||
// start-download-to-stream | ||||||
$stream = fopen('php://temp', 'w+b'); | ||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
$bucket->downloadToStream( | ||||||
new ObjectID('66e0a5487c880f844c0a32b1'), | ||||||
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.
Suggested change
|
||||||
$stream | ||||||
); | ||||||
// end-download-to-stream | ||||||
|
||||||
// Renames a file from the GridFS bucket with the specified ObjectID | ||||||
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.
Suggested change
Consistency with the server manual. |
||||||
// start-rename-files | ||||||
$bucket->rename(new ObjectID('66e0a5487c880f844c0a32b1'), 'new_file_name'); | ||||||
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.
Suggested change
|
||||||
// end-rename-files | ||||||
|
||||||
// Deletes a file from the GridFS bucket with the specified ObjectID | ||||||
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.
Suggested change
|
||||||
// start-delete-files | ||||||
$bucket->delete(new ObjectID('66e0a5487c880f844c0a32b1')); | ||||||
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.
Suggested change
|
||||||
// end-delete-files |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ Write Data to MongoDB | |
/write/replace | ||
/write/insert | ||
/write/update | ||
/write/gridfs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed this earlier. The code below uses
new MongoDB\Client(...)
so thisuse
statement has no effect. Consider removing it.Alternatively, decide if you want to rely on
use
statements for all class references in the interest of consistency. I see you're doing that fornew ObjectId(...)
.