登录
首页 >  Golang >  Go问答

实现并行上传多个文件到 Amazon S3 的方法

来源:stackoverflow

时间:2024-03-12 14:21:26 295浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《实现并行上传多个文件到 Amazon S3 的方法》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

我正在尝试将目录上传到 amazon s3 存储桶中。然而,上传目录的唯一方法是遍历该目录内的所有文件并逐个上传。

我正在使用 go 来迭代目录中的文件。然而,对于我迭代的每个文件,我想分离一个 goroutine 来上传文件,同时主线程迭代目录中的下一个元素并分离另一个 goroutine 来上传相同的文件。

知道如何使用 goroutines 和 channels 并行上传目录中的所有文件吗?

修改后的代码片段实现了一个 goroutine 和一个并行上传文件的通道。但我不确定这是否是正确的实现。

func uploadDirToS3(dir string, svc *s3.S3) {
    fileList := []string{}
    filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
        fmt.Println("PATH ==> " + path)
        fileList = append(fileList, path)
        return nil
    })
    for _, pathOfFile := range fileList[1:] {
        channel := make(chan bool)
        go uploadFiletoS3(pathOfFile, svc, channel)
        <-channel
    }
}

func uploadFiletoS3(path string, svc *s3.S3, channel chan bool) {
    file, err := os.Open(path)
    if err != nil {
        fmt.Println(err)
    }
    defer file.Close()
    fileInfo, _ := file.Stat()
    size := fileInfo.Size()

    buffer := make([]byte, size)
    file.Read(buffer)
    fileBytes := bytes.NewReader(buffer)
    fileType := http.DetectContentType(buffer)

    s3Path := file.Name()

    params := &s3.PutObjectInput{
        Bucket:        aws.String("name-of-bucket"),
        Key:           aws.String(s3Path),
        Body:          fileBytes,
        ContentLength: aws.Int64(size),
        ContentType:   aws.String(fileType),
    }

    resp, err := svc.PutObject(params)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("response %s", awsutil.StringValue(resp))
    close(channel)
}

关于如何更好地实现这一点有什么想法吗?我研究过 waitgroups,但由于某种原因,我发现 channels 在这种情况下更容易理解和实现。


解决方案


所以,您正在寻找并发性,它植根于 go 指令。对于循环中启动的 goroutine 之间的同步,可以使用 chanelssync.WaitGroup。第二个选项更容易做到。 此外,您还必须重构您的函数并将内部 for 逻辑移至单独的函数中。

func uploadDirToS3(dir string, svc *s3.S3) {
    fileList := []string{}
    filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
        fileList = append(fileList, path)
        return nil
    })
    var wg sync.WaitGroup
    wg.Add(len(fileList))
    for _, pathOfFile := range fileList[1:] {
        //maybe spin off a goroutine here??
        go putInS3(pathOfFile, svc, &wg)
    }
    wg.Wait()
}

func putInS3(pathOfFile string, svc *s3.S3, wg *sync.WaitGroup) {
    defer func() {
        wg.Done()
    }()
    file, _ := os.Open(pathOfFile)
    defer file.Close()
    fileInfo, _ := file.Stat()
    size := fileInfo.Size()
    buffer := make([]byte, size)
    file.Read(buffer)
    fileBytes := bytes.NewReader(buffer)
    fileType := http.DetectContentType(buffer)
    path := file.Name()
    params := &s3.PutObjectInput{
        Bucket:        aws.String("bucket-name"),
        Key:           aws.String(path),
        Body:          fileBytes,
        ContentLength: aws.Int64(size),
        ContentType:   aws.String(fileType),
    }

    resp, _ := svc.PutObject(params)
    fmt.Printf("response %s", awsutil.StringValue(resp))
}

以上就是《实现并行上传多个文件到 Amazon S3 的方法》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>