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

为何aws-sdk-go中动态内存存储时S3 GetObject耗时远超预期?

S3文件下载耗时优化记录

问题现象

经测试,从Fargate任务下载S3中6MB文件耗时约1.3秒,而1.7MB文件仅需150毫秒。按线性增长计算,6MB文件最多应耗时约600毫秒,但实际耗时远高于预期。

原实现代码

buff := aws.NewWriteAtBuffer([]byte{})
getObjectInput := &s3.GetObjectInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(uri),
}
_, err = u.downloader.DownloadWithContext(ctx, buff, getObjectInput)

问题分析

调试后发现原代码存在内存占用过高的问题,这是导致下载耗时异常的核心原因。

优化后代码

getHeaderInput := &s3.HeadObjectInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(uri),
}
res, _ := u.s3.HeadObject(getHeaderInput)
buff := aws.NewWriteAtBuffer(make([]byte, 0, *res.ContentLength))
getObjectInput := &s3.GetObjectInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(uri),
}
_, err = u.downloader.DownloadWithContext(ctx, buff, getObjectInput)

优化效果

修改后6MB文件下载耗时降至约600毫秒,符合线性增长预期,整体耗时明显缩短。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 04:21:03