forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
72 lines (66 loc) · 2 KB
/
s3.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
package s3
import (
"github.com/argoproj/pkg/file"
argos3 "github.com/argoproj/pkg/s3"
log "github.com/sirupsen/logrus"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
)
// S3ArtifactDriver is a driver for AWS S3
type S3ArtifactDriver struct {
Endpoint string
Region string
Secure bool
AccessKey string
SecretKey string
}
// newMinioClient instantiates a new minio client object.
func (s3Driver *S3ArtifactDriver) newS3Client() (argos3.S3Client, error) {
opts := argos3.S3ClientOpts{
Endpoint: s3Driver.Endpoint,
Region: s3Driver.Region,
Secure: s3Driver.Secure,
AccessKey: s3Driver.AccessKey,
SecretKey: s3Driver.SecretKey,
}
return argos3.NewS3Client(opts)
}
// Load downloads artifacts from S3 compliant storage
func (s3Driver *S3ArtifactDriver) Load(inputArtifact *wfv1.Artifact, path string) error {
s3cli, err := s3Driver.newS3Client()
if err != nil {
return err
}
origErr := s3cli.GetFile(inputArtifact.S3.Bucket, inputArtifact.S3.Key, path)
if origErr == nil {
return nil
}
if !argos3.IsS3ErrCode(origErr, "NoSuchKey") {
return origErr
}
// If we get here, the error was a NoSuchKey. The key might be a s3 "directory"
isDir, err := s3cli.IsDirectory(inputArtifact.S3.Bucket, inputArtifact.S3.Key)
if err != nil {
log.Warnf("Failed to test if %s is a directory: %v", inputArtifact.S3.Bucket, err)
return origErr
}
if !isDir {
// It's neither a file, nor a directory. Return the original NoSuchKey error
return origErr
}
return s3cli.GetDirectory(inputArtifact.S3.Bucket, inputArtifact.S3.Key, path)
}
// Save saves an artifact to S3 compliant storage
func (s3Driver *S3ArtifactDriver) Save(path string, outputArtifact *wfv1.Artifact) error {
s3cli, err := s3Driver.newS3Client()
if err != nil {
return err
}
isDir, err := file.IsDirectory(path)
if err != nil {
return err
}
if isDir {
return s3cli.PutDirectory(outputArtifact.S3.Bucket, outputArtifact.S3.Key, path)
}
return s3cli.PutFile(outputArtifact.S3.Bucket, outputArtifact.S3.Key, path)
}