nodejs获取音频文件

发布于 2025-01-24 05:09:23 字数 2165 浏览 1 评论 0原文

我已经在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文