-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.go
99 lines (88 loc) · 2.16 KB
/
info.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/urfave/cli"
"path"
)
func Info(config *Config, c *cli.Context) error {
args := c.Args()
s3c := GetS3Client(config)
if len(args) != 1 {
return fmt.Errorf("arguments must be 1 bucket path")
}
s3path, err := NewS3Path(args[0])
if err != nil {
fmt.Println(err)
return err
}
if len(s3path.Path) == 0 {
return infoBucket(s3c, s3path)
}
return infoObject(s3c, s3path)
}
func infoObject(s3c *s3.S3, s3path *S3Path) error {
//head object
hoi := &s3.HeadObjectInput{
Bucket: aws.String(s3path.Bucket),
Key: aws.String(s3path.Path),
}
hoir, err := s3c.HeadObject(hoi)
if err != nil {
fmt.Println(err)
return err
}
// bucket policy
gbpi := &s3.GetBucketPolicyInput{
Bucket: aws.String(s3path.Bucket),
}
gbpr, err := s3c.GetBucketPolicy(gbpi)
if err != nil {
fmt.Println(err)
return err
}
fmt.Printf("%s://%s (object): \n", s3path.Scheme, path.Join(s3path.Bucket, s3path.Path))
fmt.Printf(" File size: %d\n", *hoir.ContentLength)
fmt.Printf(" Last mod: %s\n", *hoir.LastModified)
fmt.Printf(" MIME type: %s\n", *hoir.ContentType)
fmt.Printf(" MD5 sum: %s\n", *hoir.ETag)
fmt.Printf(" Policy: %s\n", *gbpr.Policy)
return nil
}
func infoBucket(s3c *s3.S3, s3path *S3Path) error {
//bucket policy
gbpi := &s3.GetBucketPolicyInput{
Bucket: aws.String(s3path.Bucket),
}
gbpr, err := s3c.GetBucketPolicy(gbpi)
if err != nil {
fmt.Println(err)
return err
}
//bucket location
gbli := &s3.GetBucketLocationInput{
Bucket: aws.String(s3path.Bucket),
}
gblr, err := s3c.GetBucketLocation(gbli)
if err != nil {
fmt.Println(err)
return err
}
//bucket acl
gbai := &s3.GetBucketAclInput{
Bucket: aws.String(s3path.Bucket),
}
gbar, err := s3c.GetBucketAcl(gbai)
if err != nil {
fmt.Println(err)
return err
}
fmt.Printf("%s://%s (bucket): \n", s3path.Scheme, s3path.Bucket)
fmt.Printf(" Location: %s\n", *gblr.LocationConstraint)
fmt.Printf(" Policy: %s\n", *gbpr.Policy)
if len(gbar.Grants) > 0 {
fmt.Printf(" ACL: %9s:%s\n", *gbar.Grants[0].Grantee.ID, *gbar.Grants[0].Permission)
}
return nil
}