从Minio对象流式传输媒体文件。

huangapple go评论84阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年6月15日 17:00:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/72628570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定