-
Notifications
You must be signed in to change notification settings - Fork 2
/
Upload.go
40 lines (33 loc) · 1.15 KB
/
Upload.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
package main
import (
"log"
"os"
"github.com/minio/minio-go"
)
// CleanUp is a function to delete uploaded files
func CleanUp(sourceFilePath string, destinationFilePath string) {
os.Remove(sourceFilePath)
os.Remove(destinationFilePath)
log.Printf("%s file deleted after upload.\n", destinationFilePath)
}
// Upload is a function to upload zip to Spaces
func Upload(fileName string, sourceFilePath string, destinationFilePath string) {
// check if file exist before upload and delete after uploading finished.
if _, err := os.Stat(destinationFilePath); err == nil {
s3Client, err := minio.New(os.Getenv("S3_URL"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("SECRET_ACCESS_KEY"), true)
if err != nil {
log.Fatalln(err)
}
if _, err := s3Client.FPutObject(os.Getenv("BUCKET_NAME"), fileName, destinationFilePath, minio.PutObjectOptions{
ContentType: "application/zip",
}); err != nil {
log.Println(err)
}
log.Printf("%s uploaded successfully.\n", destinationFilePath)
CleanUp(sourceFilePath, destinationFilePath)
} else if os.IsNotExist(err) {
log.Printf("%s file does not exist.\n", destinationFilePath)
} else {
log.Fatalln(err)
}
}