| 1 |
// Command s3cli is a minimal CLI for testing the s3 package. It supports put, get, rm, and ls. |
| 2 |
// |
| 3 |
// Usage: |
| 4 |
// |
| 5 |
// export S3_URL="s3://ACCESS_KEY:SECRET_KEY@BUCKET/PREFIX?region=REGION&endpoint=ENDPOINT" |
| 6 |
// |
| 7 |
// s3cli put <key> <file> Upload a file |
| 8 |
// s3cli put <key> - Upload from stdin |
| 9 |
// s3cli get <key> Download to stdout |
| 10 |
// s3cli rm <key> [<key>...] Delete one or more objects |
| 11 |
// s3cli ls List all objects |
| 12 |
package main |
| 13 |
|
| 14 |
import ( |
| 15 |
"context" |
| 16 |
"fmt" |
| 17 |
"io" |
| 18 |
"os" |
| 19 |
"text/tabwriter" |
| 20 |
|
| 21 |
"heckel.io/ntfy/v2/s3" |
| 22 |
) |
| 23 |
|
| 24 |
func main() { |
| 25 |
if len(os.Args) < 2 { |
| 26 |
usage() |
| 27 |
} |
| 28 |
s3URL := os.Getenv("S3_URL") |
| 29 |
if s3URL == "" { |
| 30 |
fail("S3_URL environment variable is required") |
| 31 |
} |
| 32 |
cfg, err := s3.ParseURL(s3URL) |
| 33 |
if err != nil { |
| 34 |
fail("invalid S3_URL: %s", err) |
| 35 |
} |
| 36 |
client := s3.New(cfg) |
| 37 |
ctx := context.Background() |
| 38 |
|
| 39 |
switch os.Args[1] { |
| 40 |
case "put": |
| 41 |
cmdPut(ctx, client) |
| 42 |
case "get": |
| 43 |
cmdGet(ctx, client) |
| 44 |
case "rm": |
| 45 |
cmdRm(ctx, client) |
| 46 |
case "ls": |
| 47 |
cmdLs(ctx, client) |
| 48 |
default: |
| 49 |
usage() |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func cmdPut(ctx context.Context, client *s3.Client) { |
| 54 |
if len(os.Args) != 4 { |
| 55 |
fail("usage: s3cli put <key> <file|->\n") |
| 56 |
} |
| 57 |
key := os.Args[2] |
| 58 |
path := os.Args[3] |
| 59 |
|
| 60 |
var r io.Reader |
| 61 |
var size int64 |
| 62 |
if path == "-" { |
| 63 |
r = os.Stdin |
| 64 |
} else { |
| 65 |
f, err := os.Open(path) |
| 66 |
if err != nil { |
| 67 |
fail("open %s: %s", path, err) |
| 68 |
} |
| 69 |
defer f.Close() |
| 70 |
stat, err := f.Stat() |
| 71 |
if err != nil { |
| 72 |
fail("stat %s: %s", path, err) |
| 73 |
} |
| 74 |
r = f |
| 75 |
size = stat.Size() |
| 76 |
} |
| 77 |
|
| 78 |
if err := client.PutObject(ctx, key, r, size); err != nil { |
| 79 |
fail("put: %s", err) |
| 80 |
} |
| 81 |
fmt.Fprintf(os.Stderr, "uploaded %s\n", key) |
| 82 |
} |
| 83 |
|
| 84 |
func cmdGet(ctx context.Context, client *s3.Client) { |
| 85 |
if len(os.Args) != 3 { |
| 86 |
fail("usage: s3cli get <key>\n") |
| 87 |
} |
| 88 |
key := os.Args[2] |
| 89 |
|
| 90 |
reader, size, err := client.GetObject(ctx, key) |
| 91 |
if err != nil { |
| 92 |
fail("get: %s", err) |
| 93 |
} |
| 94 |
defer reader.Close() |
| 95 |
n, err := io.Copy(os.Stdout, reader) |
| 96 |
if err != nil { |
| 97 |
fail("read: %s", err) |
| 98 |
} |
| 99 |
fmt.Fprintf(os.Stderr, "downloaded %s (%d bytes, content-length: %d)\n", key, n, size) |
| 100 |
} |
| 101 |
|
| 102 |
func cmdRm(ctx context.Context, client *s3.Client) { |
| 103 |
if len(os.Args) < 3 { |
| 104 |
fail("usage: s3cli rm <key> [<key>...]\n") |
| 105 |
} |
| 106 |
keys := os.Args[2:] |
| 107 |
if err := client.DeleteObjects(ctx, keys); err != nil { |
| 108 |
fail("rm: %s", err) |
| 109 |
} |
| 110 |
fmt.Fprintf(os.Stderr, "deleted %d object(s)\n", len(keys)) |
| 111 |
} |
| 112 |
|
| 113 |
func cmdLs(ctx context.Context, client *s3.Client) { |
| 114 |
objects, err := client.ListObjectsV2(ctx) |
| 115 |
if err != nil { |
| 116 |
fail("ls: %s", err) |
| 117 |
} |
| 118 |
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 119 |
var totalSize int64 |
| 120 |
for _, obj := range objects { |
| 121 |
fmt.Fprintf(w, "%d\t%s\n", obj.Size, obj.Key) |
| 122 |
totalSize += obj.Size |
| 123 |
} |
| 124 |
w.Flush() |
| 125 |
fmt.Fprintf(os.Stderr, "%d object(s), %d bytes total\n", len(objects), totalSize) |
| 126 |
} |
| 127 |
|
| 128 |
func usage() { |
| 129 |
fmt.Fprintf(os.Stderr, `Usage: s3cli <command> [args...] |
| 130 |
|
| 131 |
Commands: |
| 132 |
put <key> <file|-> Upload a file (use - for stdin) |
| 133 |
get <key> Download to stdout |
| 134 |
rm <key> [keys...] Delete objects |
| 135 |
ls List all objects |
| 136 |
|
| 137 |
Environment: |
| 138 |
S3_URL S3 connection URL (required) |
| 139 |
s3://ACCESS_KEY:SECRET_KEY@BUCKET[/PREFIX]?region=REGION[&endpoint=ENDPOINT] |
| 140 |
`) |
| 141 |
os.Exit(1) |
| 142 |
} |
| 143 |
|
| 144 |
func fail(format string, args ...any) { |
| 145 |
fmt.Fprintf(os.Stderr, format+"\n", args...) |
| 146 |
os.Exit(1) |
| 147 |
} |