123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- package oss
- import (
- "bytes"
- "git.sxidc.com/go-tools/utils/strutils"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- "github.com/pkg/errors"
- "io"
- "log"
- )
- var Bkt *oss.Bucket
- var pfx string
- func Init(endpoint, keyID, keySecret, prefix string) error {
- pfx = prefix
- client, err := oss.New(endpoint, keyID, keySecret)
- if err != nil {
- return err
- }
- Bkt, err = client.Bucket("fs-yf")
- if err != nil {
- return err
- }
- return nil
- }
- func Touch(reader io.Reader, to string) error {
- return TouchPrefix(reader, to, true)
- }
- func TouchPrefix(reader io.Reader, to string, addPfx bool) error {
- if addPfx {
- return Bkt.PutObject(pfx+to, reader)
- }
- return Bkt.PutObject(to, reader)
- }
- func Url(obj string) (string, error) {
- return UrlPfx(obj, true)
- }
- func UrlPfx(obj string, addPfx bool) (string, error) {
- if strutils.HasBlank(obj) {
- log.Println("oss获取Url发现违规参数")
- return "", nil
- }
- if addPfx {
- return Bkt.SignURL(pfx+obj, oss.HTTPGet, 60*60*60*365)
- }
- return Bkt.SignURL(obj, oss.HTTPGet, 60*60*60*365)
- }
- func ZoomUrl(obj, s string) (string, error) {
- if strutils.HasBlank(obj) {
- log.Println("oss发现违规参数")
- return "", nil
- }
- return Bkt.SignURL(pfx+obj, oss.HTTPGet, 60, oss.Process(s))
- }
- func ZoomUrlPfx(obj, s string, addPfx bool) (string, error) {
- if strutils.HasBlank(obj) {
- log.Println("oss发现违规参数")
- return "", nil
- }
- if addPfx {
- return Bkt.SignURL(pfx+obj, oss.HTTPGet, 60, oss.Process(s))
- }
- return Bkt.SignURL(obj, oss.HTTPGet, 60, oss.Process(s))
- }
- func Exist(obj string) (bool, error) {
- if strutils.HasBlank(obj) {
- return false, nil
- }
- return Bkt.IsObjectExist(pfx + obj)
- }
- func Rm(objs ...string) error {
- L := len(objs)
- if L == 0 {
- return nil
- }
- result, err := Bkt.DeleteObjects(objs)
- if err != nil {
- return err
- }
- if len(result.DeletedObjects) != L {
- go func() {
- // 日志打印哪些Obj删除失败
- sucMap := make(map[string]struct{}, len(result.DeletedObjects))
- for _, deleted := range result.DeletedObjects {
- sucMap[deleted] = struct{}{}
- }
- for _, obj := range objs {
- if _, ok := sucMap[obj]; !ok {
- log.Println("Oss删除%s图片失败")
- }
- }
- }()
- }
- return nil
- }
- func Ls(dir string) ([]string, error) {
- if strutils.HasBlank(dir) {
- return []string{}, nil
- }
- v2, err := Bkt.ListObjectsV2(oss.Prefix(pfx + dir))
- if err != nil {
- return nil, err
- }
- ks := make([]string, len(v2.Objects))
- for i, obj := range v2.Objects {
- ks[i] = obj.Key
- }
- return ks, nil
- }
- func LsWithoutPrefix(dir string) ([]string, error) {
- if strutils.HasBlank(dir) {
- return []string{}, nil
- }
- v2, err := Bkt.ListObjectsV2(oss.Prefix(dir))
- if err != nil {
- return nil, err
- }
- ks := make([]string, len(v2.Objects))
- for i, obj := range v2.Objects {
- ks[i] = obj.Key
- }
- return ks, nil
- }
- type ObjectInfo struct {
- Key string
- Type string
- Size int64
- }
- func LsDetails(dir string) ([]ObjectInfo, error) {
- if strutils.HasBlank(dir) {
- return []ObjectInfo{}, nil
- }
- v2, err := Bkt.ListObjectsV2(oss.Prefix(pfx + dir))
- if err != nil {
- return nil, err
- }
- ks := make([]ObjectInfo, len(v2.Objects))
- for i, obj := range v2.Objects {
- ks[i] = ObjectInfo{
- Key: obj.Key,
- Type: obj.Type,
- Size: obj.Size,
- }
- }
- return ks, nil
- }
- func Mv(f, t string) error {
- if err := Cp(f, t); err != nil {
- return err
- }
- return Bkt.DeleteObject(pfx + f)
- }
- func CpOrigin(f, t string) error {
- _, err := Bkt.CopyObject(f, t)
- return err
- }
- func Cp(f, t string) error {
- return CpOrigin(pfx+f, pfx+t)
- }
- func TouchFormatImg(reader io.Reader, to string) error {
- if reader == nil || strutils.HasBlank(to) {
- log.Println("oss上传发现违规参数")
- return nil
- }
- return Bkt.PutObject(pfx+to, reader, oss.ContentType("image/jpg"))
- }
- func GetObjectBytes(obj string) ([]byte, error) {
- if strutils.HasBlank(obj) {
- return nil, errors.New("参数错误")
- }
- buf := new(bytes.Buffer)
- body, err := Bkt.GetObject(pfx + obj)
- if err != nil {
- return nil, err
- }
- _, err = io.Copy(buf, body)
- if err != nil {
- return nil, err
- }
- err = body.Close()
- if err != nil {
- return nil, err
- }
- return buf.Bytes(), nil
- }
- func MvWithoutPrefix(f, t string) error {
- if _, err := Bkt.CopyObject(f, t); err != nil {
- return err
- }
- return Bkt.DeleteObject(f)
- }
|