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

通过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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀