1
+ package main
2
+
3
+ import (
4
+ "github.com/aws/aws-sdk-go/aws"
5
+ "github.com/aws/aws-sdk-go/aws/credentials"
6
+ "github.com/aws/aws-sdk-go/aws/session"
7
+ "github.com/aws/aws-sdk-go/service/s3"
8
+ "github.com/caarlos0/env"
9
+ "fmt"
10
+ "os"
11
+ "time"
12
+ "strings"
13
+ )
14
+
15
+ type config struct {
16
+ Bucket string `env:"BUCKET"`
17
+ Prefix string `env:"PATH_PREFIX"`
18
+ Timezone string `env:"TIMEZONE"`
19
+ OlderThanMinutes int `env:"OLDER_THAN_MINUTES"`
20
+ SmallerThanMegabytes int64 `env:"SMALLER_THAN_MEGABYTES"`
21
+ AwsAccessKeyID string `env:"AWS_ACCESS_KEY_ID"`
22
+ AwsSecretAccessKey string `env:"AWS_SECRET_ACCESS_KEY"`
23
+ AwsRegion string `env:"AWS_REGION"`
24
+ }
25
+
26
+ func main () {
27
+
28
+ cfg := config {}
29
+ if err := env .Parse (& cfg ); err != nil {
30
+ exitErrorf ("%+v\n " , err )
31
+ }
32
+
33
+ fmt .Println ("Check last file in:" )
34
+ fmt .Println ("Region: " , cfg .AwsRegion )
35
+ fmt .Println ("Bucket: " , cfg .Bucket )
36
+ fmt .Println ("Prefix: " , cfg .Prefix )
37
+ fmt .Println ("Older than: " , cfg .OlderThanMinutes , "minutes" )
38
+ fmt .Println ("Smaller than: " , cfg .SmallerThanMegabytes , "MB" )
39
+
40
+ // utc life
41
+ loc , err := time .LoadLocation (cfg .Timezone )
42
+ if err != nil {
43
+ panic (err )
44
+ }
45
+
46
+ sess , err := session .NewSession (& aws.Config {
47
+ Region : aws .String (cfg .AwsRegion ),
48
+ Credentials : credentials .NewStaticCredentials (cfg .AwsAccessKeyID , cfg .AwsSecretAccessKey , "" ),
49
+ })
50
+
51
+ // Create S3 service client
52
+ svc := s3 .New (sess )
53
+
54
+ // Get the list of items
55
+ resp , err := svc .ListObjectsV2 (& s3.ListObjectsV2Input {Bucket : aws .String (cfg .Bucket ), Prefix : aws .String (cfg .Prefix )})
56
+ if err != nil {
57
+ exitErrorf ("[ERROR] Unable to list items in bucket %q, %v" , cfg .Bucket , err )
58
+ }
59
+
60
+ if (len (resp .Contents ) == 0 ) {
61
+ fmt .Println ("[ERROR] No files in bucket. " )
62
+ os .Exit (1 )
63
+ }
64
+
65
+ mostRecentObj := * resp .Contents [0 ]
66
+ for _ , item := range resp .Contents {
67
+ if (item .LastModified .After (* mostRecentObj .LastModified )) {
68
+ mostRecentObj = * item
69
+ }
70
+ }
71
+
72
+ fmt .Println ("Files in bucket:" , len (resp .Contents ))
73
+ fmt .Println ("" )
74
+
75
+ keyArray := strings .Split (* mostRecentObj .Key , "/" )
76
+ mostRecentObjName := keyArray [len (keyArray )- 1 ]
77
+ mostRecentObjDate := (* mostRecentObj .LastModified ).In (loc )
78
+ mostRecentObjSize := * mostRecentObj .Size / 1024 / 1024
79
+
80
+ fmt .Println ("Most recent file is: " )
81
+ fmt .Println ("Name: " , mostRecentObjName )
82
+ fmt .Println ("modified at: " , mostRecentObjDate )
83
+ fmt .Println ("Size: " , mostRecentObjSize , "MB" )
84
+ fmt .Println ("" )
85
+
86
+ minOlderTime := time .Now ().In (loc ).Add (time .Minute * time .Duration (- 1 * cfg .OlderThanMinutes ))
87
+ diffTime := mostRecentObjDate .Sub (minOlderTime );
88
+
89
+ error := false
90
+ if (diffTime .Minutes () < 0 ) {
91
+ fmt .Println ("[ERROR] The file is older than max allowed. (" , diffTime * - 1 , "ago )" )
92
+ error = true
93
+ }
94
+
95
+ if (cfg .SmallerThanMegabytes > 0 && * mostRecentObj .Size / 1024 / 1024 < cfg .SmallerThanMegabytes ) {
96
+ fmt .Println ("[ERROR] The file is smaller than min allowed. (" , mostRecentObjSize , "MB vs" , cfg .SmallerThanMegabytes , "MB )" )
97
+ error = true
98
+ }
99
+
100
+ if (error ) {
101
+ os .Exit (1 )
102
+ }
103
+
104
+ fmt .Println ("[SUCCESS] The file is OK." )
105
+ }
106
+
107
+ func exitErrorf (msg string , args ... interface {}) {
108
+ fmt .Fprintf (os .Stderr , msg + "\n " , args ... )
109
+ os .Exit (1 )
110
+ }
0 commit comments