douwuli4512 2016-05-26 19:57
浏览 74
已采纳

通过Golang SDK将大文件发送到Amazon S3时如何使用较少的内存?

I'm using AWS SDK on golang to send large backup files (2GB~10GB).

When the process starts there is a huge memory consumption. I know that this is because the code reads the file to a buffer, but I'm really new to Go, and I don't know how can I change this.

This is the code I'm using to read the file and send to AWS S3 Uploader:

file, err := os.Open(file_to_upload)

file_name := getFileName(file_to_upload)

if err != nil {
    fmt.Println(err)
    os.Exit(1)
}
defer file.Close()

fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()

buffer := make([]byte, size)

file.Read(buffer)

fileBytes := bytes.NewReader(buffer)

fileType := http.DetectContentType(buffer)

upparams := &s3manager.UploadInput{
    Bucket: &bucket,
    Key:    &file_name,
    Body:   fileBytes,
    ACL:    aws.String("private"),
    ContentType:   aws.String(fileType),
    Metadata: map[string]*string{
        "Key": aws.String("MetadataValue"), //required
    },
}

result, err := uploader.Upload(upparams, func(u *s3manager.Uploader){
    if partsize != 0{
        u.PartSize = int64(partsize) * 1024 * 1024
    }
    u.LeavePartsOnError = false
    u.Concurrency = parallel
})

```

What I've tested so far.

Modification: Changed the u.Concurrency from 5 to 3: Outcome: CPU: Reduced the from 26% to 21% Memory: Same usage

Modification: Changed the u.Concurrency from 5 to 2: Outcome: CPU: Reduced the from 26% to 20% Memory: Same usage

Modification: Changed the u.Concurrency from 5 to 3 and u.Partsize to 100MB: Outcome: CPU: Reduced the from 26% to 16% Memory: Same usage

The time isn't the problem here but the memory consumption.

I want to tune this for the least resources possible. How can I approach to that?

  • 写回答

1条回答 默认 最新

  • douguachi0056 2016-05-26 20:13
    关注

    There's no reason to read the entire file into memory. Just provide the Body field with the file itself

    upparams := &s3manager.UploadInput{
        Bucket: &bucket,
        Key:    &file_name,
    
        // *os.File is an io.Reader
        Body:   file,
    
        ACL:    aws.String("private"),
        ContentType:   aws.String(fileType),
        Metadata: map[string]*string{
            "Key": aws.String("MetadataValue"), //required
        },
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大