-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_funcs.go
126 lines (105 loc) · 3.03 KB
/
aws_funcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package utils
import (
"context"
"io"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
// Download the resource file from the given s3 bucket
func DownloadFile(bucketName string, objPath string, region string) ([]byte, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
if err != nil {
return nil, err
}
client := s3.NewFromConfig(cfg)
output, err := client.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(objPath),
})
if err != nil {
return nil, err
}
defer output.Body.Close()
body, err := io.ReadAll(output.Body)
if err != nil {
return nil, err
}
return body, nil
}
func DownloadAllObjKeys(bucketName string, prefix string, region string) ([]string, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
if err != nil {
return nil, err
}
client := s3.NewFromConfig(cfg)
output, err := client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(bucketName),
Prefix: aws.String(prefix),
})
if err != nil {
return nil, err
}
var objKeyArr []string
for _, item := range output.Contents {
objKeyArr = append(objKeyArr, *item.Key)
}
return objKeyArr, nil
}
func DownloadAndWriteNoteData() error {
objKeyArr, err := DownloadAllObjKeys(BUCKET_NAME, NOTES_DATA_FOLDER, BUCKET_REGION)
if err != nil {
return &ErrorMessage{
Code: 500,
Message: "Failed to Fetch Objects Keys. " + err.Error(),
}
}
for _, objKey := range objKeyArr {
file, err := DownloadFile(BUCKET_NAME, objKey, BUCKET_REGION)
if err != nil {
return &ErrorMessage{
Code: 500,
Message: "Error downloading the objects. " + err.Error(),
}
}
localStoragePath := strings.ReplaceAll(objKey, "public/notes/", "")
path_split := strings.Split(localStoragePath, "/")
localStoragePath = "./data/notes/" + localStoragePath
// path_split[path to file.txt]
// [path to]
// Join them, path/to
createDirPath := "./data/notes/" + strings.Join(path_split[:len(path_split)-1], "/")
// Create the required path
err = os.MkdirAll(createDirPath, os.ModePerm)
if err != nil {
panic(err)
}
// So if the path is not a file, it throws an error
// S3 returns dirs and files
// dirs end with '/', thus "./data/notes/"
// if end not / then create and write to the file
// OR
// relPath := c.Param("*")
// basePath := "./data/notes"
// absPath := filepath.Join(basePath, relPath)
// // Check if file exists
// _, err := os.Stat(absPath)
// if err != nil {
// return c.JSON(echo.ErrNotFound.Code, &utils.ErrorMessage{
// Code: echo.ErrNotFound.Code,
// Message: "Requested file doesnt exist",
// })
// }
if string(localStoragePath[len(localStoragePath)-1]) != "/" {
if err := os.WriteFile(localStoragePath, file, 0644); err != nil {
return &ErrorMessage{
Code: 500,
Message: "Error writing data to file. " + err.Error(),
}
}
}
}
return nil
}