【问题标题】:Golang Aws S3 NoSuchKey: The specified key does not existGolang Aws S3 NoSuchKey:指定的键不存在
【发布时间】:2016-10-25 09:04:31
【问题描述】:

我正在尝试从 S3 下载对象,以下是我的代码:

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)
}

listFile()函数中可以得到对象列表,但是调用下载时返回404,为什么?

【问题讨论】:

    标签: amazon-web-services go amazon-s3


    【解决方案1】:

    我在使用该库的最新版本时遇到了同样的问题。有时,对象键会以“./”为前缀,SDK 默认会删除该前缀,导致下载失败。

    尝试将此添加到您的 aws.Config 中,看看是否有帮助:

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

    我提交了issue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 2016-12-23
      • 2015-12-21
      • 1970-01-01
      相关资源
      最近更新 更多