Golang Aws S3 NoSuchKey:指定的键不存在

I'm trying to download Objects from S3, the following is my code:

func listFile(bucket, prefix string) error {
    svc := s3.New(sess)
    params := &s3.ListObjectsInput{
        Bucket: aws.String(bucket), // Required
        Prefix: aws.String(prefix),
    }
    return svc.ListObjectsPages(params, func(p *s3.ListObjectsOutput, lastPage bool) bool {
        for _, o := range p.Contents {
            //log.Println(*o.Key)
            log.Println(*o.Key)
            download(bucket, *o.Key)
            return true
        }
        return lastPage
    })
}

func download(bucket, key string) {
    logDir := conf.Cfg.Section("share").Key("LOG_DIR").MustString(".")
    tmpLogPath := filepath.Join(logDir, bucket, key)
    s3Svc := s3.New(sess)
    downloader := s3manager.NewDownloaderWithClient(s3Svc, func(d *s3manager.Downloader) {
        d.PartSize = 2 * 1024 * 1024 // 2MB per part
    })
    f, err := os.OpenFile(tmpLogPath, os.O_CREATE|os.O_WRONLY, 0644)
    if _, err = downloader.Download(f, &s3.GetObjectInput{
        Bucket: aws.String(bucket),
        Key:    aws.String(key),
    }); err != nil {
        log.Fatal(err)
    }
    f.Close()
}

func main() {
    bucket := "mybucket"
    key := "myprefix"
    listFile(bucket, key)
}

I can get the objects list in the function listFile(), but a 404 returned when call download, why?

I had the same problem with recent versions of the library. Sometimes, the object key will be prefixed with a "./" that the SDK will remove by default making the download fail.

Try adding this to your aws.Config and see if it helps:

config := aws.Config{
    ...
    DisableRestProtocolURICleaning: aws.Bool(true),
}

I submitted an issue.