Skip to content

Commit 8f7a2d5

Browse files
committed
DOCSP-42254: Fix GridFS code
1 parent 15d2912 commit 8f7a2d5

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

source/includes/gridfs/gridfs.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
# start create bucket
2-
const db = client.db(dbName);
3-
const bucket = new mongodb.GridFSBucket(db);
2+
db = MongoClient().test
3+
bucket = gridfs.GridFSBucket(db)
44
# end create bucket
55

66
# start create custom bucket
7-
const bucket = new mongodb.GridFSBucket(db, { bucketName: 'myCustomBucket' });
7+
bucket = gridfs.GridFSBucket(db, bucket_name='myCustomBucket')
88
# end create custom bucket
99

1010
# start upload files
11-
fs.createReadStream('./myFile').
12-
pipe(bucket.openUploadStream('myFile', {
13-
chunkSizeBytes: 1048576,
14-
metadata: { field: 'myField', value: 'myValue' }
15-
}));
11+
with open("my_file", "rb") as f:
12+
file_id = fs.put(f, filename="my_file")
1613
# end upload files
1714

1815
# start retrieve file info
19-
const cursor = bucket.find({});
20-
for await (const doc of cursor) {
21-
console.log(doc);
22-
}
16+
for file in bucket.find({}):
17+
data = file.read()
2318
# end retrieve file info
2419

2520
# start download files name
26-
bucket.openDownloadStreamByName('myFile').
27-
pipe(fs.createWriteStream('./outputFile'));
21+
destination = open('output_file','wb')
22+
bucket.download_to_stream_by_name("my_file", destination)
2823
# end download files name
2924

3025
# start download files id
31-
bucket.openDownloadStream(ObjectId("60edece5e06275bf0463aaf3")).
32-
pipe(fs.createWriteStream('./outputFile'));
26+
destination = open('output_file','wb+')
27+
bucket.download_to_stream(file_id, destination)
3328
# end download files id
3429

3530
# start rename files
36-
bucket.rename(ObjectId("60edece5e06275bf0463aaf3"), "newFileName");
31+
fs.rename(file_id, "new_file_name")
3732
# end rename files
3833

3934
# start delete files
40-
bucket.delete(ObjectId("60edece5e06275bf0463aaf3"));
35+
bucket.delete(file_id)
4136
# end delete files

0 commit comments

Comments
 (0)