Skip to content

Commit df79843

Browse files
authored
DOCSP-41972: GridFS guide (#133)
* DOCSP-41972: GridFS guide * fixes * code edits * fix * RR feedback * phpmethod * fix * JM most feedback * alternate upload/download methods * file revisions * code fix * tojson * edits * JM feedback 2 * edits * JM last feedback
1 parent 54c05bf commit df79843

File tree

4 files changed

+527
-0
lines changed

4 files changed

+527
-0
lines changed
9.01 KB
Loading

source/includes/write/gridfs.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
use MongoDB\Client;
5+
use MongoDB\BSON\ObjectId;
6+
7+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
8+
$client = new Client($uri);
9+
10+
// start-to-json
11+
function toJSON(object $document): string
12+
{
13+
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON();
14+
}
15+
// end-to-json
16+
17+
// Creates a GridFS bucket or references an existing one
18+
// start-create-bucket
19+
$bucket = $client->db->selectGridFSBucket();
20+
// end-create-bucket
21+
22+
// Creates or references a GridFS bucket with a custom name
23+
// start-create-custom-bucket
24+
$custom_bucket = $client->db->selectGridFSBucket(
25+
['bucketName' => 'myCustomBucket']
26+
);
27+
// end-create-custom-bucket
28+
29+
// Uploads a file called "my_file" to the GridFS bucket and writes data to it
30+
// start-open-upload-stream
31+
$stream = $bucket->openUploadStream('my_file', [
32+
'metadata' => ['contentType' => 'text/plain']
33+
]);
34+
fwrite($stream, 'Data to store');
35+
fclose($stream);
36+
// end-open-upload-stream
37+
38+
// Uploads data to a stream, then writes the stream to a GridFS file
39+
// start-upload-from-stream
40+
$file = fopen('/path/to/input_file', 'rb');
41+
$bucket->uploadFromStream('new_file', $file);
42+
// end-upload-from-stream
43+
44+
// Prints information about each file in the bucket
45+
// start-retrieve-file-info
46+
$files = $bucket->find();
47+
foreach ($files as $file_doc) {
48+
echo toJSON($file_doc), PHP_EOL;
49+
}
50+
// end-retrieve-file-info
51+
52+
// Downloads the "my_file" file from the GridFS bucket and prints its contents
53+
// start-open-download-stream-name
54+
$stream = $bucket->openDownloadStreamByName('my_file');
55+
$contents = stream_get_contents($stream);
56+
echo $contents, PHP_EOL;
57+
fclose($stream);
58+
// end-open-download-stream-name
59+
60+
// Downloads a file from the GridFS bucket by referencing its ObjectId value
61+
// start-download-files-id
62+
$stream = $bucket->openDownloadStream(new ObjectId('66e0a5487c880f844c0a32b1'));
63+
$contents = stream_get_contents($stream);
64+
fclose($stream);
65+
// end-download-files-id
66+
67+
// Downloads the original "my_file" file from the GridFS bucket
68+
// start-download-file-revision
69+
$stream = $bucket->openDownloadStreamByName('my_file', ['revision' => 0]);
70+
$contents = stream_get_contents($stream);
71+
fclose($stream);
72+
// end-download-file-revision
73+
74+
// Downloads an entire GridFS file to a download stream
75+
// start-download-to-stream
76+
$file = fopen('/path/to/output_file', 'wb');
77+
$bucket->downloadToStream(
78+
new ObjectId('66e0a5487c880f844c0a32b1'),
79+
$file,
80+
);
81+
// end-download-to-stream
82+
83+
// Renames a file from the GridFS bucket with the specified ObjectId
84+
// start-rename-files
85+
$bucket->rename(new ObjectId('66e0a5487c880f844c0a32b1'), 'new_file_name');
86+
// end-rename-files
87+
88+
// Deletes a file from the GridFS bucket with the specified ObjectId
89+
// start-delete-files
90+
$bucket->delete(new ObjectId('66e0a5487c880f844c0a32b1'));
91+
// end-delete-files

source/write.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Write Data to MongoDB
2626
/write/replace
2727
/write/insert
2828
/write/update
29+
/write/gridfs
2930

3031
Overview
3132
--------

0 commit comments

Comments
 (0)