You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求io.MultiReader替代方案:多文件片段复制至非追加S3对象

问题:寻找io.MultiReader替代方案实现多文件片段一次性复制(S3目标场景)

问题背景

  • 需要一次性复制多个文件片段到目标S3对象,但S3对象不支持追加数据
  • 使用io.MultiReader配合io.Copy时,最终目标文件仅包含最后一段内容
  • 预先合并文件片段再传输耗时过长,不具备可行性

原代码示例

...
dstFile, err := fs.OpenFile(ctx, dst, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
readers := make([]io.Reader, 0, len(children))
for _, item := range children {
    source := path.Join(dir, name)
    srcFile, err := fs.OpenFile(ctx, source, os.O_RDONLY, 0)
    readers = append(readers, srcFile)
}
multiSources := io.MultiReader(readers...)
writtenBytes, err := io.Copy(dstFile, multiSources)
...

解决方案

使用io.TeeReader包装io.MultiReader的输出,再通过io.Copy写入目标文件,即可将所有文件片段一次性写入:

myReader := io.TeeReader(multiSources, io.Discard)
_, err = io.Copy(dstFile, myReader)

说明:必须使用io.Copy是因为目标文件对象未实现标准Writer接口

内容的提问来源于stack exchange,提问作者Jaime Silva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 02:03:10