@@ -0,0 +1,103 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
// CreateEmptyFile 创建一个固定size大小的空文件
|
||||
// 如果seek文件失败,则删除文件,并返回错误信息
|
||||
func CreateEmptyFile(name string, size int64) (f *os.File, err error) {
|
||||
defer func() {
|
||||
if err != nil && f != nil {
|
||||
f.Close()
|
||||
os.Remove(name)
|
||||
}
|
||||
}()
|
||||
f, err = os.OpenFile(name, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = f.Seek(size-1, 0); err != nil {
|
||||
return
|
||||
}
|
||||
_, err = f.Write([]byte{0})
|
||||
return
|
||||
}
|
||||
|
||||
func MakeDir(name string) error {
|
||||
if err := os.MkdirAll(name, 0755); err != nil {
|
||||
return errors.New("make dir wrong")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteFile(data []byte, filename string) (err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
_ = os.Remove(filename)
|
||||
}
|
||||
}()
|
||||
pi := GetPathInfo(filename)
|
||||
if pi.Dir != "" {
|
||||
if err = os.MkdirAll(pi.Dir, 0755); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = f.Write(data)
|
||||
_ = f.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// WriteStruct2Xlsx 将struct切片写入Excel sheet
|
||||
func WriteStruct2Xlsx(sheet string, records interface{}) *excelize.File {
|
||||
xlsx := excelize.NewFile() // new file
|
||||
index, _ := xlsx.NewSheet(sheet) // new sheet
|
||||
xlsx.SetActiveSheet(index) // set active (default) sheet
|
||||
t := reflect.TypeOf(records)
|
||||
if t.Kind() != reflect.Slice {
|
||||
panic("records must be slice")
|
||||
}
|
||||
s := reflect.ValueOf(records)
|
||||
for i := 0; i < s.Len(); i++ {
|
||||
elem := s.Index(i).Interface()
|
||||
elemType := reflect.TypeOf(elem)
|
||||
elemValue := reflect.ValueOf(elem)
|
||||
if elemType.Kind() == reflect.Ptr {
|
||||
elemType = elemType.Elem()
|
||||
elemValue = elemValue.Elem()
|
||||
}
|
||||
if elemType.Kind() != reflect.Struct {
|
||||
panic("record in slice must be a struct")
|
||||
}
|
||||
k := 0
|
||||
for j := 0; j < elemType.NumField(); j++ {
|
||||
field := elemType.Field(j)
|
||||
tag := field.Tag.Get("xlsx")
|
||||
if tag == "-" {
|
||||
continue
|
||||
}
|
||||
if tag == "" {
|
||||
tag = field.Name
|
||||
}
|
||||
column, _ := excelize.ColumnNumberToName(k + 1)
|
||||
k++
|
||||
name := tag
|
||||
// 设置表头
|
||||
if i == 0 {
|
||||
_ = xlsx.SetCellValue(sheet, fmt.Sprintf("%s%d", column, i+1), name)
|
||||
}
|
||||
// 设置内容
|
||||
_ = xlsx.SetCellValue(sheet, fmt.Sprintf("%s%d", column, i+2), elemValue.Field(j).Interface())
|
||||
}
|
||||
}
|
||||
return xlsx
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PathInfo 路径信息
|
||||
type PathInfo struct {
|
||||
Dir string // 目录
|
||||
Ext string // 扩展名
|
||||
FileName string // 没有扩展名的文件名名称
|
||||
FullFileName string // 文件全名
|
||||
}
|
||||
|
||||
// GetPathInfo 解析路径
|
||||
// 返回文件,路径,文件名等信息
|
||||
func GetPathInfo(path string) (pi PathInfo) {
|
||||
if path == "" {
|
||||
return
|
||||
}
|
||||
if !filepath.IsAbs(path) {
|
||||
path, _ = filepath.Abs(path)
|
||||
}
|
||||
ext := filepath.Ext(path)
|
||||
dir, name := filepath.Split(path)
|
||||
pi.Dir = dir
|
||||
pi.Ext = ext
|
||||
pi.FileName = strings.TrimRight(name, ext)
|
||||
pi.FullFileName = name
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
)
|
||||
|
||||
const (
|
||||
id = "AKIAJM3XLDNUBXUY36EQ"
|
||||
secret = "DZ1Y1ETQlCEc/6JlyW2mxdE8SAfPsmSSrpxGzcTi"
|
||||
region = "ap-east-1"
|
||||
bucket = "tknk.zahokc.cn"
|
||||
URL = "https://tknk.zahokc.cn.s3.ap-east-1.amazonaws.com/"
|
||||
APPFlag = "ys-7527/"
|
||||
)
|
||||
|
||||
var (
|
||||
endpoint = ""
|
||||
disableSSL = true
|
||||
AwsSession *session.Session
|
||||
)
|
||||
|
||||
func init() {
|
||||
NewSession()
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建session
|
||||
*/
|
||||
func NewSession() {
|
||||
creds := credentials.NewStaticCredentials(id, secret, "")
|
||||
config := &aws.Config{
|
||||
Region: aws.String(region),
|
||||
Endpoint: &endpoint,
|
||||
S3ForcePathStyle: aws.Bool(true),
|
||||
Credentials: creds,
|
||||
DisableSSL: &disableSSL,
|
||||
}
|
||||
se, err := session.NewSession(config)
|
||||
AwsSession = se
|
||||
if err != nil {
|
||||
fmt.Printf("create session fail %+v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func ListAllBucket() {
|
||||
svc := s3.New(AwsSession)
|
||||
resp, err := svc.ListBuckets(&s3.ListBucketsInput{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(resp.Buckets)
|
||||
}
|
||||
}
|
||||
|
||||
/**上传文件*/
|
||||
func PutObject(key string, content []byte, ttl int64) error {
|
||||
svc := s3.New(AwsSession)
|
||||
params := &s3.PutObjectInput{
|
||||
Bucket: aws.String(bucket), // Required
|
||||
Key: aws.String(key), // Required
|
||||
ACL: aws.String("public-read"), //设置成公共读。
|
||||
Body: bytes.NewReader(content),
|
||||
}
|
||||
duration := time.Duration(ttl * 1000)
|
||||
ext := path.Ext(key)
|
||||
if ttl > 0 {
|
||||
params.SetExpires(time.Now().Add(duration))
|
||||
}
|
||||
if ext != "" {
|
||||
contentType := mime.TypeByExtension(ext)
|
||||
if contentType != "" {
|
||||
params.SetContentType(contentType)
|
||||
}
|
||||
}
|
||||
_, err := svc.PutObject(params)
|
||||
//svc.PutObjectLegalHold
|
||||
return err
|
||||
}
|
||||
|
||||
func GetObject(key string) ([]byte, error) {
|
||||
svc := s3.New(AwsSession)
|
||||
params := &s3.GetObjectInput{
|
||||
Bucket: aws.String(bucket), // Required
|
||||
Key: aws.String(key), // Require
|
||||
}
|
||||
getObjectOutput, err := svc.GetObject(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := io.ReadAll(getObjectOutput.Body)
|
||||
getObjectOutput.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
Reference in New Issue
Block a user