问题
Basically, I have set an application with the MediaDevices.getUserMedia WebRTC API. I have recorded the audios and all, all is good in theory. But I got to retrieve the audio now to listen to them.
I wonder now how to download audio Post in my MongoDB database. I have tried to export my MongoDB database but I receive only JSON or CSV file, not my audios files.
I have heard about gridFS but only in image-handling context. Before dive deeply into gridFS I would get your thinks about audio retrieving with Mlab. and MongoDB more widely. Also, gridFS seems to be designed for large files, but in my case, I just want to store ridiculously little files of some hundreds of bytes, so gridFS seems maybe overkilled? Maybe there is a more efficient solution?
EDIT : I struggle to translate my data in order to store it in my database.
So far, my console return me :
XML Parsing Error: syntax error
Here my App.js :
// post section
async handleSubmit(e){
e.preventDefault();
Axios.post("/api/words",{
"sound":this.state.blob
})
//.then((res) => res.json())
.then((data) => console.log(data))
//pass submitted value to true in order to declench allDelete function
}
(...)
// blob formatting section :
saveAudio() {
// convert saved chunks to blob
const blob = new Blob(this.chunks, {type: audioType});
this.setState({blob : blob})
Thanks.
回答1:
You don't need GridFS for files smaller than 16 MB. You can either store binary data directly in MongoDB using BSON's binary type or encode your binary data and store it as a string. Base64 is a common encoding choice when storing binary data as strings.
Once you retrieve the data from the database, you can write it to a file using fs.writeFile.
If you saved your data as a binary type, you can pass a buffer to fs.writeFile
. If you saved your data as an encoded string, you can pass a string and an encoding
option.
(If you wanted to serve the file with Express, you can set the content type and send the data using res.send)
Here's a small example in NodeJS. This reads an audio file from disk, saves it to a MongoDB database as a binary type (using the MongoDB driver's Binary class), retrieves it back from the database, and writes it to a new file on disk.
const mongodb = require('mongodb')
const util = require('util')
const fs = require('fs')
const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile)
async function main() {
const client = await mongodb.MongoClient.connect(process.env.MONGO_URI)
console.log('connected')
let db = await client.db('dbname')
// Reading in binary data from a file. data is a buffer.
let data = await readFile(__dirname + '/sample.mp3')
// Insert binary data to the database
let res = await db.collection('coll').insert({data: new mongodb.Binary(data)})
console.log(res)
let objectId = res.ops[0]._id
// Retrieve binary data from the database
let obj = await db.collection('coll').findOne({_id: objectId})
console.log(obj)
// *** This is the key part ***
// use obj.data.read to get a buffer from the binary data and write that buffer to a file
await writeFile(__dirname + '/out.mp3', obj.data.read(0, obj.data.length()))
console.log('done')
}
main()
Although people do store binary data in the database, it's probably most common for people to store files in a file system or in object storage (such as Amazon S3). Then they would just store a link to that file in the database and retrieve that file using the link. You can do whichever you feel more comfortable with.
来源:https://stackoverflow.com/questions/50535169/retrieve-audio-binary-file-stored-in-my-mlab