|
@@ -2,18 +2,28 @@ package ossClient
|
|
|
|
|
|
import (
|
|
import (
|
|
"bytes"
|
|
"bytes"
|
|
|
|
+ "context"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"errors"
|
|
"errors"
|
|
|
|
+ "google.golang.org/grpc"
|
|
"io"
|
|
"io"
|
|
"log"
|
|
"log"
|
|
"mime/multipart"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http"
|
|
"net/rpc"
|
|
"net/rpc"
|
|
"strconv"
|
|
"strconv"
|
|
|
|
+ "sync"
|
|
|
|
+ "time"
|
|
|
|
|
|
"app.yhyue.com/moapp/jybase/api"
|
|
"app.yhyue.com/moapp/jybase/api"
|
|
"jygit.jydev.jianyu360.cn/BaseService/ossClient/constant"
|
|
"jygit.jydev.jianyu360.cn/BaseService/ossClient/constant"
|
|
"jygit.jydev.jianyu360.cn/BaseService/ossClient/entity"
|
|
"jygit.jydev.jianyu360.cn/BaseService/ossClient/entity"
|
|
|
|
+ "jygit.jydev.jianyu360.cn/BaseService/ossClient/pb"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+var (
|
|
|
|
+ GrpcCoon *grpc.ClientConn
|
|
|
|
+ GrpcCoonOnce = &sync.Once{}
|
|
)
|
|
)
|
|
|
|
|
|
/* restful方式上传
|
|
/* restful方式上传
|
|
@@ -52,7 +62,7 @@ func UpLoadByRestful(domain, bucketId, objectName string, stream []byte, gzip bo
|
|
}
|
|
}
|
|
|
|
|
|
// 创建 HTTP 请求
|
|
// 创建 HTTP 请求
|
|
- if respBody, err := post(domain+constant.UploadUrl, writer, body); err != nil {
|
|
|
|
|
|
+ if respBody, err := Post(domain+constant.UploadUrl, writer, body); err != nil {
|
|
reply.Error_msg = err.Error()
|
|
reply.Error_msg = err.Error()
|
|
} else {
|
|
} else {
|
|
json.Unmarshal(respBody, &reply)
|
|
json.Unmarshal(respBody, &reply)
|
|
@@ -64,7 +74,7 @@ func UpLoadByRestful(domain, bucketId, objectName string, stream []byte, gzip bo
|
|
* @param domain 域名,例如:https://ossservice.jianyu360.cn
|
|
* @param domain 域名,例如:https://ossservice.jianyu360.cn
|
|
* @param bucketId 桶id
|
|
* @param bucketId 桶id
|
|
* @param objectName 对象名称
|
|
* @param objectName 对象名称
|
|
- * @return {"error_code":0,"error_msg":"下载成功"}
|
|
|
|
|
|
+ * @return {"error_code":0,"error_msg":"下载成功","data":文件流}
|
|
*/
|
|
*/
|
|
func DownloadByRestful(domain, bucketId, objectName string) (reply *api.Result) {
|
|
func DownloadByRestful(domain, bucketId, objectName string) (reply *api.Result) {
|
|
reply = &api.Result{}
|
|
reply = &api.Result{}
|
|
@@ -73,7 +83,7 @@ func DownloadByRestful(domain, bucketId, objectName string) (reply *api.Result)
|
|
writer := multipart.NewWriter(body)
|
|
writer := multipart.NewWriter(body)
|
|
writer.WriteField("bucket_id", bucketId)
|
|
writer.WriteField("bucket_id", bucketId)
|
|
writer.WriteField("object_name", objectName)
|
|
writer.WriteField("object_name", objectName)
|
|
- if respBody, err := post(domain+constant.DownloadUrl, writer, body); err != nil {
|
|
|
|
|
|
+ if respBody, err := Post(domain+constant.DownloadUrl, writer, body); err != nil {
|
|
reply.Error_msg = err.Error()
|
|
reply.Error_msg = err.Error()
|
|
} else {
|
|
} else {
|
|
reply.Error_msg = constant.DownloadSuccess
|
|
reply.Error_msg = constant.DownloadSuccess
|
|
@@ -86,7 +96,7 @@ func DownloadByRestful(domain, bucketId, objectName string) (reply *api.Result)
|
|
* @param domain 域名,例如:https://ossservice.jianyu360.cn
|
|
* @param domain 域名,例如:https://ossservice.jianyu360.cn
|
|
* @param bucketId 桶id
|
|
* @param bucketId 桶id
|
|
* @param objectName 对象名称
|
|
* @param objectName 对象名称
|
|
- * @return {"error_code":0,"error_msg":"上传成功"}
|
|
|
|
|
|
+ * @return {"error_code":0,"error_msg":"删除成功"}
|
|
*/
|
|
*/
|
|
func DeleteByRestful(domain, bucketId, objectName string) (reply *api.Result) {
|
|
func DeleteByRestful(domain, bucketId, objectName string) (reply *api.Result) {
|
|
reply = &api.Result{}
|
|
reply = &api.Result{}
|
|
@@ -95,7 +105,7 @@ func DeleteByRestful(domain, bucketId, objectName string) (reply *api.Result) {
|
|
writer := multipart.NewWriter(body)
|
|
writer := multipart.NewWriter(body)
|
|
writer.WriteField("bucket_id", bucketId)
|
|
writer.WriteField("bucket_id", bucketId)
|
|
writer.WriteField("object_name", objectName)
|
|
writer.WriteField("object_name", objectName)
|
|
- if respBody, err := post(domain+constant.DeleteUrl, writer, body); err != nil {
|
|
|
|
|
|
+ if respBody, err := Post(domain+constant.DeleteUrl, writer, body); err != nil {
|
|
reply.Error_msg = err.Error()
|
|
reply.Error_msg = err.Error()
|
|
} else {
|
|
} else {
|
|
json.Unmarshal(respBody, &reply)
|
|
json.Unmarshal(respBody, &reply)
|
|
@@ -116,7 +126,7 @@ func GetBidDetailByRestful(domain, bucketId, objectName string) (reply *api.Resu
|
|
writer := multipart.NewWriter(body)
|
|
writer := multipart.NewWriter(body)
|
|
writer.WriteField("bucket_id", bucketId)
|
|
writer.WriteField("bucket_id", bucketId)
|
|
writer.WriteField("object_name", objectName)
|
|
writer.WriteField("object_name", objectName)
|
|
- if respBody, err := post(domain+constant.GetBidDetailUrl, writer, body); err != nil {
|
|
|
|
|
|
+ if respBody, err := Post(domain+constant.GetBidDetailUrl, writer, body); err != nil {
|
|
reply.Error_msg = err.Error()
|
|
reply.Error_msg = err.Error()
|
|
} else {
|
|
} else {
|
|
reply.Error_msg = constant.GetBidDetailSuccess
|
|
reply.Error_msg = constant.GetBidDetailSuccess
|
|
@@ -125,7 +135,7 @@ func GetBidDetailByRestful(domain, bucketId, objectName string) (reply *api.Resu
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-func post(url string, writer *multipart.Writer, body *bytes.Buffer) ([]byte, error) {
|
|
|
|
|
|
+func Post(url string, writer *multipart.Writer, body *bytes.Buffer) ([]byte, error) {
|
|
// 关闭表单写入器
|
|
// 关闭表单写入器
|
|
if err := writer.Close(); err != nil {
|
|
if err := writer.Close(); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
@@ -169,9 +179,9 @@ func post(url string, writer *multipart.Writer, body *bytes.Buffer) ([]byte, err
|
|
* @return {"error_code":0,"error_msg":"上传成功"}
|
|
* @return {"error_code":0,"error_msg":"上传成功"}
|
|
* @return error 错误信息
|
|
* @return error 错误信息
|
|
*/
|
|
*/
|
|
-func UpLoadByRpc(address string, args *entity.UploadArgs) (api.Result, error) {
|
|
|
|
- var reply api.Result
|
|
|
|
- err := rpcCall(address, "OSSService.Upload", args, &reply)
|
|
|
|
|
|
+func UpLoadByRpc(address string, args *entity.UploadArgs) (*api.Result, error) {
|
|
|
|
+ reply := &api.Result{}
|
|
|
|
+ err := RpcCall(address, "OSSService.Upload", args, reply)
|
|
return reply, err
|
|
return reply, err
|
|
}
|
|
}
|
|
|
|
|
|
@@ -181,12 +191,12 @@ func UpLoadByRpc(address string, args *entity.UploadArgs) (api.Result, error) {
|
|
* @param args 参数
|
|
* @param args 参数
|
|
* @param args.BucketID 文件名
|
|
* @param args.BucketID 文件名
|
|
* @param args.objectName 对象名称
|
|
* @param args.objectName 对象名称
|
|
- * @return {"error_code":0,"error_msg":"下载成功","data":"文件流"}
|
|
|
|
|
|
+ * @return {"error_code":0,"error_msg":"下载成功","data":文件流}
|
|
* @return error 错误信息
|
|
* @return error 错误信息
|
|
*/
|
|
*/
|
|
-func DownloadByRpc(address string, args *entity.Args) (api.Result, error) {
|
|
|
|
- var reply api.Result
|
|
|
|
- err := rpcCall(address, "OSSService.Download", args, &reply)
|
|
|
|
|
|
+func DownloadByRpc(address string, args *entity.Args) (*api.Result, error) {
|
|
|
|
+ reply := &api.Result{}
|
|
|
|
+ err := RpcCall(address, "OSSService.Download", args, reply)
|
|
return reply, err
|
|
return reply, err
|
|
}
|
|
}
|
|
|
|
|
|
@@ -198,9 +208,9 @@ func DownloadByRpc(address string, args *entity.Args) (api.Result, error) {
|
|
* @return {"error_code":0,"error_msg":"删除成功"}
|
|
* @return {"error_code":0,"error_msg":"删除成功"}
|
|
* @return error 错误信息
|
|
* @return error 错误信息
|
|
*/
|
|
*/
|
|
-func DeleteByRpc(address string, args *entity.Args) (api.Result, error) {
|
|
|
|
- var reply api.Result
|
|
|
|
- err := rpcCall(address, "OSSService.Delete", args, &reply)
|
|
|
|
|
|
+func DeleteByRpc(address string, args *entity.Args) (*api.Result, error) {
|
|
|
|
+ reply := &api.Result{}
|
|
|
|
+ err := RpcCall(address, "OSSService.Delete", args, reply)
|
|
return reply, err
|
|
return reply, err
|
|
}
|
|
}
|
|
|
|
|
|
@@ -210,25 +220,105 @@ func DeleteByRpc(address string, args *entity.Args) (api.Result, error) {
|
|
* @param args 参数
|
|
* @param args 参数
|
|
* @param args.BucketID 文件名
|
|
* @param args.BucketID 文件名
|
|
* @param args.objectName 对象名称
|
|
* @param args.objectName 对象名称
|
|
- * @return {"error_code":0,"error_msg":"下载成功","data":"正文内容"}
|
|
|
|
|
|
+ * @return {"error_code":0,"error_msg":"获取成功","data":"正文内容"}
|
|
* @return error 错误信息
|
|
* @return error 错误信息
|
|
*/
|
|
*/
|
|
-func GetBidDetailByRpc(address string, args *entity.Args) (api.Result, error) {
|
|
|
|
- var reply api.Result
|
|
|
|
- err := rpcCall(address, "OSSService.GetBidDetail", args, &reply)
|
|
|
|
|
|
+func GetBidDetailByRpc(address string, args *entity.Args) (*api.Result, error) {
|
|
|
|
+ reply := &api.Result{}
|
|
|
|
+ err := RpcCall(address, "OSSService.GetBidDetail", args, reply)
|
|
return reply, err
|
|
return reply, err
|
|
}
|
|
}
|
|
-func rpcCall(address, serviceMethod string, args any, reply any) error {
|
|
|
|
- client, err := rpc.DialHTTP("tcp", address)
|
|
|
|
|
|
+
|
|
|
|
+// rpc call
|
|
|
|
+func RpcCall(address, serviceMethod string, args any, reply any) error {
|
|
|
|
+ conn, err := rpc.DialHTTP("tcp", address)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Println(err)
|
|
log.Println(err)
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- defer client.Close()
|
|
|
|
- err = client.Call(serviceMethod, args, reply)
|
|
|
|
|
|
+ err = conn.Call(serviceMethod, args, reply)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Println(err)
|
|
log.Println(err)
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// grpc call
|
|
|
|
+func GrpcCall(address string, args *entity.Args) (pb.ServiceClient, context.Context, error) {
|
|
|
|
+ var err error
|
|
|
|
+ GrpcCoonOnce.Do(func() {
|
|
|
|
+ GrpcCoon, err = grpc.Dial(address, grpc.WithInsecure())
|
|
|
|
+ })
|
|
|
|
+ var ctx context.Context
|
|
|
|
+ if args.Timeout == -1 {
|
|
|
|
+ ctx = context.Background()
|
|
|
|
+ } else {
|
|
|
|
+ thisCtx, cancel := context.WithTimeout(context.TODO(), args.Timeout*time.Second)
|
|
|
|
+ ctx = thisCtx
|
|
|
|
+ defer cancel()
|
|
|
|
+ }
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println(err)
|
|
|
|
+ return nil, ctx, err
|
|
|
|
+ }
|
|
|
|
+ return pb.NewServiceClient(GrpcCoon), ctx, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ *grpc方式下载
|
|
|
|
+ * @param address 域名,例如:192.168.3.206:8110
|
|
|
|
+ * @param args 参数
|
|
|
|
+ * @param args.BucketID 文件名
|
|
|
|
+ * @param args.objectName 对象名称
|
|
|
|
+ * @return []byte
|
|
|
|
+ * @return error 错误信息
|
|
|
|
+ */
|
|
|
|
+func DownloadByGrpc(address string, args *entity.Args) (*pb.Response, error) {
|
|
|
|
+ serviceClient, ctx, err := GrpcCall(address, args)
|
|
|
|
+ result := &pb.Response{}
|
|
|
|
+ if err != nil {
|
|
|
|
+ return result, err
|
|
|
|
+ }
|
|
|
|
+ resp, err := serviceClient.Download(ctx, &pb.Request{BucketID: args.BucketID, ObjectName: args.ObjectName})
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println(err)
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ b := &bytes.Buffer{}
|
|
|
|
+ for {
|
|
|
|
+ reply, err := resp.Recv()
|
|
|
|
+ if err == io.EOF {
|
|
|
|
+ break
|
|
|
|
+ } else if err != nil {
|
|
|
|
+ log.Println(err)
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ result.ErrorMsg = reply.ErrorMsg
|
|
|
|
+ result.ErrorCode = reply.ErrorCode
|
|
|
|
+ b.Write(reply.Data)
|
|
|
|
+ }
|
|
|
|
+ result.Data = b.Bytes()
|
|
|
|
+ return result, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ *grpc方式获取标讯正文
|
|
|
|
+ * @param address 域名,例如:192.168.3.206:8110
|
|
|
|
+ * @param args 参数
|
|
|
|
+ * @param args.BucketID 文件名
|
|
|
|
+ * @param args.objectName 对象名称
|
|
|
|
+ * @return {"error_code":0,"error_msg":"获取成功","data":"正文内容"}
|
|
|
|
+ * @return error 错误信息
|
|
|
|
+ */
|
|
|
|
+func GetBidDetailByGrpc(address string, args *entity.Args) (*api.Result, error) {
|
|
|
|
+ result, err := DownloadByGrpc(address, args)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return &api.Result{}, err
|
|
|
|
+ }
|
|
|
|
+ return &api.Result{
|
|
|
|
+ Data: string(result.Data),
|
|
|
|
+ Error_code: int(result.ErrorCode),
|
|
|
|
+ Error_msg: result.ErrorMsg,
|
|
|
|
+ }, nil
|
|
|
|
+}
|