我正在使用aws-sdk-go从s3下载文件,然而,我发现如果密钥不存在于桶中,调用GetObject将永远阻塞(我从aws-cli发现了这个事实)。任何一个能告诉我是怎么回事?下面是我的代码片段:
// newS3Session return a s3 session with AddrInfo
func newS3Session(addr AddrInfo) *session.Session {
cred := credentials.NewStaticCredentials(addr.AccessKey, addr.SecretKey, "")
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("default"),
Credentials: cred,
Endpoint: aws.String(addr.EndPoint),
S3ForcePathStyle: aws.Bool(false),
DisableSSL: aws.Bool(true),
}))
return sess
}
func download(_ context.Context, s *session.Session, filename string) error {
downloader := s3manager.NewDownloader(s)
file, err := os.Create(tmpPath + filename)
if err != nil {
return fmt.Errorf("file_create_error|err=%w,filename=%s", err, filename)
}
defer func() {
err = file.Close()
}()
svc := s3.New(s)
_, err = svc.HeadObjectWithContext(context.Background(), &s3.HeadObjectInput{
Bucket: aws.String(srcAddr.Bucket),
Key: aws.String(filepath.Join(srcAddr.FilePath, filename)),
})
if err != nil {
panic(err)
}
fmt.Printf("file_found|filepath:%sn", filepath.Join(srcAddr.FilePath, filename))
n, err := downloader.Download(file, &s3.GetObjectInput{
Bucket: aws.String(srcAddr.Bucket),
Key: aws.String(filepath.Join(srcAddr.FilePath, filename)),
})
if err != nil {
return fmt.Errorf("fail_to_download_file|err=%w,filepath=%s", err,
filepath.Join(srcAddr.FilePath, filename))
}
fmt.Printf("file %s downloaded, size=%dB", filename, n)
return nil
}
代码工作正常时,主要存在于s3
我也遇到过类似的问题,这就是问题- https://tracker.ceph.com/issues/38638
我可以绕过aws错误:
if awsErr, ok := err.(awserr.Error); ok && (awsErr.Code() == "Forbidden" || awsErr.Code() == "NotFound") {
return false, nil
} else if err != nil {
return false, err
} else {
return true, nil
}