doushifang4382 2015-12-17 22:38
浏览 77
已采纳

从AWS S3紧急状态下载日志文件:运行时错误:

I'm to download all the log files in a particular bucket (eventually every bucket I have), here is the code I'm using

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...
", d.bucket, key, file)
    params := &s3.GetObjectInput{Bucket: &d.bucket, Key: &key}
    d.Download(fd, params)
}

However when I run this code I receive a panic error

cannot use client (type *s3.S3) as type client.ConfigProvider in argument to s3manager.NewDownloader: *s3.S3 does not implement client.ConfigProvider (missing ClientConfig method)

I have no idea what this code won't work, any thoughts and or fixes

  • 写回答

1条回答 默认 最新

  • doudao1950 2015-12-17 22:59
    关注

    You're passing nil to s3manager.NewDownloader where it requires a Session

    sess := session.New()
    manager := s3manager.NewDownloader(sess)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 windows2019+nginx+php耗时久
  • ¥15 labelme打不开怎么办
  • ¥35 按照图片上的两个任务要求,用keil5写出运行代码,并在proteus上仿真成功,🙏
  • ¥15 免费的电脑视频剪辑类软件如何盈利
  • ¥30 MPI读入tif文件并将文件路径分配给各进程时遇到问题
  • ¥15 pycharm中导入模块出错
  • ¥20 Ros2 moveit2 Windows环境配置,有偿,价格可商议。
  • ¥15 有关“完美的代价”问题的代码漏洞
  • ¥15 请帮我看一下这个简易化学配平器的逻辑有什么问题吗?
  • ¥15 暴力法无法解出,可能要使用dp和数学知识