我使用这篇文章中的脚本从s3下载文件。 一切正常,但下载的文件都是0B。
我的脚本与上面的脚本基本相同。
我使用~/.aws/credentials
设置我的密钥并在脚本中设置区域。
这是我的剧本:
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
var (
Bucket = "logs" // Download from this bucket
Prefix = "local-dir/my_log/20150611/20150611" // Using this key prefix
LocalDirectory = "s3logs" // Into this directory
)
func main() {
manager := s3manager.NewDownloader(nil)
d := downloader{bucket: Bucket, dir: LocalDirectory, Downloader: manager}
client := s3.New(&aws.Config{Region: "ap-northeast-1"})
params := &s3.ListObjectsInput{Bucket: &Bucket, Prefix: &Prefix}
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)
}
该脚本很好地列出了存储桶中的对象,但没有将文件下载到我的本地文件系统中。并且脚本没有遇到任何异常。
知道为什么吗? 谢谢!