1
1
# 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 )
4
4
# end create bucket
5
5
6
6
# start create custom bucket
7
- const bucket = new mongodb .GridFSBucket (db , { bucketName : 'myCustomBucket' });
7
+ bucket = gridfs .GridFSBucket (db , bucket_name = 'myCustomBucket' )
8
8
# end create custom bucket
9
9
10
10
# 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" )
16
13
# end upload files
17
14
18
15
# 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 ()
23
18
# end retrieve file info
24
19
25
20
# 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 )
28
23
# end download files name
29
24
30
25
# 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 )
33
28
# end download files id
34
29
35
30
# start rename files
36
- bucket .rename (ObjectId ( "60edece5e06275bf0463aaf3" ) , "newFileName" );
31
+ fs .rename (file_id , "new_file_name" )
37
32
# end rename files
38
33
39
34
# start delete files
40
- bucket .delete (ObjectId ( "60edece5e06275bf0463aaf3" ));
35
+ bucket .delete (file_id )
41
36
# end delete files
0 commit comments