diff --git a/instances.go b/instances.go index f718f23..8d3ae9a 100644 --- a/instances.go +++ b/instances.go @@ -653,24 +653,55 @@ func (minioInstance *MinIO) uploadFile(reader io.Reader, tags map[string]string, return nil, err } - result := map[string]string{"location": object.Bucket + "/" + fileName, "size": strconv.Itoa(int(object.Size))} + result := map[string]string{"location": targetSite + "/" + object.Bucket + "/" + fileName, "size": strconv.Itoa(int(object.Size))} return result, nil } +func (minioInstance *MinIO) getDirectObject(datasetPath string) (*minio.Object, error) { + healthyInstances, err := minioInstance.Healths() + + if err != nil { + return &minio.Object{}, err + } + + targetSite := strings.Split(datasetPath, "/")[0] + "//" + strings.Split(datasetPath, "/")[2] + fmt.Println(targetSite) + find := false + for k, _ := range healthyInstances { + if k == targetSite { + find = true + } + } + + if !find { + return nil, errors.New("could not find the target minio instance inside the healthy ones") + } + + bucketName := strings.Split(datasetPath, "/")[3] + objectPath := strings.Join(strings.Split(datasetPath, "/")[4:], "/") + + reader, err := minioInstance.clients[targetSite].GetObject( + context.Background(), + bucketName, + objectPath, + minio.GetObjectOptions{}, + ) + + return reader, nil +} + func (minioInstance *MinIO) getObject(url string, datasetPath string, forever bool) (string, error) { path := fmt.Sprintf("%s/%s", minioInstance.aliases[url], datasetPath) var expirationTime string if forever { - expirationTime = "366d" + expirationTime = "7d" } else { expirationTime = "10m" } - fmt.Println(expirationTime) - cmdArgs := []string{"./mc", "share", "download", "--expire", expirationTime, "--json", path} cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) diff --git a/main.go b/main.go index cb29dd0..8f89034 100644 --- a/main.go +++ b/main.go @@ -323,6 +323,33 @@ func main() { } }) + r.GET("/get/object", func(c *gin.Context) { + + datasetPath, exists := c.GetQuery("path") + + if !exists { + c.JSON(400, gin.H{ + "message": "dataset_path parameter is required!", + }) + } else { + data, err := minio.getDirectObject(datasetPath) + + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + defer data.Close() + + c.Header("Content-Disposition", "attachment; filename=downloaded_file.csv") + + _, err = io.Copy(c.Writer, data) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + } + }) + r.GET("/list_location", func(c *gin.Context) { path, exists := c.GetQuery("path") @@ -509,9 +536,7 @@ func main() { "message": "Something happened when trying to upload the file!", }) } else { - c.JSON(201, gin.H{ - "message": result, - }) + c.JSON(201, result) } })