Skip to content

Commit 6f893ac

Browse files
authored
Merge pull request #45 from orangekame3/add-user
[feat]: Add user
2 parents 5dbae4a + e3311ad commit 6f893ac

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ Flags:
263263
-p, --profile string AWS profile to use (default "default")
264264
-r, --region string AWS region to use (overrides the region specified in the profile)
265265
-s, --size Print the size of each file in bytes along with the name.
266-
266+
-u, --username Print the owner of
267267

268268
```
269269

cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ var (
5151
size bool
5252
humanReadable bool
5353
dateTime bool
54+
username bool
5455
)
5556

5657
var rootCmd = &cobra.Command{
@@ -81,7 +82,7 @@ var rootCmd = &cobra.Command{
8182
if level > 0 {
8283
maxDepth = &level
8384
}
84-
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth,size, humanReadable,dateTime)
85+
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth,size, humanReadable,dateTime,username)
8586
if err != nil {
8687
log.Fatalf("failed to fetch S3 object keys: %v", err)
8788
return
@@ -141,6 +142,7 @@ func init() {
141142
rootCmd.Flags().BoolVarP(&size, "size", "s", false, "Print the size of each file in bytes along with the name.")
142143
rootCmd.Flags().BoolVarP(&humanReadable, "human-readable", "H", false, "Print the size of each file but in a more human readable way, e.g. appending a size letter for kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), petabytes (P) and exabytes(E).")
143144
rootCmd.Flags().BoolVarP(&dateTime, "date-time", "D", false, "Print the last modified time of each file.")
145+
rootCmd.Flags().BoolVarP(&username, "username", "u", false, "Print the owner of each file.")
144146
}
145147

146148
func extractBucketAndPrefix(input string) (string, string, error) {

pkg/s3.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ func formatBytes(b int64) string {
8787
}
8888

8989
// FetchS3ObjectKeys returns a slice of keys for all objects in the specified bucket and prefix
90-
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int,size, humanReadable bool,dateTime bool) ([][]string, error) {
90+
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int,size, humanReadable bool,dateTime bool,username bool) ([][]string, error) {
9191
var delimiter *string
92+
var fetchOwner *bool
9293
if maxDepth != nil {
9394
delimiter = aws.String("/")
9495
}
@@ -109,10 +110,15 @@ func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDep
109110
continue
110111
}
111112

113+
if username {
114+
fetchOwner = aws.Bool(true)
115+
}
116+
112117
input := &s3.ListObjectsV2Input{
113118
Bucket: aws.String(bucket),
114119
Prefix: aws.String(currentPrefix),
115120
Delimiter: delimiter,
121+
FetchOwner: fetchOwner,
116122
}
117123

118124
paginator := s3.NewListObjectsV2Paginator(s3Client, input)
@@ -125,8 +131,14 @@ func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDep
125131

126132
for _, obj := range page.Contents {
127133
key := strings.Split(*obj.Key, "/")
134+
// fmt.Println(*obj.Owner.DisplayName)
128135

129136
meta := []string{}
137+
138+
if username {
139+
meta = append(meta, *obj.Owner.DisplayName)
140+
}
141+
130142
if humanReadable {
131143
meta = append(meta, formatBytes(*obj.Size))
132144
} else if size {

0 commit comments

Comments
 (0)