猿问

从 AWS S3 下载日志文件恐慌:运行时错误:

我要下载特定存储桶中的所有日志文件(最终是我拥有的每个存储桶),这是我正在使用的代码


package main


import (

    "fmt"

    "os"

    "path/filepath"


    "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"

)


var (

    // variables empty for security

    Bucket         = ""                                               // Download from this bucket

    Prefix         = "" // Using this key prefix

    LocalDirectory = "s3logs"                                                    // Into this directory

)


func main() {


    client := s3.New(session.New(), &aws.Config{Region: aws.String("us-west-1")})

    params := &s3.ListObjectsInput{Bucket: &Bucket, Prefix: &Prefix}


    manager := s3manager.NewDownloader(client)

    d := downloader{bucket: Bucket, dir: LocalDirectory, Downloader: manager}


    client.ListObjectsPages(params, d.eachPage)


}


type downloader struct {

    *s3manager.Downloader

    bucket, dir string

}


func (d *downloader) eachPage(page *s3.ListObjectsOutput, more bool) bool {

    for _, obj := range page.Contents {

        d.downloadToFile(*obj.Key)

    }


    return true

}


func (d *downloader) downloadToFile(key string) {

    // Create the directories in the path

    file := filepath.Join(d.dir, key)


    if err := os.MkdirAll(filepath.Dir(file), 0775); err != nil {

        panic(err)

    }

    fmt.Printf("Downloading " + key)

    // Setup the local file

    fd, err := os.Create(file)

    if err != nil {

        panic(err)

    }


    defer fd.Close()


    // Download the file using the AWS SDK

    fmt.Printf("Downloading s3://%s/%s to %s...\n", d.bucket, key, file)

    params := &s3.GetObjectInput{Bucket: &d.bucket, Key: &key}

    d.Download(fd, params)

}

但是,当我运行此代码时,我收到一个恐慌错误


不能在 s3manager.NewDownloader 的参数中使用客户端(类型 *s3.S3)作为类型 client.ConfigProvider:*s3.S3 没有实现 client.ConfigProvider(缺少 ClientConfig 方法)


我不知道此代码将不起作用,任何想法和/或修复


ibeautiful
浏览 179回答 1
1回答

饮歌长啸

你正在传递nil到s3manager.NewDownloader需要一个Sessionsess := session.New() manager := s3manager.NewDownloader(sess)
随时随地看视频慕课网APP

相关分类

Go
我要回答