为了账号安全,请及时绑定邮箱和手机立即绑定

使用 golang 的 AWS S3 并行下载

使用 golang 的 AWS S3 并行下载

Go
扬帆大鱼 2023-06-05 09:18:45
我正在编写一个函数,使用 aws-sdk for go 从 AWS S3 存储桶下载一个大文件 (9GB)。我需要对此进行优化并快速下载文件。func DownloadFromS3Bucket(bucket, item, path string) {    os.Setenv("AWS_ACCESS_KEY_ID", constants.AWS_ACCESS_KEY_ID)    os.Setenv("AWS_SECRET_ACCESS_KEY", constants.AWS_SECRET_ACCESS_KEY)    file, err := os.Create(filepath.Join(path, item))    if err != nil {        fmt.Printf("Error in downloading from file: %v \n", err)        os.Exit(1)    }    defer file.Close()    sess, _ := session.NewSession(&aws.Config{        Region: aws.String(constants.AWS_REGION)},    )    downloader := s3manager.NewDownloader(sess)    numBytes, err := downloader.Download(file,        &s3.GetObjectInput{            Bucket: aws.String(bucket),            Key:    aws.String(item),        })    if err != nil {        fmt.Printf("Error in downloading from file: %v \n", err)        os.Exit(1)    }    fmt.Println("Download completed", file.Name(), numBytes, "bytes")}有人可以建议扩展此功能的解决方案。
查看完整描述

1 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

尝试将您的 NewDownLoader() 更改为此。

// Create a downloader with the session and custom options
downloader := s3manager.NewDownloader(sess, func(d *s3manager.Downloader) {
     d.PartSize = 64 * 1024 * 1024 // 64MB per part
     d.Concurrency = 4})

可以用 d 设置的选项列表。

查看完整回答
反对 回复 2023-06-05
  • 1 回答
  • 0 关注
  • 168 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信