Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3204

Support setting GridFS metadata at the end of uploading to a stream

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: GridFS
    • None
    • Fully Compatible

      Currently, when uploading to a stream using the sample code provided such as:

      using (var stream = bucket.OpenUploadStream("filename", options))
      {
          var id = stream.Id; // the unique Id of the file being uploaded
          // write the contents of the file to stream
          stream.Close();
      }
      

      The metadata document needs to be known ahead of time. However, there may be scenarios where it could be useful to set/modify the metadata after the upload of the file itself is complete. The primary example is storing the hash of a file being created on the fly where the value is not known until the file is done being written. Another example might be to store the duration of the time it took to upload the file.

      I fully recognize that nothing stops us from updating the metadata after the fact but this results in another round trip to the database that is not needed since the current implementation doesn't create the files document until after the upload already. My suggestion would be an overload to OpenUploadStream and OpenUploadStreamAsync that would allow for a callback to update the metadata:

      public GridFSUploadStream<TFileId> OpenUploadStream(TFileId id, string filename, GridFSUploadOptions options = null, Func<BsonDocument, BsonDocument> meatdataCallback = null, CancellationToken cancellationToken = default(CancellationToken))
      

      This callback lambda would get the original metadata BsonDocument passed to it and would return the final metadata BsonDocument to be stored. This would of course have to be carried through to GridFsForwardOnlyStream and called (if provided) during CreateFilesCollectionDocument() (which already doesn't get called until the end anyhow so I don't think this would be too difficult).

            Assignee:
            Unassigned Unassigned
            Reporter:
            ttutko@gmail.com Thomas Tutko
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: