-
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
Merged
norareidy
merged 17 commits into
mongodb:php-standardization
from
norareidy:DOCSP-41972-gridfs
Sep 16, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f2a1dff
DOCSP-41972: GridFS guide
norareidy 65e67fb
fixes
norareidy f709846
code edits
norareidy fb135d1
fix
norareidy 1389902
RR feedback
norareidy 5a40fcb
phpmethod
norareidy 8ed2975
fix
norareidy c590514
JM most feedback
norareidy 5a8cd80
alternate upload/download methods
norareidy 7ccbe17
file revisions
norareidy 279a5a5
code fix
norareidy 3e7d5e0
tojson
norareidy 6d401a0
edits
norareidy 34a6abf
JM feedback 2
norareidy af8020c
edits
norareidy e1f1190
JM last feedback
norareidy 706a25f
Merge remote-tracking branch 'upstream/php-standardization' into DOCS…
norareidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
use MongoDB\Client; | ||
use MongoDB\BSON\ObjectId; | ||
|
||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||
$client = new 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 | ||
$file = fopen('/path/to/input_file', 'rb'); | ||
$bucket->uploadFromStream('new_file', $file); | ||
// 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')); | ||
$contents = stream_get_contents($stream); | ||
fclose($stream); | ||
// end-download-files-id | ||
|
||
// Downloads the original "my_file" file from the GridFS bucket | ||
// start-download-file-revision | ||
$stream = $bucket->openDownloadStreamByName('my_file', ['revision' => 0]); | ||
$contents = stream_get_contents($stream); | ||
fclose($stream); | ||
// end-download-file-revision | ||
|
||
// Downloads an entire GridFS file to a download stream | ||
// start-download-to-stream | ||
$file = fopen('/path/to/output_file', 'wb'); | ||
$bucket->downloadToStream( | ||
new ObjectId('66e0a5487c880f844c0a32b1'), | ||
$file, | ||
); | ||
// end-download-to-stream | ||
|
||
// Renames a file from the GridFS bucket with the specified ObjectId | ||
// start-rename-files | ||
$bucket->rename(new ObjectId('66e0a5487c880f844c0a32b1'), 'new_file_name'); | ||
// end-rename-files | ||
|
||
// Deletes a file from the GridFS bucket with the specified ObjectId | ||
// start-delete-files | ||
$bucket->delete(new ObjectId('66e0a5487c880f844c0a32b1')); | ||
// end-delete-files |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ Write Data to MongoDB | |
/write/replace | ||
/write/insert | ||
/write/update | ||
/write/gridfs | ||
|
||
Overview | ||
-------- | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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(...)
.