diff --git a/chainstorage-sdk b/chainstorage-sdk index eca97b8..db0bbf6 100755 Binary files a/chainstorage-sdk and b/chainstorage-sdk differ diff --git a/chainstorage-sdk.yaml b/chainstorage-sdk.yaml index 5b43524..5b815e2 100755 --- a/chainstorage-sdk.yaml +++ b/chainstorage-sdk.yaml @@ -1,12 +1,12 @@ server: #链存服务API地址 - chainStorageApiBaseAddress: http://127.0.0.1:8821 + chainStorageAPIEndpoint: http://127.0.0.1:8821 #CAR文件工作目录 - carFileGenerationPath: ./temp/carfile + carFileWorkPath: ./temp/carfile #CAR文件分片阈值application.yaml carFileShardingThreshold: 104857600 #链存服务API token - chainStorageApiToken: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlLZXkiOiJaT0dKNVI0QTZPU1JIWUlOTUlMQSIsImV4cCI6MTY4NDgzNDUxNSwiaWRlbnRpdHkiOnsiSWQiOjYsIk5pY2tOYW1lIjoidXNlcjEiLCJQcm9maWxlSW1hZ2UiOiIiLCJXYWxsZXRBZGRyIjoidXNlcjEiLCJ0ZXJtaW5hbCI6InBjL3NkayJ9LCJvcmlnX2lhdCI6MTY4MjI0MjUyNH0.w0lQ-RGufy5j3yMfbnL94hOvLeeCSccZbSIwd6kXvbI + chainStorageApiToken: #HTTP request user agent (K2请求需要) httpRequestUserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" #HTTP request overtime diff --git a/cmd/bucket.go b/cmd/bucket.go index ae5613e..94cad55 100644 --- a/cmd/bucket.go +++ b/cmd/bucket.go @@ -15,10 +15,11 @@ import ( "time" ) +// var ( // -//var ( // offset int -//) +// +// ) func init() { bucketListCmd.Flags().StringP("Bucket", "b", "", "桶名称") @@ -78,7 +79,7 @@ func bucketListRun(cmd *cobra.Command, args []string) { pageSize = offset } - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) @@ -308,7 +309,7 @@ func bucketCreateRun(cmd *cobra.Command, args []string) { } } - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) @@ -461,7 +462,7 @@ func bucketRemoveRun(cmd *cobra.Command, args []string) { } - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) @@ -606,7 +607,7 @@ func bucketEmptyRun(cmd *cobra.Command, args []string) { // //} - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) diff --git a/cmd/car.go b/cmd/car.go index 4bcd6d8..4b6ff24 100644 --- a/cmd/car.go +++ b/cmd/car.go @@ -68,7 +68,7 @@ func carUploadRun(cmd *cobra.Command, args []string) { processError("ls", err, args) } - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) @@ -397,7 +397,7 @@ func carImportRun(cmd *cobra.Command, args []string) { processError("ls", err, args) } - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) diff --git a/cmd/chainstorage-cmd b/cmd/chainstorage-cmd deleted file mode 100755 index 33f9d09..0000000 Binary files a/cmd/chainstorage-cmd and /dev/null differ diff --git a/cmd/chainstorage-sdk.yaml b/cmd/chainstorage-sdk.yaml index 5b704f2..ae4f28b 100755 --- a/cmd/chainstorage-sdk.yaml +++ b/cmd/chainstorage-sdk.yaml @@ -1,12 +1,12 @@ server: #链存服务API地址 - chainStorageApiBaseAddress: http://127.0.0.1:8821 + chainStorageAPIEndpoint: http://127.0.0.1:8821 #CAR文件工作目录 - carFileGenerationPath: ./temp/carfile + carFileWorkPath: ./temp/carfile #CAR文件分片阈值application.yaml carFileShardingThreshold: 104857600 #链存服务API token - chainStorageApiToken: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlLZXkiOiJaT0dKNVI0QTZPU1JIWUlOTUlMQSIsImV4cCI6MTY4NDgzNDUxNSwiaWRlbnRpdHkiOnsiSWQiOjYsIk5pY2tOYW1lIjoidXNlcjEiLCJQcm9maWxlSW1hZ2UiOiIiLCJXYWxsZXRBZGRyIjoidXNlcjEiLCJ0ZXJtaW5hbCI6InBjL3NkayJ9LCJvcmlnX2lhdCI6MTY4MjI0MjUyNH0.w0lQ-RGufy5j3yMfbnL94hOvLeeCSccZbSIwd6kXvbI + chainStorageApiToken: #HTTP request user agent (K2请求需要) httpRequestUserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" #HTTP request user agent (K2请求需要) diff --git a/cmd/cmd b/cmd/cmd deleted file mode 100755 index b03bb9f..0000000 Binary files a/cmd/cmd and /dev/null differ diff --git a/cmd/object.go b/cmd/object.go index c98766e..6dd7a95 100644 --- a/cmd/object.go +++ b/cmd/object.go @@ -106,7 +106,7 @@ func objectListRun(cmd *cobra.Command, args []string) { pageSize = offset } - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) @@ -352,7 +352,7 @@ func objectRenameRun(cmd *cobra.Command, args []string) { } - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) @@ -549,7 +549,7 @@ func objectRemoveRun(cmd *cobra.Command, args []string) { // //} - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) @@ -743,7 +743,7 @@ func objectDownloadRun(cmd *cobra.Command, args []string) { // //} - sdk, err := chainstoragesdk.New() + sdk, err := chainstoragesdk.New(&applicationConfig) if err != nil { //todo: log detail error? //fmt.Printf("error:%+v\n", err) diff --git a/cmd/root.go b/cmd/root.go index 33f2214..1355520 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,10 +2,13 @@ package main import ( "fmt" + chainstoragesdk "github.com/paradeum-team/chainstorage-sdk/sdk" "github.com/spf13/cobra" "os" ) +var applicationConfig chainstoragesdk.ApplicationConfig + var cmdDescription string = ` USAGE gcscmd - Golang ChainStorage Command line tool @@ -56,6 +59,7 @@ var rootCmd = &cobra.Command{ } func Execute() { + applicationConfig = chainstoragesdk.ApplicationConfig{} rootCmd.AddCommand( bucketListCmd, diff --git a/main.go b/main.go index b9314d8..36bc97f 100644 --- a/main.go +++ b/main.go @@ -18,9 +18,11 @@ func main() { // //fmt.Printf("response:%+v\n", response) - // region 桶数据 + config := chainstoragesdk.ApplicationConfig{} - sdk, err := chainstoragesdk.New() + // region 桶数据 + //sdkCfgFile := "" + sdk, err := chainstoragesdk.New(&config) if err != nil { fmt.Printf("error:%+v\n", err) return @@ -292,7 +294,7 @@ func main() { // // // 请求Url // urlQuery = strings.TrimSuffix(urlQuery, "&") -// apiBaseAddress := conf.myConfig.chainStorageApiBaseAddress +// apiBaseAddress := conf.myConfig.chainStorageAPIEndpoint // apiPath := "api/v1/apiKeys" // apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) // @@ -347,7 +349,7 @@ func main() { // // // 请求Url // urlQuery = strings.TrimSuffix(urlQuery, "&") -// apiBaseAddress := conf.myConfig.chainStorageApiBaseAddress +// apiBaseAddress := conf.myConfig.chainStorageAPIEndpoint // apiPath := "api/v1/buckets" // apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) // @@ -396,7 +398,7 @@ func main() { // } // // // 请求Url -// apiBaseAddress := conf.myConfig.chainStorageApiBaseAddress +// apiBaseAddress := conf.myConfig.chainStorageAPIEndpoint // apiPath := "api/v1/bucket" // apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) // @@ -437,7 +439,7 @@ func main() { // } // // // 请求Url -// apiBaseAddress := conf.myConfig.chainStorageApiBaseAddress +// apiBaseAddress := conf.myConfig.chainStorageAPIEndpoint // apiPath := "api/v1/bucket/status/clean" // apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) // @@ -474,7 +476,7 @@ func main() { // //todo: bucket id check? // // // 请求Url -// apiBaseAddress := conf.myConfig.chainStorageApiBaseAddress +// apiBaseAddress := conf.myConfig.chainStorageAPIEndpoint // apiPath := fmt.Sprintf("api/v1/bucket/%d", bucketId) // apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) // @@ -533,7 +535,7 @@ func main() { // // // 请求Url // urlQuery = strings.TrimSuffix(urlQuery, "&") -// apiBaseAddress := conf.myConfig.chainStorageApiBaseAddress +// apiBaseAddress := conf.myConfig.chainStorageAPIEndpoint // apiPath := "api/v1/objects/search" // apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) // @@ -577,7 +579,7 @@ func main() { // } // // // 请求Url -// apiBaseAddress := conf.myConfig.chainStorageApiBaseAddress +// apiBaseAddress := conf.myConfig.chainStorageAPIEndpoint // apiPath := "api/v1/object" // apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) // @@ -625,7 +627,7 @@ func main() { // } // // // 请求Url -// apiBaseAddress := conf.myConfig.chainStorageApiBaseAddress +// apiBaseAddress := conf.myConfig.chainStorageAPIEndpoint // apiPath := fmt.Sprintf("api/v1/object/name/%d", objectId) // apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) // @@ -671,7 +673,7 @@ func main() { // } // // // 请求Url -// apiBaseAddress := conf.myConfig.chainStorageApiBaseAddress +// apiBaseAddress := conf.myConfig.chainStorageAPIEndpoint // apiPath := fmt.Sprintf("api/v1/object/mark/%d", objectId) // apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) // diff --git a/sdk/bucket.go b/sdk/bucket.go index 45b9d91..56dc338 100644 --- a/sdk/bucket.go +++ b/sdk/bucket.go @@ -48,8 +48,8 @@ func (b *Bucket) GetBucketList(bucketName string, pageSize, pageIndex int) (mode // 请求Url urlQuery = strings.TrimSuffix(urlQuery, "&") - //apiBaseAddress := conf.cssConfig.ChainStorageApiBaseAddress - apiBaseAddress := b.Config.ChainStorageApiBaseAddress + //apiBaseAddress := conf.cssConfig.ChainStorageApiEndpoint + apiBaseAddress := b.Config.ChainStorageApiEndpoint apiPath := "api/v1/buckets" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -108,7 +108,7 @@ func (b *Bucket) CreateBucket(bucketName string, storageNetworkCode, bucketPrinc } // 请求Url - apiBaseAddress := b.Config.ChainStorageApiBaseAddress + apiBaseAddress := b.Config.ChainStorageApiEndpoint apiPath := "api/v1/bucket" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -151,7 +151,7 @@ func (b *Bucket) EmptyBucket(bucketId int) (model.BucketEmptyResponse, error) { } // 请求Url - apiBaseAddress := b.Config.ChainStorageApiBaseAddress + apiBaseAddress := b.Config.ChainStorageApiEndpoint apiPath := "api/v1/bucket/status/clean" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -200,7 +200,7 @@ func (b *Bucket) RemoveBucket(bucketId int, autoEmptyBucketData bool) (model.Buc } // 请求Url - apiBaseAddress := b.Config.ChainStorageApiBaseAddress + apiBaseAddress := b.Config.ChainStorageApiEndpoint apiPath := fmt.Sprintf("api/v1/bucket/%d", bucketId) apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -237,7 +237,7 @@ func (b *Bucket) GetBucketByName(bucketName string) (model.BucketCreateResponse, return response, err } - apiBaseAddress := b.Config.ChainStorageApiBaseAddress + apiBaseAddress := b.Config.ChainStorageApiEndpoint apiPath := fmt.Sprintf("api/v1/bucket/name/%s", url.QueryEscape(bucketName)) apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) diff --git a/sdk/car.go b/sdk/car.go index ee9cf7c..9e7904b 100644 --- a/sdk/car.go +++ b/sdk/car.go @@ -44,7 +44,7 @@ type Car struct { // 创建CAR文件 func (c *Car) CreateCarFile(dataPath string, fileDestination string) error { ctx := context.Background() - carVersion := 1 + carVersion := c.Config.CarVersion return createCar(ctx, carVersion, fileDestination, dataPath) } @@ -63,7 +63,7 @@ func (c *Car) ReferenceObject(req *model.CarFileUploadReq) (model.ObjectCreateRe response := model.ObjectCreateResponse{} // 请求Url - apiBaseAddress := c.Config.ChainStorageApiBaseAddress + apiBaseAddress := c.Config.ChainStorageApiEndpoint apiPath := "api/v1/upload/car/reference" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -119,7 +119,7 @@ func (c *Car) UploadCarFile(req *model.CarFileUploadReq) (model.ObjectCreateResp response := model.ObjectCreateResponse{} // 请求Url - apiBaseAddress := c.Config.ChainStorageApiBaseAddress + apiBaseAddress := c.Config.ChainStorageApiEndpoint apiPath := "api/v1/upload/car/file" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -175,7 +175,7 @@ func (c *Car) UploadShardingCarFile(req *model.CarFileUploadReq) (model.Sharding response := model.ShardingCarFileUploadResponse{} // 请求Url - apiBaseAddress := c.Config.ChainStorageApiBaseAddress + apiBaseAddress := c.Config.ChainStorageApiEndpoint apiPath := "api/v1/upload/car/shard" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -234,7 +234,7 @@ func (c *Car) VerifyShardingCarFiles(req *model.CarFileUploadReq) (model.Shardin response := model.ShardingCarFilesVerifyResponse{} // 请求Url - apiBaseAddress := c.Config.ChainStorageApiBaseAddress + apiBaseAddress := c.Config.ChainStorageApiEndpoint apiPath := "api/v1/upload/car/verify" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -291,7 +291,7 @@ func (c *Car) ConfirmShardingCarFiles(req *model.CarFileUploadReq) (model.Object response := model.ObjectCreateResponse{} // 请求Url - apiBaseAddress := c.Config.ChainStorageApiBaseAddress + apiBaseAddress := c.Config.ChainStorageApiEndpoint apiPath := "api/v1/upload/car/confirm" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -301,6 +301,7 @@ func (c *Car) ConfirmShardingCarFiles(req *model.CarFileUploadReq) (model.Object objectName := req.ObjectName objectTypeCode := req.ObjectTypeCode shardingAmount := req.ShardingAmount + objectSize := req.ObjectSize //fileDestination := req.FileDestination //params := map[string]string{ @@ -315,6 +316,7 @@ func (c *Car) ConfirmShardingCarFiles(req *model.CarFileUploadReq) (model.Object "objectName": objectName, "objectTypeCode": objectTypeCode, "shardingAmount": shardingAmount, + "objectSize": objectSize, } // API调用 @@ -554,23 +556,23 @@ func (c *Car) GenerateTempFileName(prefix, suffix string) string { randBytes := make([]byte, 16) rand.Read(randBytes) - carFileGenerationPath := c.Config.CarFileGenerationPath - if _, err := os.Stat(carFileGenerationPath); os.IsNotExist(err) { - _ = os.MkdirAll(carFileGenerationPath, os.ModePerm) + carFileWorkPath := c.Config.CarFileWorkPath + if _, err := os.Stat(carFileWorkPath); os.IsNotExist(err) { + _ = os.MkdirAll(carFileWorkPath, os.ModePerm) } //return filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+suffix) - return filepath.Join(carFileGenerationPath, prefix+hex.EncodeToString(randBytes)+suffix) + return filepath.Join(carFileWorkPath, prefix+hex.EncodeToString(randBytes)+suffix) } func (c *Car) generateFileName(prefix, suffix string) string { - carFileGenerationPath := c.Config.CarFileGenerationPath - if _, err := os.Stat(carFileGenerationPath); os.IsNotExist(err) { - _ = os.MkdirAll(carFileGenerationPath, os.ModePerm) + carFileWorkPath := c.Config.CarFileWorkPath + if _, err := os.Stat(carFileWorkPath); os.IsNotExist(err) { + _ = os.MkdirAll(carFileWorkPath, os.ModePerm) } //return filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+suffix) - return filepath.Join(carFileGenerationPath, prefix+suffix) + return filepath.Join(carFileWorkPath, prefix+suffix) } func (c *Car) ParseCarFile(carFilePath string, rootLink *model.RootLink) error { @@ -862,7 +864,7 @@ func (c *Car) UploadCarFileExt(req *model.CarFileUploadReq, extReader io.Reader) response := model.ObjectCreateResponse{} // 请求Url - apiBaseAddress := c.Config.ChainStorageApiBaseAddress + apiBaseAddress := c.Config.ChainStorageApiEndpoint apiPath := "api/v1/upload/car/file" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -918,7 +920,7 @@ func (c *Car) UploadShardingCarFileExt(req *model.CarFileUploadReq, extReader io response := model.ShardingCarFileUploadResponse{} // 请求Url - apiBaseAddress := c.Config.ChainStorageApiBaseAddress + apiBaseAddress := c.Config.ChainStorageApiEndpoint apiPath := "api/v1/upload/car/shard" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -971,3 +973,118 @@ func (c *Car) UploadShardingCarFileExt(req *model.CarFileUploadReq, extReader io return response, nil } + +// 导入CAR文件 +func (c *Car) ImportCarFileExt(req *model.CarFileUploadReq, extReader io.Reader) (model.ObjectCreateResponse, error) { + response := model.ObjectCreateResponse{} + + // 请求Url + apiBaseAddress := c.Config.ChainStorageApiEndpoint + apiPath := "api/v1/import/car/file" + apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) + + bucketId := req.BucketId + rawSha256 := req.RawSha256 + objectCid := req.ObjectCid + objectName := req.ObjectName + fileDestination := req.FileDestination + carFileCid := req.CarFileCid + objectSize := strconv.FormatInt(req.ObjectSize, 10) + + params := map[string]string{ + "bucketId": strconv.Itoa(bucketId), + "rawSha256": rawSha256, + "objectCid": objectCid, + "carFileCid": carFileCid, + "objectName": objectName, + "objectSize": objectSize, + } + //params := map[string]interface{}{ + // "bucketId": bucketId, + // "rawSha256": rawSha256, + // "objectCid": objectCid, + //} + + // API调用 + httpStatus, body, err := c.Client.RestyPostFormExt(objectName, fileDestination, params, apiUrl, extReader) + if err != nil { + c.logger.Errorf(fmt.Sprintf("API:UploadCarFile:HttpPost, apiUrl:%s, params:%+v, httpStatus:%d, err:%+v\n", apiUrl, params, httpStatus, err)) + + return response, err + } + + if httpStatus != http.StatusOK { + c.logger.Errorf(fmt.Sprintf("API:UploadCarFile:HttpPost, apiUrl:%s, params:%+v, httpStatus:%d, body:%s\n", apiUrl, params, httpStatus, string(body))) + + return response, errors.New(string(body)) + } + + // 响应数据解析 + err = json.Unmarshal(body, &response) + if err != nil { + c.logger.Errorf(fmt.Sprintf("API:UploadCarFile:JsonUnmarshal, body:%s, err:%+v\n", string(body), err)) + + return response, err + } + + return response, nil +} + +// 导入CAR文件分片 +func (c *Car) ImportShardingCarFileExt(req *model.CarFileUploadReq, extReader io.Reader) (model.ShardingCarFileUploadResponse, error) { + response := model.ShardingCarFileUploadResponse{} + + // 请求Url + apiBaseAddress := c.Config.ChainStorageApiEndpoint + apiPath := "api/v1/import/car/shard" + apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) + + bucketId := req.BucketId + rawSha256 := req.RawSha256 + objectCid := req.ObjectCid + objectName := req.ObjectName + fileDestination := req.FileDestination + shardingSha256 := req.ShardingSha256 + shardingNo := req.ShardingNo + carFileCid := req.CarFileCid + objectSize := strconv.FormatInt(req.ObjectSize, 10) + + params := map[string]string{ + "bucketId": strconv.Itoa(bucketId), + "rawSha256": rawSha256, + "objectCid": objectCid, + "shardingSha256": shardingSha256, + "shardingNo": strconv.Itoa(shardingNo), + "carFileCid": carFileCid, + "objectSize": objectSize, + } + //params := map[string]interface{}{ + // "bucketId": bucketId, + // "rawSha256": rawSha256, + // "objectCid": objectCid, + //} + + // API调用 + httpStatus, body, err := c.Client.RestyPostFormExt(objectName, fileDestination, params, apiUrl, extReader) + if err != nil { + c.logger.Errorf(fmt.Sprintf("API:UploadShardingCarFile:HttpPost, apiUrl:%s, params:%+v, httpStatus:%d, err:%+v\n", apiUrl, params, httpStatus, err)) + + return response, err + } + + if httpStatus != http.StatusOK { + c.logger.Errorf(fmt.Sprintf("API:UploadShardingCarFile:HttpPost, apiUrl:%s, params:%+v, httpStatus:%d, body:%s\n", apiUrl, params, httpStatus, string(body))) + + return response, errors.New(string(body)) + } + + // 响应数据解析 + err = json.Unmarshal(body, &response) + if err != nil { + c.logger.Errorf(fmt.Sprintf("API:UploadShardingCarFile:JsonUnmarshal, body:%s, err:%+v\n", string(body), err)) + + return response, err + } + + return response, nil +} diff --git a/sdk/client.go b/sdk/client.go index 3d274d5..50082f8 100644 --- a/sdk/client.go +++ b/sdk/client.go @@ -1,6 +1,11 @@ package chainstoragesdk import ( + "encoding/json" + "errors" + "fmt" + "github.com/paradeum-team/chainstorage-sdk/sdk/model" + "net/http" "sync" ) @@ -18,10 +23,10 @@ type CssClient struct { Car *Car } -func newClient() (*CssClient, error) { +func newClient(config *ApplicationConfig) (*CssClient, error) { var err error once.Do(func() { - initConfig() + initConfig(config) mClient = &CssClient{} mClient.Config = &cssConfig @@ -38,7 +43,40 @@ func newClient() (*CssClient, error) { return mClient, err } -func New() (*CssClient, error) { +func New(config *ApplicationConfig) (*CssClient, error) { - return newClient() + return newClient(config) +} + +func (c *CssClient) GetIpfsVersion() (model.VersionResponse, error) { + response := model.VersionResponse{} + + // 请求Url + apiBaseAddress := c.Config.ChainStorageApiEndpoint + apiPath := "ipfsVersion" + apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) + + // API调用 + httpStatus, body, err := c.httpClient.RestyGet(apiUrl) + if err != nil { + c.Logger.logger.Errorf(fmt.Sprintf("API:GetObjectByName:HttpGet, apiUrl:%s, httpStatus:%d, err:%+v\n", apiUrl, httpStatus, err)) + + return response, err + } + + if httpStatus != http.StatusOK { + c.Logger.logger.Errorf(fmt.Sprintf("API:GetObjectByName:HttpGet, apiUrl:%s, httpStatus:%d, body:%s\n", apiUrl, httpStatus, string(body))) + + return response, errors.New(string(body)) + } + + // 响应数据解析 + err = json.Unmarshal(body, &response) + if err != nil { + c.Logger.logger.Errorf(fmt.Sprintf("API:GetObjectByName:JsonUnmarshal, body:%s, err:%+v\n", string(body), err)) + + return response, err + } + + return response, nil } diff --git a/sdk/code/error.go b/sdk/code/error.go index 41847ee..6a75836 100644 --- a/sdk/code/error.go +++ b/sdk/code/error.go @@ -70,6 +70,7 @@ const ( errCarUploadFileChunkCarFileFail errCarUploadFileReferenceObjcetFail errCarUploadFileCidNotEqualRawCid + errCarUploadFileInvalidDataFolder ) var ( @@ -133,6 +134,7 @@ var ( ErrCarUploadFileComputeCarFileHashFail = NewBizError(errCarUploadFileComputeCarFileHashFail, "CAR文件HASH计算失败", "Fail to compute CAR file HASH") ErrCarUploadFileChunkCarFileFail = NewBizError(errCarUploadFileChunkCarFileFail, "生成CAR文件分片操作失败", "Fail to chunk CAR file") ErrCarUploadFileReferenceObjcetFail = NewBizError(errCarUploadFileReferenceObjcetFail, "执行CID秒传操作失败", "Fail to reference object by CID") + ErrCarUploadFileInvalidDataFolder = NewBizError(errCarUploadFileInvalidDataFolder, "上传目录为空或者目录中的数据无效", "Uploading folder is empty, or uploading data is invalid in the folder") ////登录注册 //ErrWalletInvalidFailed = NewBizError(errLogin, "钱包地址非法空", "Invalid wallet") diff --git a/sdk/config.go b/sdk/config.go index 0d7d3c1..1b393e3 100644 --- a/sdk/config.go +++ b/sdk/config.go @@ -26,19 +26,25 @@ var cssLoggerConfig LoggerConf type Configuration struct { // 链存服务API地址 - ChainStorageApiBaseAddress string `profile:"chainStorageApiBaseAddress" profileDefault:"http://127.0.0.1:8821" json:"chainStorageApiBaseAddress"` + ChainStorageApiEndpoint string `profile:"chainStorageApiEndpoint" profileDefault:"http://127.0.0.1:8821" json:"chainStorageApiEndpoint"` // CAR文件工作目录 - CarFileGenerationPath string `profile:"carFileGenerationPath" profileDefault:"./temp/carfile" json:"carFileGenerationPath"` + CarFileWorkPath string `profile:"carFileWorkPath" profileDefault:"./tmp/carfile" json:"carFileWorkPath"` // CAR文件分片阈值 - CarFileShardingThreshold int `profile:"carFileShardingThreshold" profileDefault:"1048576" json:"carFileShardingThreshold"` + CarFileShardingThreshold int `profile:"carFileShardingThreshold" profileDefault:"10485760" json:"carFileShardingThreshold"` // 链存服务API token ChainStorageApiToken string `profile:"chainStorageApiToken" profileDefault:"" json:"chainStorageApiToken"` // HTTP request user agent (K2请求需要) HttpRequestUserAgent string `profile:"httpRequestUserAgent" profileDefault:"" json:"httpRequestUserAgent"` + + // HTTP request user agent (K2请求需要) + HttpRequestOvertime int `profile:"httpRequestOvertime" profileDefault:"30" json:"httpRequestOvertime"` + + // CAR version + CarVersion int `profile:"carVersion" profileDefault:"1" json:"carVersion"` } type LoggerConf struct { @@ -55,13 +61,73 @@ type ApplicationConfig struct { Logger LoggerConf `profile:"logger"` } -func initConfig() { +func initConfig(config *ApplicationConfig) { + cssConfig = config.Server + cssLoggerConfig = config.Logger + + //check chain-storage-api base address + if len(cssConfig.ChainStorageApiEndpoint) > 0 { + chainStorageAPIEndpoint := cssConfig.ChainStorageApiEndpoint + if !strings.HasPrefix(chainStorageAPIEndpoint, "http") { + fmt.Println("ERROR: invalid chain-storage-api endpoint in Configuration, chain-storage-api endpoint must be a valid http/https url, exiting") + os.Exit(1) + } + + if !strings.HasSuffix(chainStorageAPIEndpoint, "/") { + cssConfig.ChainStorageApiEndpoint += "/" + } + } else { + fmt.Println("ERROR: no chain-storage-api endpoint provided in Configuration, at least 1 valid http/https chain-storage-api endpoint must be given, exiting") + os.Exit(1) + } + + if len(cssConfig.ChainStorageApiToken) == 0 { + fmt.Println("ERROR: invalid chain-storage-api token in Configuration, chain-storage-api token must not be empty") + os.Exit(1) + } else if !strings.HasPrefix(cssConfig.ChainStorageApiToken, "Bearer ") { + cssConfig.ChainStorageApiToken = "Bearer " + cssConfig.ChainStorageApiToken + } + + // CAR文件分片阈值,缺省10MB + carFileShardingThreshold := cssConfig.CarFileShardingThreshold + if carFileShardingThreshold <= 0 { + cssConfig.CarFileShardingThreshold = 10485760 + } + + // CAR文件工作目录 + carFileWorkPath := cssConfig.CarFileWorkPath + if len(carFileWorkPath) == 0 { + cssConfig.CarFileWorkPath = `./tmp/carfile` + } + + // HTTP request user agent (K2请求需要) + httpRequestUserAgent := cssConfig.HttpRequestUserAgent + if len(httpRequestUserAgent) == 0 { + cssConfig.HttpRequestUserAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36` + } + + // HTTP request overtime + httpRequestOvertime := cssConfig.HttpRequestOvertime + if httpRequestOvertime <= 0 { + cssConfig.HttpRequestOvertime = 30 + } + + // CAR version + carVersion := cssConfig.CarVersion + if carVersion <= 0 { + cssConfig.CarVersion = 1 + } +} + +func initConfigWithConfigFile(configFile string) { //rand.Seed(time.Now().UnixNano()) //if len(configFile) == 0 { // configFile = "./github.com/paradeum-team/chainstorage-sdk.yaml" //} + if len(configFile) == 0 { + configFile = "./chainstorage-sdk.yaml" + } - configFile := "./chainstorage-sdk.yaml" config, err := gprofile.Profile(&ApplicationConfig{}, configFile, true) if err != nil { fmt.Errorf("Profile execute error", err) @@ -78,15 +144,15 @@ func initConfig() { //} //check chain-storage-api base address - if len(cssConfig.ChainStorageApiBaseAddress) > 0 { - chainStorageApiBaseAddress := cssConfig.ChainStorageApiBaseAddress - if !strings.HasPrefix(chainStorageApiBaseAddress, "http") { + if len(cssConfig.ChainStorageApiEndpoint) > 0 { + chainStorageAPIEndpoint := cssConfig.ChainStorageApiEndpoint + if !strings.HasPrefix(chainStorageAPIEndpoint, "http") { fmt.Println("ERROR: invalid chain-storage-api base address in Configuration file, chain-storage-api base address must be a valid http/https url, exiting") os.Exit(1) } - if !strings.HasSuffix(chainStorageApiBaseAddress, "/") { - cssConfig.ChainStorageApiBaseAddress += "/" + if !strings.HasSuffix(chainStorageAPIEndpoint, "/") { + cssConfig.ChainStorageApiEndpoint += "/" } } else { fmt.Println("ERROR: no chain-storage-api base address provided in Configuration file, at least 1 valid http/https chain-storage-api base address must be given, exiting") @@ -114,7 +180,7 @@ func initConfig() { } -func InitConfig2() { +func InitConfigWithDefault() { //rand.Seed(time.Now().UnixNano()) config, err := gprofile.Profile(&ApplicationConfig{}, "./chainstorage-sdk.yaml", true) if err != nil { @@ -131,15 +197,15 @@ func InitConfig2() { //} //check chain-storage-api base address - if len(cssConfig.ChainStorageApiBaseAddress) > 0 { - chainStorageApiBaseAddress := cssConfig.ChainStorageApiBaseAddress - if !strings.HasPrefix(chainStorageApiBaseAddress, "http") { + if len(cssConfig.ChainStorageApiEndpoint) > 0 { + chainStorageAPIEndpoint := cssConfig.ChainStorageApiEndpoint + if !strings.HasPrefix(chainStorageAPIEndpoint, "http") { fmt.Println("ERROR: invalid chain-storage-api base address in Configuration file, chain-storage-api base address must be a valid http/https url, exiting") os.Exit(1) } - if !strings.HasSuffix(chainStorageApiBaseAddress, "/") { - cssConfig.ChainStorageApiBaseAddress += "/" + if !strings.HasSuffix(chainStorageAPIEndpoint, "/") { + cssConfig.ChainStorageApiEndpoint += "/" } } else { fmt.Println("ERROR: no chain-storage-api base address provided in Configuration file, at least 1 valid http/https chain-storage-api base address must be given, exiting") diff --git a/sdk/model/api_key.go b/sdk/model/api_key.go index f810c28..fbd37a8 100644 --- a/sdk/model/api_key.go +++ b/sdk/model/api_key.go @@ -4,7 +4,7 @@ import "time" type ApiKey struct { Id int `json:"id" comment:"ApiKeyID"` - UserId int `json:"userId" comment:"用户ID"` + UserId int `json:"-" comment:"用户ID"` ApiName string `json:"apiName" comment:"Api名称(3-63字长度限制)"` ApiKey string `json:"apiKey" comment:"comment:ApiKey(默认20字节)"` ApiSecret string `json:"apiSecret" comment:"Api密钥(默认40字节)"` diff --git a/sdk/model/bucket.go b/sdk/model/bucket.go index 00c36c5..a294002 100644 --- a/sdk/model/bucket.go +++ b/sdk/model/bucket.go @@ -6,7 +6,7 @@ import ( type Bucket struct { Id int `json:"id" comment:"桶ID"` - UserId int `json:"userId" comment:"用户ID"` + UserId int `json:"-" comment:"用户ID"` BucketName string `json:"bucketName" comment:"桶名称(3-63字长度限制)"` StorageNetworkCode int `json:"storageNetworkCode" comment:"存储网络编码(10001-IPFS)"` BucketPrincipleCode int `json:"bucketPrincipleCode" comment:"桶策略编码(10001-公开,10000-私有)"` diff --git a/sdk/model/common.go b/sdk/model/common.go new file mode 100644 index 0000000..84093b7 --- /dev/null +++ b/sdk/model/common.go @@ -0,0 +1,11 @@ +package model + +type VersionResponse struct { + Code int `json:"code"` + Msg string `json:"msg"` + Data Version `json:"data"` +} +type Version struct { + Code int `json:"code"` + Version string `json:"version"` +} diff --git a/sdk/model/object.go b/sdk/model/object.go index 27fe699..8a39cbb 100644 --- a/sdk/model/object.go +++ b/sdk/model/object.go @@ -6,7 +6,7 @@ import ( type Object struct { Id int `json:"id" comment:"对象ID"` - UserId int `json:"userId" comment:"用户ID"` + UserId int `json:"-" comment:"用户ID"` BucketId int `json:"bucketId" comment:"桶主键"` ObjectName string `json:"objectName" comment:"对象名称(255字限制)"` ObjectTypeCode int `json:"objectTypeCode" comment:"对象类型编码"` diff --git a/sdk/object.go b/sdk/object.go index f501280..edab3c1 100644 --- a/sdk/object.go +++ b/sdk/object.go @@ -47,7 +47,7 @@ func (o *Object) GetObjectList(bucketId int, objectItem string, pageSize, pageIn // 请求Url urlQuery = strings.TrimSuffix(urlQuery, "&") - apiBaseAddress := o.Config.ChainStorageApiBaseAddress + apiBaseAddress := o.Config.ChainStorageApiEndpoint apiPath := "api/v1/objects/search" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -95,7 +95,7 @@ func (o *Object) RemoveObject(objectIds []int) (model.ObjectRemoveResponse, erro } // 请求Url - apiBaseAddress := o.Config.ChainStorageApiBaseAddress + apiBaseAddress := o.Config.ChainStorageApiEndpoint apiPath := "api/v1/object" apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -148,7 +148,7 @@ func (o *Object) RenameObject(objectId int, objectName string, isOverwrite bool) } // 请求Url - apiBaseAddress := o.Config.ChainStorageApiBaseAddress + apiBaseAddress := o.Config.ChainStorageApiEndpoint apiPath := fmt.Sprintf("api/v1/object/name/%d", objectId) apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -196,7 +196,7 @@ func (o *Object) MarkObject(objectId int, isMarked bool) (model.ObjectMarkRespon } // 请求Url - apiBaseAddress := o.Config.ChainStorageApiBaseAddress + apiBaseAddress := o.Config.ChainStorageApiEndpoint apiPath := fmt.Sprintf("api/v1/object/mark/%d", objectId) apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -243,7 +243,7 @@ func (o *Object) IsExistObjectByCid(objectCid string) (model.ObjectExistResponse urlQuery := url.QueryEscape(objectCid) // 请求Url - apiBaseAddress := o.Config.ChainStorageApiBaseAddress + apiBaseAddress := o.Config.ChainStorageApiEndpoint apiPath := fmt.Sprintf("api/v1/object/existCid/%s", urlQuery) apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) @@ -290,7 +290,7 @@ func (o *Object) GetObjectByName(bucketId int, objectName string) (model.ObjectC urlQuery += fmt.Sprintf("objectName=%s", url.QueryEscape(objectName)) // 请求Url - apiBaseAddress := o.Config.ChainStorageApiBaseAddress + apiBaseAddress := o.Config.ChainStorageApiEndpoint apiPath := fmt.Sprintf("api/v1/object/find/name%s", urlQuery) apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath) diff --git a/sdk/pldlogger.go b/sdk/pldlogger.go index 648404f..a999769 100755 --- a/sdk/pldlogger.go +++ b/sdk/pldlogger.go @@ -40,7 +40,7 @@ func newLogger(cfg *LoggerConf) *PldLogger { return } - logInfoPath := pldLoggerInstance.createGinSysLogPath("chainstorage") + logInfoPath := pldLoggerInstance.createGinSysLogPath("chainstorage-sdk") /*file, err := os.OpenFile(logInfoPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777) if err != nil { log.Printf("ERROR: %s\n", fmt.Sprintf("%s append|create failed:%v", logInfoPath, err))