3 // ========================LICENSE_START=================================
6 // Copyright (C) 2023: Nordix Foundation
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 // ========================LICENSE_END===================================
20 package miniocollector
28 "main/common/dataTypes"
29 "main/components/xmltransform"
35 jsoniter "github.com/json-iterator/go"
36 "github.com/minio/minio-go/v7"
37 "github.com/minio/minio-go/v7/pkg/credentials"
38 log "github.com/sirupsen/logrus"
42 func XmlToJsonConv(evtData *dataTypes.XmlFileEventHeader) string {
43 filestoreUser := os.Getenv("FILESTORE_USER")
44 filestorePwd := os.Getenv("FILESTORE_PWD")
45 filestoreServer := os.Getenv("FILESTORE_SERVER")
47 s3Client, err := minio.New(filestoreServer, &minio.Options{
48 Creds: credentials.NewStaticV4(filestoreUser, filestorePwd, ""),
54 expiry := time.Second * 24 * 60 * 60 // 1 day.
55 objectName := evtData.Name
56 bucketName := evtData.ObjectStoreBucket
57 compresion := evtData.Compression
58 reqParams := make(url.Values)
60 xmlh, err := jsoniter.Marshal(evtData)
62 fmt.Printf("Error: %s", err)
66 // Generate presigned GET url with lambda function
67 presignedURL, err := s3Client.PresignedGetObject(context.Background(), bucketName, objectName, expiry, reqParams)
71 fileBytes := xmltransform.Convert(presignedURL.String(), compresion, string(xmlh))
72 newObjectName := objectName + "kafka-producer-pm-xml2json-0.json.gz"
74 err = gzipWrite(&buf, &fileBytes)
75 uploadObject(s3Client, buf.Bytes(), newObjectName, "pm-files-json")
82 func uploadObject(mc *minio.Client, b []byte, objectName string, fsbucket string) {
83 contentType := "application/json"
84 if strings.HasSuffix(objectName, ".gz") {
85 contentType = "application/gzip"
88 // Upload the xml file with PutObject
89 r := bytes.NewReader(b)
90 tctx := context.Background()
91 if checkMinioBucket(mc, fsbucket) == false {
92 err := createMinioBucket(mc, fsbucket)
94 log.Error("Cannot create bucket: ", fsbucket, ", ", err)
99 for i := 1; i < 64 && ok == false; i = i * 2 {
100 info, err := mc.PutObject(tctx, fsbucket, objectName, r, int64(len(b)), minio.PutObjectOptions{ContentType: contentType})
104 log.Warn("Cannot upload (first attempt): ", objectName, ", ", err)
106 log.Warn("Cannot upload (retry): ", objectName, ", ", err)
108 time.Sleep(time.Duration(i) * time.Second)
110 log.Debug("Successfully uploaded: ", objectName, " of size:", info.Size)
116 func createMinioBucket(mc *minio.Client, bucket string) error {
117 tctx := context.Background()
118 err := mc.MakeBucket(tctx, bucket, minio.MakeBucketOptions{})
120 // Check to see if we already own this bucket (which happens if you run this twice)
121 exists, errBucketExists := mc.BucketExists(tctx, bucket)
122 if errBucketExists == nil && exists {
123 log.Debug("Already own bucket:", bucket)
126 log.Error("Cannot create or check bucket ", bucket, " in minio client", err)
130 log.Debug("Successfully created bucket: ", bucket)
135 func checkMinioBucket(mc *minio.Client, bucket string) bool {
136 tctx := context.Background()
137 exists, err := mc.BucketExists(tctx, bucket)
138 if err == nil && exists {
139 log.Debug("Already own bucket:", bucket)
142 log.Error("Bucket does not exist, bucket ", bucket, " in minio client", err)
146 // Write gzipped data to a Writer
148 func gzipWrite(w io.Writer, data *[]byte) error {
149 gw, err1 := gzip.NewWriterLevel(w, gzip.BestSpeed)
155 _, err2 := gw.Write(*data)