为何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
相关产品推荐
相关产品推荐

