public async void DownloadToStreamAsync(ObjectId id, Stream destinationStream, GridFSDownloadOptions options = null) { MongoClient client = GetClient(m_connectionStr); var db = client.GetDatabase(DatabaseName); var bucket = new GridFSBucket(db, BucksOptions); await bucket.DownloadToStreamAsync(id, destinationStream, options); }
public async Task DownloadToStreamAsync(ObjectId id, Stream source, MediaTypeEnum bucketName) { var bucket = new GridFSBucket(_mongoDatabase, new GridFSBucketOptions { BucketName = bucketName.ToString() }); await bucket.DownloadToStreamAsync(id, source); }
public async Task <Stream> GetGifContent(dynamic id) { var fid = new ObjectId(id); var stream = new MemoryStream(); await _DMIGifs.DownloadToStreamAsync(fid, stream); stream.Seek(0, SeekOrigin.Begin); return(stream); }
private static async Task MigrateGridFS( IMongoDatabase database, JsonTextWriter jsonTextWriter, StreamWriter streamWriter) { var bucket = new GridFSBucket(database); var collectionName = bucket.Options.BucketName == "fs" ? "Files" : bucket.Options.BucketName; var filter = Builders <GridFSFileInfo> .Filter.Empty; var attachmentNumber = 0; using (var cursor = await bucket.FindAsync(filter)) { while (await cursor.MoveNextAsync()) { foreach (var fileInfo in cursor.Current) { var document = ToExpandoObject(fileInfo.Metadata); var dictionary = (IDictionary <string, object>)document; var documentId = fileInfo.Id.ToString(); var totalSize = fileInfo.Length; var contentType = GetContentType(fileInfo, dictionary); using (var stream = new MemoryStream()) { //can't OpenDownloadStreamAsync because it's not a seeakble stream await bucket.DownloadToStreamAsync(fileInfo.Id, stream); stream.Position = 0; attachmentNumber++; var attachmentInfo = await MigrationHelpers.WriteAttachment( stream, totalSize, documentId, collectionName, contentType, attachmentNumber, jsonTextWriter, streamWriter); MigrationHelpers.WriteDocument( document, documentId, collectionName, null, jsonTextWriter, new List <Dictionary <string, object> > { attachmentInfo }); } } } } }
private async Task StreamDownload(StreamDownloadMessage message) { var elkUrl = new Uri("http://*****:*****@"C:\files\", message.FileName))) { await bucket.DownloadToStreamAsync(message.DocId, fileStream); } stopWatch.Stop(); Console.WriteLine($"The {message.FileName} file was downloaded. Elapsed time: {stopWatch.ElapsedMilliseconds} ms."); }
public async Task ReadFile(int id, string consumer, Stream stream) { var client = GetClient(); using (var session = await client.StartSessionAsync()) { var collection = client.GetDatabase(_url.DatabaseName).GetCollection <FileDocument>(CollectionName); var cursor = await collection.FindAsync(Builders <FileDocument> .Filter.And( Builders <FileDocument> .Filter.Eq(x => x.Id, id), Builders <FileDocument> .Filter.Eq(x => x.Consumer, consumer))); var filedoc = await cursor.SingleAsync(); var bucket = new GridFSBucket(client.GetDatabase(_url.DatabaseName), new GridFSBucketOptions { BucketName = "Uploads" }); await bucket.DownloadToStreamAsync(filedoc.FileName, stream); } }
public async Task <IActionResult> Get(String imgId) { try { ObjectId oid = new ObjectId(imgId); Byte[] result; using (var d = new MemoryStream()) { await _imagesBucket.DownloadToStreamAsync(oid, d); result = d.ToArray(); } return(File(result, "image/jpeg")); } catch (Exception ex) { return(Json(ex.RaiseException())); } }
public static async Task DownloadFileToStream() { var database = DatabaseHelper.GetDatabaseReference("localhost", "file_store"); IGridFSBucket bucket = new GridFSBucket(database); var filter = Builders <GridFSFileInfo <ObjectId> > .Filter.Eq(x => x.Filename, "sample2.pdf"); var searchResult = await bucket.FindAsync(filter); var fileEntry = searchResult.FirstOrDefault(); var fileName = "c:\\temp\\mystream.pdf"; using (Stream fileStream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write)) { await bucket.DownloadToStreamAsync(fileEntry.Id, fileStream); fileStream.Close(); } }
public async Task <bool> DownloadFileAsync(ObjectId id, Stream OutStrem, string bucketname) { var bucket = new GridFSBucket(database, new GridFSBucketOptions { ChunkSizeBytes = 64512, BucketName = bucketname }); var filter = Builders <GridFSFileInfo <ObjectId> > .Filter.Where(x => x.Id == id); var cursor = await bucket.FindAsync(filter); var result = await cursor.ToListAsync(); if (result.Count == 0) { return(false); } await bucket.DownloadToStreamAsync(id, OutStrem); return(true); }
public async Task <bool> DownloadFileAsync(ObjectId id, string outpath, string bucketname) { var bucket = new GridFSBucket(database, new GridFSBucketOptions { ChunkSizeBytes = 64512, BucketName = bucketname }); var filter = Builders <GridFSFileInfo <ObjectId> > .Filter.Where(x => x.Id == id); var cursor = await bucket.FindAsync(filter); var result = await cursor.ToListAsync(); if (result.Count == 0) { return(false); } using (var outStream = new FileStream(outpath, FileMode.OpenOrCreate, FileAccess.Write)) { await bucket.DownloadToStreamAsync(id, outStream); } return(true); }
public async Task GetOneAsync(ObjectId id, Stream outStream) => await _mediaBucket.DownloadToStreamAsync(id, outStream);
/// <summary> /// Downloads a file stored in GridFS and writes the contents to a stream. /// </summary> /// <param name="id"></param> /// <param name="destination"></param> /// <param name="options"></param> /// <returns></returns> public Task DownloadToStreamAsync(ObjectId id, Stream destination, GridFSDownloadOptions options = null) { var bucket = new GridFSBucket(base.GetDatabase(), BucketOptions); return(bucket.DownloadToStreamAsync(id, destination, options)); }