Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7e75d5f5eb | ||
![]() |
d0d264d1e7 | ||
![]() |
aee7202752 | ||
![]() |
5f9f144ab3 | ||
![]() |
e246123ce2 | ||
![]() |
1e2d00edaf | ||
![]() |
0c7ec61e65 | ||
![]() |
c214b6c94a |
35
common_fun/file_func.go
Normal file
35
common_fun/file_func.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package cf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// EnsureDirExists 确保目录存在,如果不存在则创建
|
||||||
|
func EnsureDirExists(dirPath string) error {
|
||||||
|
// 判断路径是否存在
|
||||||
|
_, err := os.Stat(dirPath)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
// 目录不存在,递归创建
|
||||||
|
err = os.MkdirAll(dirPath, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("创建目录失败,目录:%s,err: %w", dirPath, err)
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
// 其他错误(如权限问题)
|
||||||
|
return fmt.Errorf("检查目录失败,目录:%s,err: %w", dirPath, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnsureParentDirExists 确保文件的目录存在,如果不存在则创建
|
||||||
|
func EnsureParentDirExists(filePath string) error {
|
||||||
|
// 提取父级目录路径
|
||||||
|
dir := filepath.Dir(filePath)
|
||||||
|
err := EnsureDirExists(dir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
19
common_fun/file_func_test.go
Normal file
19
common_fun/file_func_test.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package cf
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestEnsureDirExists(t *testing.T) {
|
||||||
|
url := "/test"
|
||||||
|
err := EnsureDirExists(url)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("EnsureDirExists() error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnsureParentDirExists(t *testing.T) {
|
||||||
|
filePath := "/test/test1/test.txt"
|
||||||
|
err := EnsureParentDirExists(filePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("EnsureDirExists() error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
2
go.mod
2
go.mod
@@ -9,6 +9,7 @@ require (
|
|||||||
github.com/alibabacloud-go/dysmsapi-20170525/v5 v5.1.1
|
github.com/alibabacloud-go/dysmsapi-20170525/v5 v5.1.1
|
||||||
github.com/alibabacloud-go/tea v1.3.8
|
github.com/alibabacloud-go/tea v1.3.8
|
||||||
github.com/alibabacloud-go/tea-utils/v2 v2.0.7
|
github.com/alibabacloud-go/tea-utils/v2 v2.0.7
|
||||||
|
github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.2.3
|
||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/redis/go-redis/v9 v9.10.0
|
github.com/redis/go-redis/v9 v9.10.0
|
||||||
github.com/stretchr/testify v1.10.0
|
github.com/stretchr/testify v1.10.0
|
||||||
@@ -30,6 +31,7 @@ require (
|
|||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/tjfoc/gmsm v1.4.1 // indirect
|
github.com/tjfoc/gmsm v1.4.1 // indirect
|
||||||
golang.org/x/net v0.41.0 // indirect
|
golang.org/x/net v0.41.0 // indirect
|
||||||
|
golang.org/x/time v0.4.0 // indirect
|
||||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
9
go.sum
9
go.sum
@@ -44,13 +44,17 @@ github.com/alibabacloud-go/tea-utils/v2 v2.0.6/go.mod h1:qxn986l+q33J5VkialKMqT/
|
|||||||
github.com/alibabacloud-go/tea-utils/v2 v2.0.7 h1:WDx5qW3Xa5ZgJ1c8NfqJkF6w+AU5wB8835UdhPr6Ax0=
|
github.com/alibabacloud-go/tea-utils/v2 v2.0.7 h1:WDx5qW3Xa5ZgJ1c8NfqJkF6w+AU5wB8835UdhPr6Ax0=
|
||||||
github.com/alibabacloud-go/tea-utils/v2 v2.0.7/go.mod h1:qxn986l+q33J5VkialKMqT/TTs3E+U9MJpd001iWQ9I=
|
github.com/alibabacloud-go/tea-utils/v2 v2.0.7/go.mod h1:qxn986l+q33J5VkialKMqT/TTs3E+U9MJpd001iWQ9I=
|
||||||
github.com/alibabacloud-go/tea-xml v1.1.3/go.mod h1:Rq08vgCcCAjHyRi/M7xlHKUykZCEtyBy9+DPF6GgEu8=
|
github.com/alibabacloud-go/tea-xml v1.1.3/go.mod h1:Rq08vgCcCAjHyRi/M7xlHKUykZCEtyBy9+DPF6GgEu8=
|
||||||
|
github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.2.3 h1:LyeTJauAchnWdre3sAyterGrzaAtZ4dSNoIvDvaWfo4=
|
||||||
|
github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.2.3/go.mod h1:FTzydeQVmR24FI0D6XWUOMKckjXehM/jgMn1xC+DA9M=
|
||||||
github.com/aliyun/credentials-go v1.1.2/go.mod h1:ozcZaMR5kLM7pwtCMEpVmQ242suV6qTJya2bDq4X1Tw=
|
github.com/aliyun/credentials-go v1.1.2/go.mod h1:ozcZaMR5kLM7pwtCMEpVmQ242suV6qTJya2bDq4X1Tw=
|
||||||
github.com/aliyun/credentials-go v1.3.1/go.mod h1:8jKYhQuDawt8x2+fusqa1Y6mPxemTsBEN04dgcAcYz0=
|
github.com/aliyun/credentials-go v1.3.1/go.mod h1:8jKYhQuDawt8x2+fusqa1Y6mPxemTsBEN04dgcAcYz0=
|
||||||
github.com/aliyun/credentials-go v1.3.6/go.mod h1:1LxUuX7L5YrZUWzBrRyk0SwSdH4OmPrib8NVePL3fxM=
|
github.com/aliyun/credentials-go v1.3.6/go.mod h1:1LxUuX7L5YrZUWzBrRyk0SwSdH4OmPrib8NVePL3fxM=
|
||||||
github.com/aliyun/credentials-go v1.4.5 h1:O76WYKgdy1oQYYiJkERjlA2dxGuvLRrzuO2ScrtGWSk=
|
github.com/aliyun/credentials-go v1.4.5 h1:O76WYKgdy1oQYYiJkERjlA2dxGuvLRrzuO2ScrtGWSk=
|
||||||
github.com/aliyun/credentials-go v1.4.5/go.mod h1:Jm6d+xIgwJVLVWT561vy67ZRP4lPTQxMbEYRuT2Ti1U=
|
github.com/aliyun/credentials-go v1.4.5/go.mod h1:Jm6d+xIgwJVLVWT561vy67ZRP4lPTQxMbEYRuT2Ti1U=
|
||||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
|
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
@@ -169,10 +173,7 @@ golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
|
|||||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||||
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
|
|
||||||
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
|
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
|
||||||
golang.org/x/net v0.37.1-0.20250305215238-2914f4677317 h1:wneCP+2d9NUmndnyTmY7VwUNYiP26xiN/AtdcojQ1lI=
|
|
||||||
golang.org/x/net v0.37.1-0.20250305215238-2914f4677317/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
|
||||||
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
|
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
|
||||||
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
@@ -227,6 +228,8 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
|||||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||||
|
golang.org/x/time v0.4.0 h1:Z81tqI5ddIoXDPvVQ7/7CC9TnLM7ubaFG2qXYd5BbYY=
|
||||||
|
golang.org/x/time v0.4.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||||
|
@@ -16,13 +16,13 @@ import (
|
|||||||
|
|
||||||
type AnNaQiGpsClient struct {
|
type AnNaQiGpsClient struct {
|
||||||
AppCode string
|
AppCode string
|
||||||
Host string
|
host string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAnNaQiGpsClient(appCode string) *AnNaQiGpsClient {
|
func NewAnNaQiGpsClient(appCode string) *AnNaQiGpsClient {
|
||||||
return &AnNaQiGpsClient{
|
return &AnNaQiGpsClient{
|
||||||
AppCode: appCode,
|
AppCode: appCode,
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
host: "https://jmgeocode.market.alicloudapi.com",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ func (n *AnNaQiGpsClient) GetGpsInfo(longitude, latitude float64) (res *AddressC
|
|||||||
location := lo + "," + la
|
location := lo + "," + la
|
||||||
// 拼接URL
|
// 拼接URL
|
||||||
var fullURL string
|
var fullURL string
|
||||||
fullURL, err = url.JoinPath(n.Host, "geocode/regeo_query")
|
fullURL, err = url.JoinPath(n.host, "geocode/regeo_query")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "获取gps:%s信息失败,拼接路径错误", location)
|
return nil, errors.Wrapf(err, "获取gps:%s信息失败,拼接路径错误", location)
|
||||||
}
|
}
|
||||||
@@ -56,6 +56,9 @@ func (n *AnNaQiGpsClient) GetGpsInfo(longitude, latitude float64) (res *AddressC
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "获取gps:%s信息失败,发送请求失败", location)
|
return nil, errors.Wrapf(err, "获取gps:%s信息失败,发送请求失败", location)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败,%+v", location, resp.Status))
|
||||||
|
}
|
||||||
defer func(Body io.ReadCloser) {
|
defer func(Body io.ReadCloser) {
|
||||||
err = Body.Close()
|
err = Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -76,67 +79,67 @@ func (n *AnNaQiGpsClient) GetGpsInfo(longitude, latitude float64) (res *AddressC
|
|||||||
return nil, errors.Wrapf(err, "获取gps:%s信息失败,解析JSON响应失败,%s", location, string(body))
|
return nil, errors.Wrapf(err, "获取gps:%s信息失败,解析JSON响应失败,%s", location, string(body))
|
||||||
}
|
}
|
||||||
// 检查API返回状态
|
// 检查API返回状态
|
||||||
if apiResult.Code != 200 {
|
if apiResult.Code == 200 {
|
||||||
return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败,%+v", location, apiResult))
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(apiResult.Data.Regeocodes) > 0 {
|
if len(apiResult.Data.Regeocodes) > 0 {
|
||||||
return &apiResult.Data.Regeocodes[0].AddressComponent, nil
|
return &apiResult.Data.Regeocodes[0].AddressComponent, nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败,%+v", location, apiResult))
|
return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败,%+v", location, apiResult))
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApiResult struct {
|
func (n *AnNaQiGpsClient) GetLocation(city, region, address string) (res []*Geocode, err error) {
|
||||||
Data Data `json:"data"`
|
values := url.Values{}
|
||||||
Msg string `json:"msg"`
|
values.Set("city", city)
|
||||||
Success bool `json:"success"`
|
values.Set("address", region+address)
|
||||||
Code int `json:"code"`
|
|
||||||
TaskNo string `json:"taskNo"`
|
// 创建请求
|
||||||
|
var req *http.Request
|
||||||
|
req, err = http.NewRequest(http.MethodPost, n.host+"/geocode/geo/query", bytes.NewBuffer([]byte(values.Encode())))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "获取经纬度失败,创建请求失败")
|
||||||
|
}
|
||||||
|
// 设置请求头
|
||||||
|
req.Header.Add("Authorization", "APPCODE "+n.AppCode)
|
||||||
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||||
|
|
||||||
|
// 发送请求
|
||||||
|
// 创建HTTP客户端
|
||||||
|
client := &http.Client{}
|
||||||
|
client.Timeout = 5 * time.Second
|
||||||
|
var resp *http.Response
|
||||||
|
resp, err = client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "获取经纬度失败,发送请求失败")
|
||||||
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, errors.New(fmt.Sprintf("获取经纬度失败,%+v", resp.Status))
|
||||||
|
}
|
||||||
|
defer func(Body io.ReadCloser) {
|
||||||
|
err = Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("关闭获取经纬度响应体失败: %+v\n", err)
|
||||||
|
}
|
||||||
|
}(resp.Body)
|
||||||
|
// 读取响应体
|
||||||
|
var body []byte
|
||||||
|
body, err = io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "获取经纬度失败,读取响应体失败")
|
||||||
}
|
}
|
||||||
|
|
||||||
type Data struct {
|
// 解析JSON响应
|
||||||
Regeocodes []Regeocode `json:"regeocodes"`
|
var locationInfo LocationInfo
|
||||||
|
err = json.Unmarshal(body, &locationInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "获取经纬度失败,解析JSON响应失败,%s", string(body))
|
||||||
|
}
|
||||||
|
// 检查API返回状态
|
||||||
|
if locationInfo.Code == 200 {
|
||||||
|
if locationInfo.Data.Count > 0 {
|
||||||
|
return locationInfo.Data.Geocodes, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Regeocode struct {
|
return nil, errors.New(fmt.Sprintf("获取经纬度失败,%+v", locationInfo.Msg))
|
||||||
FormattedAddress string `json:"formatted_address"`
|
|
||||||
AddressComponent AddressComponent `json:"addressComponent"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AddressComponent struct {
|
|
||||||
// BusinessAreas []interface{} `json:"businessAreas"`
|
|
||||||
Country string `json:"country"`
|
|
||||||
Province string `json:"province"`
|
|
||||||
Citycode string `json:"citycode"`
|
|
||||||
City string `json:"city"`
|
|
||||||
Adcode string `json:"adcode"`
|
|
||||||
// StreetNumber StreetNumber `json:"streetNumber"`
|
|
||||||
// Towncode string `json:"towncode"`
|
|
||||||
// District string `json:"district"`
|
|
||||||
// Neighborhood Neighborhood `json:"neighborhood"`
|
|
||||||
// Township string `json:"township"`
|
|
||||||
// Building Building `json:"building"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type BusinessArea struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Location string `json:"location"`
|
|
||||||
Id string `json:"id"`
|
|
||||||
}
|
|
||||||
type StreetNumber struct {
|
|
||||||
Number string `json:"number"`
|
|
||||||
Distance string `json:"distance"`
|
|
||||||
Street string `json:"street"`
|
|
||||||
Location string `json:"location"`
|
|
||||||
Direction string `json:"direction"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Neighborhood struct {
|
|
||||||
Name interface{} `json:"name"`
|
|
||||||
Type interface{} `json:"type"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Building struct {
|
|
||||||
Name interface{} `json:"name"`
|
|
||||||
Type interface{} `json:"type"`
|
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@ package gps_tool
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,7 +26,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test1",
|
name: "test1",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 113.419152,
|
longitude: 113.419152,
|
||||||
@@ -37,7 +37,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test2",
|
name: "test2",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 110.165223,
|
longitude: 110.165223,
|
||||||
@@ -48,7 +47,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test2",
|
name: "test2",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 115.928973,
|
longitude: 115.928973,
|
||||||
@@ -60,7 +58,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test3",
|
name: "test3",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 107.397284,
|
longitude: 107.397284,
|
||||||
@@ -72,7 +69,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test4",
|
name: "test4",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 115.929015,
|
longitude: 115.929015,
|
||||||
@@ -84,7 +80,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test5",
|
name: "test5",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 115.929100,
|
longitude: 115.929100,
|
||||||
@@ -96,7 +91,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test6",
|
name: "test6",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 126.587051,
|
longitude: 126.587051,
|
||||||
@@ -108,7 +102,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test7",
|
name: "test7",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 126.595051,
|
longitude: 126.595051,
|
||||||
@@ -120,7 +113,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test8",
|
name: "test8",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 125.342693,
|
longitude: 125.342693,
|
||||||
@@ -132,7 +124,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test9",
|
name: "test9",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 112.485550,
|
longitude: 112.485550,
|
||||||
@@ -144,7 +135,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test10",
|
name: "test10",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 115.928821,
|
longitude: 115.928821,
|
||||||
@@ -156,7 +146,6 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
name: "test11",
|
name: "test11",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
AppCode: "",
|
AppCode: "",
|
||||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
|
||||||
},
|
},
|
||||||
args: args{
|
args: args{
|
||||||
longitude: 115.928821,
|
longitude: 115.928821,
|
||||||
@@ -166,12 +155,55 @@ func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
n := &AnNaQiGpsClient{
|
n := NewAnNaQiGpsClient(tt.fields.AppCode)
|
||||||
AppCode: tt.fields.AppCode,
|
|
||||||
Host: tt.fields.Host,
|
|
||||||
}
|
|
||||||
gotRes, err := n.GetGpsInfo(tt.args.longitude, tt.args.latitude)
|
gotRes, err := n.GetGpsInfo(tt.args.longitude, tt.args.latitude)
|
||||||
log.Println(gotRes, err)
|
log.Println(gotRes, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAnNaQiGpsClient_GetLocation(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
AppCode string
|
||||||
|
Host string
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
city string
|
||||||
|
region string
|
||||||
|
address string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
args args
|
||||||
|
wantRes *[]Geocode
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "奥园城市天地",
|
||||||
|
fields: fields{
|
||||||
|
AppCode: "",
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
city: "广州",
|
||||||
|
region: "番禺",
|
||||||
|
address: "奥园城市天地9区2栋",
|
||||||
|
},
|
||||||
|
wantRes: nil,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
n := NewAnNaQiGpsClient(tt.fields.AppCode)
|
||||||
|
gotRes, err := n.GetLocation(tt.args.city, tt.args.region, tt.args.address)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("GetLocation() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(gotRes, tt.wantRes) {
|
||||||
|
t.Errorf("GetLocation() gotRes = %v, want %v", gotRes, tt.wantRes)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
81
gps_tool/response.go
Normal file
81
gps_tool/response.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package gps_tool
|
||||||
|
|
||||||
|
type ApiResult struct {
|
||||||
|
Data Data `json:"data"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Code int `json:"code"`
|
||||||
|
TaskNo string `json:"taskNo"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
Regeocodes []Regeocode `json:"regeocodes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Regeocode struct {
|
||||||
|
FormattedAddress string `json:"formatted_address"`
|
||||||
|
AddressComponent AddressComponent `json:"addressComponent"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AddressComponent struct {
|
||||||
|
// BusinessAreas []interface{} `json:"businessAreas"`
|
||||||
|
Country string `json:"country"`
|
||||||
|
Province string `json:"province"`
|
||||||
|
Citycode string `json:"citycode"`
|
||||||
|
City string `json:"city"`
|
||||||
|
Adcode string `json:"adcode"`
|
||||||
|
// StreetNumber StreetNumber `json:"streetNumber"`
|
||||||
|
// Towncode string `json:"towncode"`
|
||||||
|
// District string `json:"district"`
|
||||||
|
// Neighborhood Neighborhood `json:"neighborhood"`
|
||||||
|
// Township string `json:"township"`
|
||||||
|
// Building Building `json:"building"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type BusinessArea struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Location string `json:"location"`
|
||||||
|
Id string `json:"id"`
|
||||||
|
}
|
||||||
|
type StreetNumber struct {
|
||||||
|
Number string `json:"number"`
|
||||||
|
Distance string `json:"distance"`
|
||||||
|
Street string `json:"street"`
|
||||||
|
Location string `json:"location"`
|
||||||
|
Direction string `json:"direction"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Neighborhood struct {
|
||||||
|
Name interface{} `json:"name"`
|
||||||
|
Type interface{} `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Building struct {
|
||||||
|
Name interface{} `json:"name"`
|
||||||
|
Type interface{} `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LocationInfo struct {
|
||||||
|
Data LocationDatum `json:"data"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Code int `json:"code"`
|
||||||
|
TaskNo string `json:"taskNo"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LocationDatum struct {
|
||||||
|
Count int `json:"count"`
|
||||||
|
Geocodes []*Geocode `json:"geocodes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Geocode struct {
|
||||||
|
Country string `json:"country"`
|
||||||
|
FormattedAddress string `json:"formatted_address"`
|
||||||
|
City string `json:"city"`
|
||||||
|
Adcode string `json:"adcode"`
|
||||||
|
Level string `json:"level"`
|
||||||
|
Province string `json:"province"`
|
||||||
|
Citycode string `json:"citycode"`
|
||||||
|
District string `json:"district"`
|
||||||
|
Location string `json:"location"`
|
||||||
|
}
|
190
oss_tool/aliyun_oss.go
Normal file
190
oss_tool/aliyun_oss.go
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
package oss_tool
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
cf "git.ssgfgtfy.com/public/ssgf_utils/common_fun"
|
||||||
|
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||||||
|
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
|
||||||
|
"image"
|
||||||
|
"image/jpeg"
|
||||||
|
"image/png"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ALiYunOSSClient struct {
|
||||||
|
AccessKeyID string
|
||||||
|
AccessKeySecret string
|
||||||
|
Region string
|
||||||
|
ossClient *oss.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAliYunOSS 创建阿里云OSS客户端
|
||||||
|
func (c *ALiYunOSSClient) NewAliYunOSS() (err error) {
|
||||||
|
if c.AccessKeyID == "" || c.AccessKeySecret == "" {
|
||||||
|
return errors.New("请配置 oss accessKeyID accessKeySecret")
|
||||||
|
}
|
||||||
|
if c.Region == "" {
|
||||||
|
return errors.New("请配置 oss region")
|
||||||
|
}
|
||||||
|
provider := credentials.NewStaticCredentialsProvider(c.AccessKeyID, c.AccessKeySecret)
|
||||||
|
cfg := oss.LoadDefaultConfig().WithSignatureVersion(oss.SignatureVersionV4).WithCredentialsProvider(provider).WithRegion(c.Region)
|
||||||
|
c.ossClient = oss.NewClient(cfg)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildPutSignUrl 获取上传签名
|
||||||
|
func (c *ALiYunOSSClient) BuildPutSignUrl(bucket string, key string, expires time.Duration) (result *oss.PresignResult, err error) {
|
||||||
|
// 生成PutObject的预签名URL
|
||||||
|
result, err = c.ossClient.Presign(
|
||||||
|
context.Background(),
|
||||||
|
&oss.PutObjectRequest{
|
||||||
|
Bucket: oss.Ptr(bucket),
|
||||||
|
Key: oss.Ptr(key),
|
||||||
|
},
|
||||||
|
oss.PresignExpires(expires),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildPutSignUrlByPutObjectRequest 根据PutObjectRequest生成PutObject的预签名URL
|
||||||
|
func (c *ALiYunOSSClient) BuildPutSignUrlByPutObjectRequest(req *oss.PutObjectRequest, expires time.Duration) (result *oss.PresignResult, err error) {
|
||||||
|
// 生成PutObject的预签名URL
|
||||||
|
result, err = c.ossClient.Presign(
|
||||||
|
context.Background(),
|
||||||
|
req,
|
||||||
|
oss.PresignExpires(expires),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildSignUrlByGetObjectRequest 根据GetObjectRequest生成GetObject的预签名URL
|
||||||
|
func (c *ALiYunOSSClient) BuildSignUrlByGetObjectRequest(req *oss.GetObjectRequest) (result *oss.PresignResult, err error) {
|
||||||
|
// 生成PutObject的预签名URL
|
||||||
|
result, err = c.ossClient.Presign(
|
||||||
|
context.Background(),
|
||||||
|
req,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutForLocalFile 上传本地文件
|
||||||
|
func (c *ALiYunOSSClient) PutForLocalFile(bucket, key, path string) (result *oss.PutObjectResult, err error) {
|
||||||
|
// 创建上传对象的请求
|
||||||
|
putRequest := &oss.PutObjectRequest{
|
||||||
|
Bucket: oss.Ptr(bucket), // 存储空间名称
|
||||||
|
Key: oss.Ptr(key), // 对象名称
|
||||||
|
StorageClass: oss.StorageClassStandard, // 指定对象的存储类型为标准存储
|
||||||
|
ContentType: oss.Ptr("application/octet-stream"),
|
||||||
|
}
|
||||||
|
// 执行上传对象的请求
|
||||||
|
result, err = c.ossClient.PutObjectFromFile(context.Background(), putRequest, path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetObject 下载文件,返回的是一个*oss.GetObjectResult (不推荐使用,如果使用的话,需要手动调用object.Body.Close()手动关闭资源)
|
||||||
|
func (c *ALiYunOSSClient) GetObject(bucket string, key string) (object *oss.GetObjectResult, body io.ReadCloser, err error) {
|
||||||
|
getRequest := &oss.GetObjectRequest{
|
||||||
|
Bucket: oss.Ptr(bucket), // 存储空间名称
|
||||||
|
Key: oss.Ptr(key), // 对象名称
|
||||||
|
}
|
||||||
|
// 执行获取对象的操作并处理结果
|
||||||
|
result, err := c.ossClient.GetObject(context.TODO(), getRequest)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
//defer func() {
|
||||||
|
// _ = result.Body.Close() // 确保在函数结束时关闭响应体
|
||||||
|
//}()
|
||||||
|
return result, result.Body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetObjectToFile 获取对象并保存到本地
|
||||||
|
func (c *ALiYunOSSClient) GetObjectToFile(bucket string, key string, path string) (err error) {
|
||||||
|
getRequest := &oss.GetObjectRequest{
|
||||||
|
Bucket: oss.Ptr(bucket), // 存储空间名称
|
||||||
|
Key: oss.Ptr(key), // 对象名称
|
||||||
|
}
|
||||||
|
// 执行获取对象的操作并处理结果
|
||||||
|
result, err := c.ossClient.GetObject(context.TODO(), getRequest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = result.Body.Close() // 确保在函数结束时关闭响应体
|
||||||
|
}()
|
||||||
|
// 确保文件目录存在
|
||||||
|
err = cf.EnsureParentDirExists(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// 创建目标文件
|
||||||
|
file, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// 将 reader 中的内容复制到文件中
|
||||||
|
_, err = io.Copy(file, result.Body)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetObjectToImage 获取对象并保存为图片对象
|
||||||
|
func (c *ALiYunOSSClient) GetObjectToImage(bucket string, key string) (img image.Image, err error) {
|
||||||
|
getRequest := &oss.GetObjectRequest{
|
||||||
|
Bucket: oss.Ptr(bucket), // 存储空间名称
|
||||||
|
Key: oss.Ptr(key), // 对象名称
|
||||||
|
}
|
||||||
|
// 执行获取对象的操作并处理结果
|
||||||
|
result, err := c.ossClient.GetObject(context.TODO(), getRequest)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = result.Body.Close() // 确保在函数结束时关闭响应体
|
||||||
|
}()
|
||||||
|
img, _, err = image.Decode(result.Body)
|
||||||
|
if err != nil {
|
||||||
|
// 尝试 JPEG 解码
|
||||||
|
img, err = jpeg.Decode(result.Body)
|
||||||
|
if err != nil {
|
||||||
|
// 尝试 PNG 解码
|
||||||
|
img, err = png.Decode(result.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("图片解码失败(支持的格式:JPEG/PNG): %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return img, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DelObject 删除对象
|
||||||
|
func (c *ALiYunOSSClient) DelObject(bucket string, key string) (result *oss.DeleteObjectResult, err error) {
|
||||||
|
// 创建删除对象的请求
|
||||||
|
request := &oss.DeleteObjectRequest{
|
||||||
|
Bucket: oss.Ptr(bucket), // 存储空间名称
|
||||||
|
Key: oss.Ptr(key), // 对象名称
|
||||||
|
}
|
||||||
|
// 执行删除对象的操作并处理结果
|
||||||
|
result, err = c.ossClient.DeleteObject(context.TODO(), request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, err
|
||||||
|
}
|
120
oss_tool/aliyun_oss_test.go
Normal file
120
oss_tool/aliyun_oss_test.go
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
package oss_tool
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
accessKeyId = os.Getenv("OSS_ACCESS_KEY_ID")
|
||||||
|
accessKeySecret = os.Getenv("OSS_SECRET_ACCESS_KEY")
|
||||||
|
region = os.Getenv("OSS_REGION")
|
||||||
|
client = &ALiYunOSSClient{
|
||||||
|
AccessKeyID: accessKeyId,
|
||||||
|
AccessKeySecret: accessKeySecret,
|
||||||
|
Region: region,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestALiYunOSSClient_NewAliYunOSS(t *testing.T) {
|
||||||
|
err := client.NewAliYunOSS()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
t.Log(client.ossClient)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestALiYunOSSClient_BuildPutSignUrl(t *testing.T) {
|
||||||
|
err := client.NewAliYunOSS()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
sign, err := client.BuildPutSignUrl("", "test/upload/bizhi1.jpg", 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
t.Log(sign)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestALiYunOSSClient_BuildPutSignUrlByPutObjectRequest(t *testing.T) {
|
||||||
|
err := client.NewAliYunOSS()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
req := &oss.PutObjectRequest{}
|
||||||
|
req.Bucket = oss.Ptr("")
|
||||||
|
req.Key = oss.Ptr("test/upload/bizhi2.jpg")
|
||||||
|
req.ContentType = oss.Ptr("application/octet-stream")
|
||||||
|
sign, err := client.BuildPutSignUrlByPutObjectRequest(req, 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
t.Log(sign)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestALiYunOSSClient_PutForLocalFile(t *testing.T) {
|
||||||
|
err := client.NewAliYunOSS()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
result, err := client.PutForLocalFile("ssgfdown", "test/upload/bizhi2.jpg", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
t.Log(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestALiYunOSSClient_BuildSignUrlByGetObjectRequest(t *testing.T) {
|
||||||
|
err := client.NewAliYunOSS()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
req := &oss.GetObjectRequest{}
|
||||||
|
req.Bucket = oss.Ptr("ssgfdown")
|
||||||
|
req.Key = oss.Ptr("test/upload/bizhi2.jpg")
|
||||||
|
result, err := client.BuildSignUrlByGetObjectRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
t.Log(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestALiYunOSSClient_GetObjectToFile(t *testing.T) {
|
||||||
|
err := client.NewAliYunOSS()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
err = client.GetObjectToFile("", "test/upload/bizhi1.jpg", "D:/bizhi1.jpg")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
t.Log("成功")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestALiYunOSSClient_GetObjectToImage(t *testing.T) {
|
||||||
|
err := client.NewAliYunOSS()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
img, err := client.GetObjectToImage("", "test/upload/bizhi1.jpg")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
t.Log(img)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestALiYunOSSClient_DelObject(t *testing.T) {
|
||||||
|
err := client.NewAliYunOSS()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
res, err := client.DelObject("", "test/upload/bizhi2.jpg")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
t.Log(res)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user