dourangdz750379 2016-10-25 09:04
浏览 385

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?

  • 写回答

1条回答 默认 最新

  • dongmi6102 2016-12-01 01:25
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序