@@ -450,6 +450,53 @@ def open_upload_stream(self, filename, chunk_size_bytes=None,
450
450
451
451
return GridIn (self ._collection , ** opts )
452
452
453
+ def open_upload_stream_with_id (
454
+ self , file_id , filename , chunk_size_bytes = None , metadata = None ):
455
+ """Opens a Stream that the application can write the contents of the
456
+ file to.
457
+
458
+ The user must specify the file id and filename, and can choose to add
459
+ any additional information in the metadata field of the file document
460
+ or modify the chunk size.
461
+ For example::
462
+
463
+ my_db = MongoClient().test
464
+ fs = GridFSBucket(my_db)
465
+ grid_in, file_id = fs.open_upload_stream(
466
+ ObjectId(),
467
+ "test_file",
468
+ chunk_size_bytes=4,
469
+ metadata={"contentType": "text/plain"})
470
+ grid_in.write("data I want to store!")
471
+ grid_in.close() # uploaded on close
472
+
473
+ Returns an instance of :class:`~gridfs.grid_file.GridIn`.
474
+
475
+ Raises :exc:`~gridfs.errors.NoFile` if no such version of
476
+ that file exists.
477
+ Raises :exc:`~ValueError` if `filename` is not a string.
478
+
479
+ :Parameters:
480
+ - `file_id`: The id to use for this file. The id must not have
481
+ already been used for another file.
482
+ - `filename`: The name of the file to upload.
483
+ - `chunk_size_bytes` (options): The number of bytes per chunk of this
484
+ file. Defaults to the chunk_size_bytes in :class:`GridFSBucket`.
485
+ - `metadata` (optional): User data for the 'metadata' field of the
486
+ files collection document. If not provided the metadata field will
487
+ be omitted from the files collection document.
488
+ """
489
+ validate_string ("filename" , filename )
490
+
491
+ opts = {"_id" : file_id ,
492
+ "filename" : filename ,
493
+ "chunk_size" : (chunk_size_bytes if chunk_size_bytes
494
+ is not None else self ._chunk_size_bytes )}
495
+ if metadata is not None :
496
+ opts ["metadata" ] = metadata
497
+
498
+ return GridIn (self ._collection , ** opts )
499
+
453
500
def upload_from_stream (self , filename , source , chunk_size_bytes = None ,
454
501
metadata = None ):
455
502
"""Uploads a user file to a GridFS bucket.
@@ -488,6 +535,43 @@ def upload_from_stream(self, filename, source, chunk_size_bytes=None,
488
535
489
536
return gin ._id
490
537
538
+ def upload_from_stream_with_id (self , file_id , filename , source ,
539
+ chunk_size_bytes = None , metadata = None ):
540
+ """Uploads a user file to a GridFS bucket with a custom file id.
541
+
542
+ Reads the contents of the user file from `source` and uploads
543
+ it to the file `filename`. Source can be a string or file-like object.
544
+ For example::
545
+
546
+ my_db = MongoClient().test
547
+ fs = GridFSBucket(my_db)
548
+ file_id = fs.upload_from_stream(
549
+ ObjectId(),
550
+ "test_file",
551
+ "data I want to store!",
552
+ chunk_size_bytes=4,
553
+ metadata={"contentType": "text/plain"})
554
+
555
+ Raises :exc:`~gridfs.errors.NoFile` if no such version of
556
+ that file exists.
557
+ Raises :exc:`~ValueError` if `filename` is not a string.
558
+
559
+ :Parameters:
560
+ - `file_id`: The id to use for this file. The id must not have
561
+ already been used for another file.
562
+ - `filename`: The name of the file to upload.
563
+ - `source`: The source stream of the content to be uploaded. Must be
564
+ a file-like object that implements :meth:`read` or a string.
565
+ - `chunk_size_bytes` (options): The number of bytes per chunk of this
566
+ file. Defaults to the chunk_size_bytes of :class:`GridFSBucket`.
567
+ - `metadata` (optional): User data for the 'metadata' field of the
568
+ files collection document. If not provided the metadata field will
569
+ be omitted from the files collection document.
570
+ """
571
+ with self .open_upload_stream_with_id (
572
+ file_id , filename , chunk_size_bytes , metadata ) as gin :
573
+ gin .write (source )
574
+
491
575
def open_download_stream (self , file_id ):
492
576
"""Opens a Stream from which the application can read the contents of
493
577
the stored file specified by file_id.
0 commit comments