Skip to content

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
merged 17 commits into from
Sep 16, 2024
Binary file added source/includes/figures/GridFS-upload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions source/includes/write/gridfs.php
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;
Copy link
Member

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 this use 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 for new ObjectId(...).

use MongoDB\BSON\ObjectID;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\ObjectId;

PHP allows case-insensitive class names, but the canonical name is ObjectId.

use MongoDB\GridFS\Bucket;
use MongoDB\Driver\Manager;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use MongoDB\GridFS\Bucket;
use MongoDB\Driver\Manager;

These use statements are no longer needed.


$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);
// 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);

$bucket->uploadFromStream('new_file', $stream);
// end-upload-from-stream

// Prints information about each file in the bucket
// start-retrieve-file-info
$files = $bucket->find();
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'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$stream = $bucket->openDownloadStream(new ObjectID('66e0a5487c880f844c0a32b1'));
$stream = $bucket->openDownloadStream(new ObjectId('66e0a5487c880f844c0a32b1'));

$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');
$bucket->downloadToStream(
new ObjectID('66e0a5487c880f844c0a32b1'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new ObjectID('66e0a5487c880f844c0a32b1'),
new ObjectId('66e0a5487c880f844c0a32b1'),

$stream
);
// end-download-to-stream

// Renames a file from the GridFS bucket with the specified ObjectID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Renames a file from the GridFS bucket with the specified ObjectID
// Renames a file from the GridFS bucket with the specified ObjectId

Consistency with the server manual.

// start-rename-files
$bucket->rename(new ObjectID('66e0a5487c880f844c0a32b1'), 'new_file_name');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$bucket->rename(new ObjectID('66e0a5487c880f844c0a32b1'), 'new_file_name');
$bucket->rename(new ObjectId('66e0a5487c880f844c0a32b1'), 'new_file_name');

// end-rename-files

// Deletes a file from the GridFS bucket with the specified ObjectID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Deletes a file from the GridFS bucket with the specified ObjectID
// Deletes a file from the GridFS bucket with the specified ObjectId

// start-delete-files
$bucket->delete(new ObjectID('66e0a5487c880f844c0a32b1'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$bucket->delete(new ObjectID('66e0a5487c880f844c0a32b1'));
$bucket->delete(new ObjectId('66e0a5487c880f844c0a32b1'));

// end-delete-files
1 change: 1 addition & 0 deletions source/write.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Write Data to MongoDB
/write/replace
/write/insert
/write/update
/write/gridfs
Loading
Loading