nodejs获取音频文件
我已经在gridfs中存储了一些音频文件,并且可以进行find()查询以获取它们。
但是如何获得它们,以便可以从这些文件中流式传输音频?
import clientPromise from "../../lib/mongodb";
import { MongoClient, GridFSBucket } from 'mongodb'
var CryptoJS = require("crypto-js");
//import { hash } from 'bcryptjs';
export default async function audio(req, res) {
const uri = process.env.MONGODB_URI
let client
let clientPromise
const options = {}
client = new MongoClient(uri, options)
clientPromise = client.connect()
const clients = await clientPromise
const database = clients.db('AUDIODB');
const bucket = new GridFSBucket(database,{bucketName:"uploads"});
const { id } = req.query;
const cursor = bucket.find({});
cursor.forEach(doc => console.log(doc)); //THIS SHOWS ME THE FILES UPLOADED
}
export async function getServerSideProps(context) {
try {
// client.db() will be the default database passed in the MONGODB_URI
// You can change the database by calling the client.db() function and specifying a database like:
// const db = client.db("myDatabase");
// Then you can execute queries against your database like so:
// db.find({}) or any of the MongoDB Node Driver commands
await clientPromise
return {
props: { isConnected: true },
}
} catch (e) {
console.error(e)
return {
props: { isConnected: false },
}
}
}
从上面可以看到,我将其显示我上传的所有文件,
cursor.forEach(doc => console.log(doc)); //THIS SHOWS ME THE FILES UPLOADED
但是这只是我的以下内容
{
_id: new ObjectId("6267ba6dd2512c3257f30980"),
filename: '/Volumes/Studio/AUDIOFILES/2022/AUDIOPATH/AUDIO.mp3',
length: 168906,
chunkSize: 261120,
uploadDate: 2022-04-26T09:25:05.589Z
}
,但显然我无法放置
<audio src="FILENAME"/>
我的服务器,因为我的服务器没有托管上传的文件。
I have stored some audio files in GridFS, and I am able to do a find() query to fetch them.
But how do I get them so I can stream the audio from these files?
import clientPromise from "../../lib/mongodb";
import { MongoClient, GridFSBucket } from 'mongodb'
var CryptoJS = require("crypto-js");
//import { hash } from 'bcryptjs';
export default async function audio(req, res) {
const uri = process.env.MONGODB_URI
let client
let clientPromise
const options = {}
client = new MongoClient(uri, options)
clientPromise = client.connect()
const clients = await clientPromise
const database = clients.db('AUDIODB');
const bucket = new GridFSBucket(database,{bucketName:"uploads"});
const { id } = req.query;
const cursor = bucket.find({});
cursor.forEach(doc => console.log(doc)); //THIS SHOWS ME THE FILES UPLOADED
}
export async function getServerSideProps(context) {
try {
// client.db() will be the default database passed in the MONGODB_URI
// You can change the database by calling the client.db() function and specifying a database like:
// const db = client.db("myDatabase");
// Then you can execute queries against your database like so:
// db.find({}) or any of the MongoDB Node Driver commands
await clientPromise
return {
props: { isConnected: true },
}
} catch (e) {
console.error(e)
return {
props: { isConnected: false },
}
}
}
as you can see from above, I have it showing all the files that I have uploaded
cursor.forEach(doc => console.log(doc)); //THIS SHOWS ME THE FILES UPLOADED
However this just gets me the following
{
_id: new ObjectId("6267ba6dd2512c3257f30980"),
filename: '/Volumes/Studio/AUDIOFILES/2022/AUDIOPATH/AUDIO.mp3',
length: 168906,
chunkSize: 261120,
uploadDate: 2022-04-26T09:25:05.589Z
}
But clearly I can't put
<audio src="FILENAME"/>
as my server does not host the uploaded file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论