英文:
Stream media file from minio object
问题
我有一个后端的Go Fiber服务器,它使用Minio客户端SDK从云服务中获取文件。我的目标是实现视频文件流,而不暴露直接连接到云端URL,并且视频必须可前进和可后退(与云端URL的功能相同)。然而,当我添加从服务器发送的流时,视频就无法进行定位,有时只能进行后退定位。我尝试设置响应的Content-Length、Content-Type、Content-Range等等,但都没有起作用。注意:所有这些行为都发生在最新稳定版本的Chrome中,而不是Firefox或Postman。以下是我的代码:
// c 是 *fiber.Ctx
object, err := minioClient.GetObject(context.Background(), bucketName, fileName, minio.GetObjectOptions{})
if err != nil {
return err
}
objInfo, err := object.Stat()
if err != nil {
return err
}
buffer := make([]byte, objInfo.Size)
object.Read(buffer)
c.Set("Content-Length", fmt.Sprintf("%d", objInfo.Size))
c.Set("Content-Type", "video/mp4")
c.Set("Connection", "keep-alive")
c.Set("Content-Range", fmt.Sprintf("bytes 0-%d/%d", objInfo.Size, objInfo.Size))
c.Response().SetBodyStream(bytes.NewReader(buffer), int(objInfo.Size))
return nil
// c.SendStream 就是无法工作
希望对你有帮助!
英文:
I have a backend go fiber server which would serve files fetched from cloud service using minio client sdk. My goal is to achieve video file streaming with out exposing direct connection to cloud url and the video must be seekable, both forward and backward (same functionality as cloud's url). However, when I add the stream sent from the server, the video just can not be seeked or sometime only backwards seek. I tried to set the response's Content-Length, Content-Type, Content-Range, etc... and it just won't work. Notes: all behaviors happened in chrome latest stable version and not firefox or postman. Here is my code:
// c is the *fiber.Ctx
object, err := minioClient.GetObject(context.Background(), bucketName, fileName, minio.GetObjectOptions{})
if err != nil {
return err
}
objInfo, err := object.Stat()
if err != nil {
return err
}
buffer := make([]byte, objInfo.Size)
object.Read(buffer)
c.Set("Content-Length", fmt.Sprintf("%d", objInfo.Size))
c.Set("Content-Type", "video/mp4")
c.Set("Connection", "keep-alive")
c.Set("Content-Range", fmt.Sprintf("bytes 0-%d/%d", objInfo.Size, objInfo.Size))
c.Response().SetBodyStream(bytes.NewReader(buffer), int(objInfo.Size))
return nil
//c.SendStream just won't work
答案1
得分: 1
我找到了解决方案。原来我只需要添加 Accept-Ranges 头部信息,哈哈。
英文:
I have found the solution. Turn out I only need to add the Accept-Ranges header lol.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论