douzi7219 2018-03-13 21:58
浏览 256

在Golang中从AWS S3读取文件

I am trying to deploy a golang code on Heroku. My code needs a text file as input and I need to fetch this text file from S3 bucket. My go-code takes the filename as input, Can someone provide a code snippet for reading a file from S3 and storing its contents into a file?

My GOlang code-

  func getDomains(path string) (lines []string, Error error) {

    file, err := os.Open(path)
    if err != nil {
        log.Fatalln(err)
    }

    defer file.Close()

    scanner := bufio.NewScanner(file)

    for scanner.Scan() {
        lines = append(lines, scanner.Text())
    }

    return lines, scanner.Err()
}

func Process(w http.ResponseWriter, r *http.Request) {


    urls := make(chan *Http, Threads*10)
    list, err := getDomains("**NEED A TEXT FILE FROM S3 HERE as an argument**")
    if err != nil {
        log.Fatalln(err)
    }

    var wg sync.WaitGroup
    for i := 0; i < Threads; i++ {
        wg.Add(1)
        go func() {
            for url := range urls {
                url.DNS()
            }

            wg.Done()
        }()
    }

    for i := 0; i < len(list); i++ {
        Progress := fmt.Sprintln(w, len(list))
        urls <- &Http{Url: list[i], Num: Progress}
    }

    close(urls)

    wg.Wait()

    fmt.Printf("%s", strings.Repeat(" ", 100))
    fmt.Fprintln(w, "Task completed.
")
}

Can someone suggest a good library for reading the file from S3 into a text file? I cannot download the file from S3 because I have to deploy it on Heroku.

A code snippet for example will be highly appreciated!

  • 写回答

2条回答 默认 最新

  • dqjjw04440 2018-03-14 05:13
    关注

    The code snippet below should work (given that you have installed the proper dependencies):

        package main
    
        import (
            "github.com/aws/aws-sdk-go/aws"
            "github.com/aws/aws-sdk-go/aws/session"
            "github.com/aws/aws-sdk-go/service/s3"
            "github.com/aws/aws-sdk-go/service/s3/s3manager"
    
            "fmt"
            "log"
            "os"
        )
    
        func main() {
            // NOTE: you need to store your AWS credentials in ~/.aws/credentials
    
            // 1) Define your bucket and item names
            bucket := "<YOUR_BUCKET_NAME>"
            item   := "<YOUR_ITEM_NAME>"
    
            // 2) Create an AWS session
            sess, _ := session.NewSession(&aws.Config{
                    Region: aws.String("us-west-2")},
            )
    
            // 3) Create a new AWS S3 downloader 
            downloader := s3manager.NewDownloader(sess)
    
    
            // 4) Download the item from the bucket. If an error occurs, log it and exit. Otherwise, notify the user that the download succeeded.
            file, err := os.Create(item)
            numBytes, err := downloader.Download(file,
                &s3.GetObjectInput{
                    Bucket: aws.String(bucket),
                    Key:    aws.String(item),
            })
    
            if err != nil {
                log.Fatalf("Unable to download item %q, %v", item, err)
            }
    
            fmt.Println("Downloaded", file.Name(), numBytes, "bytes")
    
        }
    

    For more details you can check the AWS Go SDK and the Github Example

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料