public ObjectId UploadToStream(byte[] source, string fileName, GridFSUploadOptions options = null) { MongoClient client = GetClient(m_connectionStr); var db = client.GetDatabase(DatabaseName); var bucket = new GridFSBucket(db, BucksOptions); using (var stream = bucket.OpenUploadStream(fileName, options)) { stream.Write(source, 0, source.Count()); return(stream.Id); } }
public MongoWriteContentProvider(GridFSBucket bucket, string label) { this.stream = bucket.OpenUploadStream(label); }
/// <summary> /// these information come from: rm.Stream /// - FormatInfo.BlobId /// - Filename.name /// - Filename.ext /// </summary> /// <param name="fileId"></param> /// <param name="dateTimeUploadFilter">I do not want to sync blob that are more recent than this /// value, to avoid syncing blob while they are handled by DS</param> public Boolean Sync( string fileId, ArtifactSyncJobConfig config, DateTime?dateTimeUploadFilter = null) { var cfg = _configurator.GetConfiguration(fileId, config); // initialize Mongo Databases IGridFSBucket <string> sourceBucket; MongoUrl sourceMongoUrl; IGridFSBucket <string> destinationBucket; MongoUrl destinationMongoUrl; IMongoDatabase sourceDatabase; sourceMongoUrl = new MongoUrl(cfg.SourceConnectionString); sourceDatabase = new MongoClient(sourceMongoUrl).GetDatabase(sourceMongoUrl.DatabaseName); sourceBucket = new GridFSBucket <string>(sourceDatabase, new GridFSBucketOptions { BucketName = cfg.Bucket, ChunkSizeBytes = 1048576, // 1MB }); IMongoDatabase destinationDatabase; destinationMongoUrl = new MongoUrl(cfg.DestConnectionString); destinationDatabase = new MongoClient(destinationMongoUrl).GetDatabase(destinationMongoUrl.DatabaseName); IMongoCollection <BsonDocument> destinationCollection = destinationDatabase.GetCollection <BsonDocument>(cfg.Bucket + ".files"); destinationBucket = new GridFSBucket <string>(destinationDatabase, new GridFSBucketOptions { BucketName = cfg.Bucket, ChunkSizeBytes = 1048576, // 1MB }); // before uploading the new element check if it's already in the destination database (maybe it's an alias) var findIdFilter = Builders <GridFSFileInfo <string> > .Filter.Eq(x => x.Id, fileId); using (var cursor = destinationBucket.Find(findIdFilter)) { var exists = cursor.FirstOrDefault(); if (exists != null) { return(true); //Already synced, true } } var source = sourceBucket.Find(findIdFilter).FirstOrDefault(); if (source == null) { return(false); //source stream does not exists } if (dateTimeUploadFilter.HasValue && source.UploadDateTime > dateTimeUploadFilter) { return(false); //Consider this as not existing. } var sw = Stopwatch.StartNew(); Console.WriteLine("Sync needed Tenant {0}/{1}: ", config.Tenant, fileId); GridFSUploadOptions options = new GridFSUploadOptions(); options.ChunkSizeBytes = source.ChunkSizeBytes; options.ContentType = source.ContentType; using (var destinationStream = destinationBucket.OpenUploadStream(fileId, source.Filename, options)) { sourceBucket.DownloadToStream(fileId, destinationStream); } destinationCollection.UpdateOne( Builders <BsonDocument> .Filter.Eq("_id", fileId), Builders <BsonDocument> .Update.Set("uploadDate", source.UploadDateTime) ); Console.WriteLine("DONE {0}/{1} ({2} ms)", config.Tenant, fileId, sw.ElapsedMilliseconds); sw.Stop(); return(true); }