|
|
@ -1,24 +1,56 @@
|
|
|
|
package ObsUtil
|
|
|
|
package ObsUtil
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"dsTools/Utils/ConfigUtil"
|
|
|
|
"dsTools/Utils/CommonUtil"
|
|
|
|
"dsTools/Utils/obs"
|
|
|
|
"dsTools/Utils/obs"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
|
|
//Obs
|
|
|
|
|
|
|
|
Bucket string
|
|
|
|
|
|
|
|
Ak string
|
|
|
|
|
|
|
|
Sk string
|
|
|
|
|
|
|
|
Endpoint string
|
|
|
|
|
|
|
|
BackupPrefix string
|
|
|
|
|
|
|
|
RemainDays int32
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
var configIniFile = "./Config/Obs.ini"
|
|
|
|
|
|
|
|
//判断文件不是存在
|
|
|
|
|
|
|
|
if !CommonUtil.Exists(configIniFile) {
|
|
|
|
|
|
|
|
configIniFile = "." + configIniFile
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iniParser := IniParser{}
|
|
|
|
|
|
|
|
if err := iniParser.Load(configIniFile); err != nil {
|
|
|
|
|
|
|
|
fmt.Printf("try load config file[%s] error[%s]\n", configIniFile, err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//obs配置
|
|
|
|
|
|
|
|
Ak = iniParser.GetString("obs", "ak")
|
|
|
|
|
|
|
|
Sk = iniParser.GetString("obs", "sk")
|
|
|
|
|
|
|
|
Bucket = iniParser.GetString("obs", "bucket")
|
|
|
|
|
|
|
|
Endpoint = iniParser.GetString("obs", "endpoint")
|
|
|
|
|
|
|
|
BackupPrefix = iniParser.GetString("obs", "backupPrefix")
|
|
|
|
|
|
|
|
RemainDays = iniParser.GetInt32("obs", "remainDays")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
功能:删除指定前缀的过期文件
|
|
|
|
功能:删除指定前缀的过期文件
|
|
|
|
作者:黄海
|
|
|
|
作者:黄海
|
|
|
|
时间:2020-04-05
|
|
|
|
时间:2020-04-05
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
func DeleteExpireFile(prefix string,remainDays int32){
|
|
|
|
func DeleteExpireFile(prefix string, remainDays int32) {
|
|
|
|
// 创建ObsClient结构体
|
|
|
|
// 创建ObsClient结构体
|
|
|
|
var obsClient, _ = obs.New(ConfigUtil.Ak, ConfigUtil.Sk, ConfigUtil.Endpoint)
|
|
|
|
var obsClient, _ = obs.New(Ak, Sk, Endpoint)
|
|
|
|
|
|
|
|
|
|
|
|
input := &obs.ListObjectsInput{}
|
|
|
|
input := &obs.ListObjectsInput{}
|
|
|
|
input.Bucket = ConfigUtil.Bucket
|
|
|
|
input.Bucket = Bucket
|
|
|
|
// 设置列举带有prefix前缀的1000个对象
|
|
|
|
// 设置列举带有prefix前缀的1000个对象
|
|
|
|
input.MaxKeys = 1000
|
|
|
|
input.MaxKeys = 1000
|
|
|
|
input.Prefix = prefix
|
|
|
|
input.Prefix = prefix
|
|
|
@ -32,14 +64,14 @@ func DeleteExpireFile(prefix string,remainDays int32){
|
|
|
|
now := time.Now()
|
|
|
|
now := time.Now()
|
|
|
|
sumD := now.Sub(val.LastModified)
|
|
|
|
sumD := now.Sub(val.LastModified)
|
|
|
|
if int32(sumD.Hours()/24) > remainDays {
|
|
|
|
if int32(sumD.Hours()/24) > remainDays {
|
|
|
|
fmt.Println("发现超时文件,将删除:"+val.Key)
|
|
|
|
fmt.Println("发现超时文件,将删除:" + val.Key)
|
|
|
|
input := &obs.DeleteObjectInput{}
|
|
|
|
input := &obs.DeleteObjectInput{}
|
|
|
|
input.Bucket = ConfigUtil.Bucket
|
|
|
|
input.Bucket = Bucket
|
|
|
|
input.Key = val.Key
|
|
|
|
input.Key = val.Key
|
|
|
|
obsClient.DeleteObject(input)
|
|
|
|
obsClient.DeleteObject(input)
|
|
|
|
fmt.Println("已成功删除!" + val.Key)
|
|
|
|
fmt.Println("已成功删除!" + val.Key)
|
|
|
|
}else{
|
|
|
|
} else {
|
|
|
|
fmt.Println("没有过期,无需删除:"+val.Key)
|
|
|
|
fmt.Println("没有过期,无需删除:" + val.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if obsError, ok := err.(obs.ObsError); ok {
|
|
|
|
} else if obsError, ok := err.(obs.ObsError); ok {
|
|
|
@ -54,12 +86,12 @@ func DeleteExpireFile(prefix string,remainDays int32){
|
|
|
|
作者:黄海
|
|
|
|
作者:黄海
|
|
|
|
时间:2020-04-05
|
|
|
|
时间:2020-04-05
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
func UploadFileMultiPart(key string,sourceFile string){
|
|
|
|
func UploadFileMultiPart(key string, sourceFile string) {
|
|
|
|
// 创建ObsClient结构体
|
|
|
|
// 创建ObsClient结构体
|
|
|
|
var obsClient, _ = obs.New(ConfigUtil.Ak, ConfigUtil.Sk, ConfigUtil.Endpoint)
|
|
|
|
var obsClient, _ = obs.New(Ak, Sk, Endpoint)
|
|
|
|
// 初始化分段上传任务
|
|
|
|
// 初始化分段上传任务
|
|
|
|
input := &obs.InitiateMultipartUploadInput{}
|
|
|
|
input := &obs.InitiateMultipartUploadInput{}
|
|
|
|
input.Bucket = ConfigUtil.Bucket
|
|
|
|
input.Bucket = Bucket
|
|
|
|
input.Key = key
|
|
|
|
input.Key = key
|
|
|
|
output, err := obsClient.InitiateMultipartUpload(input)
|
|
|
|
output, err := obsClient.InitiateMultipartUpload(input)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -91,7 +123,7 @@ func UploadFileMultiPart(key string,sourceFile string){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
uploadPartInput := &obs.UploadPartInput{}
|
|
|
|
uploadPartInput := &obs.UploadPartInput{}
|
|
|
|
uploadPartInput.Bucket = ConfigUtil.Bucket
|
|
|
|
uploadPartInput.Bucket = Bucket
|
|
|
|
uploadPartInput.Key = key
|
|
|
|
uploadPartInput.Key = key
|
|
|
|
uploadPartInput.UploadId = uploadId
|
|
|
|
uploadPartInput.UploadId = uploadId
|
|
|
|
uploadPartInput.SourceFile = sourceFile
|
|
|
|
uploadPartInput.SourceFile = sourceFile
|
|
|
@ -124,7 +156,7 @@ func UploadFileMultiPart(key string,sourceFile string){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
completeMultipartUploadInput := &obs.CompleteMultipartUploadInput{}
|
|
|
|
completeMultipartUploadInput := &obs.CompleteMultipartUploadInput{}
|
|
|
|
completeMultipartUploadInput.Bucket = ConfigUtil.Bucket
|
|
|
|
completeMultipartUploadInput.Bucket = Bucket
|
|
|
|
completeMultipartUploadInput.Key = key
|
|
|
|
completeMultipartUploadInput.Key = key
|
|
|
|
completeMultipartUploadInput.UploadId = uploadId
|
|
|
|
completeMultipartUploadInput.UploadId = uploadId
|
|
|
|
completeMultipartUploadInput.Parts = parts
|
|
|
|
completeMultipartUploadInput.Parts = parts
|
|
|
@ -136,3 +168,122 @@ func UploadFileMultiPart(key string,sourceFile string){
|
|
|
|
fmt.Printf("RequestId:%s\n", completeMultipartUploadOutput.RequestId)
|
|
|
|
fmt.Printf("RequestId:%s\n", completeMultipartUploadOutput.RequestId)
|
|
|
|
obsClient.Close()
|
|
|
|
obsClient.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type IniParser struct {
|
|
|
|
|
|
|
|
confReader *ini.File // config reader
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type IniParserError struct {
|
|
|
|
|
|
|
|
errorInfo string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (e *IniParserError) Error() string { return e.errorInfo }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IniParser) Load(configFileName string) error {
|
|
|
|
|
|
|
|
conf, err := ini.Load(configFileName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
this.confReader = nil
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.confReader = conf
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IniParser) GetString(section string, key string) string {
|
|
|
|
|
|
|
|
if this.confReader == nil {
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s := this.confReader.Section(section)
|
|
|
|
|
|
|
|
if s == nil {
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return s.Key(key).String()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IniParser) GetInt32(section string, key string) int32 {
|
|
|
|
|
|
|
|
if this.confReader == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s := this.confReader.Section(section)
|
|
|
|
|
|
|
|
if s == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
valueInt, _ := s.Key(key).Int()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return int32(valueInt)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IniParser) GetUint32(section string, key string) uint32 {
|
|
|
|
|
|
|
|
if this.confReader == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s := this.confReader.Section(section)
|
|
|
|
|
|
|
|
if s == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
valueInt, _ := s.Key(key).Uint()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return uint32(valueInt)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IniParser) GetInt64(section string, key string) int64 {
|
|
|
|
|
|
|
|
if this.confReader == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s := this.confReader.Section(section)
|
|
|
|
|
|
|
|
if s == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
valueInt, _ := s.Key(key).Int64()
|
|
|
|
|
|
|
|
return valueInt
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IniParser) GetUint64(section string, key string) uint64 {
|
|
|
|
|
|
|
|
if this.confReader == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s := this.confReader.Section(section)
|
|
|
|
|
|
|
|
if s == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
valueInt, _ := s.Key(key).Uint64()
|
|
|
|
|
|
|
|
return valueInt
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IniParser) GetFloat32(section string, key string) float32 {
|
|
|
|
|
|
|
|
if this.confReader == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s := this.confReader.Section(section)
|
|
|
|
|
|
|
|
if s == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
valueFloat, _ := s.Key(key).Float64()
|
|
|
|
|
|
|
|
return float32(valueFloat)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IniParser) GetFloat64(section string, key string) float64 {
|
|
|
|
|
|
|
|
if this.confReader == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s := this.confReader.Section(section)
|
|
|
|
|
|
|
|
if s == nil {
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
valueFloat, _ := s.Key(key).Float64()
|
|
|
|
|
|
|
|
return valueFloat
|
|
|
|
|
|
|
|
}
|
|
|
|