从AWS S3下载日志文件时发生紧急错误:运行时错误。

huangapple go评论84阅读模式
英文:

Download Log Files from AWS S3 panic: runtime error:

问题

我将为您翻译以下代码:

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         = ""                                               // 从这个存储桶下载
	Prefix         = "" // 使用这个键前缀
	LocalDirectory = "s3logs"                                                    // 下载到这个目录
)

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) {
	// 创建路径中的目录
	file := filepath.Join(d.dir, key)

	if err := os.MkdirAll(filepath.Dir(file), 0775); err != nil {
		panic(err)
	}
	fmt.Printf("正在下载 " + key)
	// 设置本地文件
	fd, err := os.Create(file)
	if err != nil {
		panic(err)
	}

	defer fd.Close()

	// 使用 AWS SDK 下载文件
	fmt.Printf("正在下载 s3://%s/%s 到 %s...\n", d.bucket, key, file)
	params := &s3.GetObjectInput{Bucket: &d.bucket, Key: &key}
	d.Download(fd, params)
}

然而,当我运行这段代码时,我收到一个 panic 错误:

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'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...\n", 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

得分: 0

你正在将nil传递给s3manager.NewDownloader,而它需要一个Session

sess := session.New()
manager := s3manager.NewDownloader(sess)
英文:

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

sess := session.New()
manager := s3manager.NewDownloader(sess)

huangapple
  • 本文由 发表于 2015年12月18日 06:38:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/34345611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定