You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
829 B
39 lines
829 B
package s3
|
|
|
|
import (
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
"io/ioutil"
|
|
)
|
|
|
|
// Obtain
|
|
//
|
|
// @Description:
|
|
// @receiver s
|
|
// @param bucket
|
|
// @param key
|
|
// @return []byte
|
|
// @return error
|
|
func (s *S3Client) Obtain(bucket, key string) ([]byte, error) {
|
|
resp, err := s.S3C.GetObject(&s3.GetObjectInput{
|
|
Bucket: aws.String(bucket), // 替换为您的S3存储桶名称
|
|
Key: aws.String(key), // 替换为您要读取的对象键
|
|
})
|
|
|
|
if err != nil {
|
|
if aErr, ok := err.(awserr.Error); ok && aErr.Code() == s3.ErrCodeNoSuchKey {
|
|
// todo: 处理对象不存在的情况
|
|
return nil, err
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
// 读取resp.Body中的数据
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|