github.com/GuanceCloud/cliutils@v1.1.21/pprofparser/service/storage/oss.go (about)

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"path/filepath"
     7  	"strings"
     8  	"sync"
     9  	"time"
    10  
    11  	"github.com/GuanceCloud/cliutils/pprofparser/cfg"
    12  	"github.com/GuanceCloud/cliutils/pprofparser/domain/parameter"
    13  	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    14  )
    15  
    16  var (
    17  	timeZoneCST = time.FixedZone("CST", 3600*8)
    18  	ossClient   *OSS
    19  	ossInitLock sync.Mutex
    20  )
    21  
    22  type OSS struct {
    23  	client *oss.Client
    24  }
    25  
    26  func InitOSS() (*OSS, error) {
    27  	if ossClient != nil {
    28  		return ossClient, nil
    29  	}
    30  	ossInitLock.Lock()
    31  	defer ossInitLock.Unlock()
    32  	if ossClient == nil {
    33  		client, err := oss.New(cfg.Cfg.Oss.Host, cfg.Cfg.Oss.AccessKey,
    34  			cfg.Cfg.Oss.SecretKey)
    35  		if err != nil {
    36  			return nil, fmt.Errorf("oss.New fail: %w", err)
    37  		}
    38  		ossClient = &OSS{client: client}
    39  	}
    40  	return ossClient, nil
    41  }
    42  
    43  func (o *OSS) selectBucket() (*oss.Bucket, error) {
    44  	return o.client.Bucket(cfg.Cfg.Oss.ProfileBucket)
    45  }
    46  
    47  func (o *OSS) IsFileExists(path string) (bool, error) {
    48  	bucket, err := o.selectBucket()
    49  	if err != nil {
    50  		return false, fmt.Errorf("call IsFileExists, path [%s]: %w", path, err)
    51  	}
    52  	return bucket.IsObjectExist(path)
    53  }
    54  
    55  func (o *OSS) ReadFile(path string) (io.ReadCloser, error) {
    56  	bucket, err := o.selectBucket()
    57  	if err != nil {
    58  		return nil, fmt.Errorf("ReadFile selectBucket: %w", err)
    59  	}
    60  	return bucket.GetObject(path)
    61  }
    62  
    63  func (o *OSS) GetProfileDir(workspaceUUID string, profileID string, unixTimeNS int64) string {
    64  	if unixTimeNS >= parameter.MinTimestampMicro && unixTimeNS <= parameter.MaxTimestampMicro {
    65  		unixTimeNS *= 1000
    66  	}
    67  
    68  	date := time.Unix(0, unixTimeNS).In(timeZoneCST).Format("20060102")
    69  	path := filepath.Join(cfg.Cfg.Oss.ProfileDir, date, workspaceUUID, profileID[:2], profileID)
    70  	return strings.ReplaceAll(path, "\\", "/")
    71  }
    72  
    73  func (o *OSS) GetProfilePath(workspaceUUID string, profileID string, unixTimeNS int64, ossFilename string) string {
    74  	if unixTimeNS >= parameter.MinTimestampMicro && unixTimeNS <= parameter.MaxTimestampMicro {
    75  		unixTimeNS *= 1000
    76  	}
    77  
    78  	date := time.Unix(0, unixTimeNS).In(timeZoneCST).Format("20060102")
    79  	path := filepath.Join(cfg.Cfg.Oss.ProfileDir, date, workspaceUUID, profileID[:2], profileID, ossFilename)
    80  	return strings.ReplaceAll(path, "\\", "/")
    81  }
    82  
    83  func (o *OSS) GetProfilePathOld(workspaceUUID string, profileID string, unixTimeNS int64, ossFilename string) string {
    84  	if unixTimeNS >= parameter.MinTimestampMicro && unixTimeNS <= parameter.MaxTimestampMicro {
    85  		unixTimeNS *= 1000
    86  	}
    87  	date := time.Unix(0, unixTimeNS).In(timeZoneCST).Format("20060102")
    88  	path := filepath.Join(cfg.Cfg.Oss.ProfileDir, date, workspaceUUID, profileID, ossFilename)
    89  	return strings.ReplaceAll(path, "\\", "/")
    90  }