| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// gocdk-blob demonstrates the use of the Go CDK blob package in a |
| 16 |
// simple command-line application. |
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"log" |
| 25 |
"os" |
| 26 |
|
| 27 |
"github.com/google/subcommands" |
| 28 |
"gocloud.dev/blob" |
| 29 |
|
| 30 |
// Import the blob driver packages we want to be able to open. |
| 31 |
_ "gocloud.dev/blob/azureblob" |
| 32 |
_ "gocloud.dev/blob/fileblob" |
| 33 |
_ "gocloud.dev/blob/gcsblob" |
| 34 |
_ "gocloud.dev/blob/s3blob" |
| 35 |
) |
| 36 |
|
| 37 |
const helpSuffix = ` |
| 38 |
|
| 39 |
See https://gocloud.dev/concepts/urls/ for more background on |
| 40 |
Go CDK URLs, and sub-packages under gocloud.dev/blob |
| 41 |
(https://godoc.org/gocloud.dev/blob#pkg-subdirectories) |
| 42 |
for details on the blob.Bucket URL format. |
| 43 |
` |
| 44 |
|
| 45 |
func main() { |
| 46 |
os.Exit(run()) |
| 47 |
} |
| 48 |
|
| 49 |
func run() int { |
| 50 |
subcommands.Register(subcommands.HelpCommand(), "") |
| 51 |
subcommands.Register(&downloadCmd{}, "") |
| 52 |
subcommands.Register(&listCmd{}, "") |
| 53 |
subcommands.Register(&uploadCmd{}, "") |
| 54 |
log.SetFlags(0) |
| 55 |
log.SetPrefix("gocdk-blob: ") |
| 56 |
flag.Parse() |
| 57 |
return int(subcommands.Execute(context.Background())) |
| 58 |
} |
| 59 |
|
| 60 |
type downloadCmd struct{} |
| 61 |
|
| 62 |
func (*downloadCmd) Name() string { return "download" } |
| 63 |
func (*downloadCmd) Synopsis() string { return "Output a blob to stdout" } |
| 64 |
func (*downloadCmd) Usage() string { |
| 65 |
return `download <bucket URL> <key> |
| 66 |
|
| 67 |
Read the blob <key> from <bucket URL> and write it to stdout. |
| 68 |
|
| 69 |
Example: |
| 70 |
gocdk-blob download gs://mybucket my/gcs/file > foo.txt` + helpSuffix |
| 71 |
} |
| 72 |
|
| 73 |
func (*downloadCmd) SetFlags(_ *flag.FlagSet) {} |
| 74 |
|
| 75 |
func (*downloadCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 76 |
if f.NArg() != 2 { |
| 77 |
f.Usage() |
| 78 |
return subcommands.ExitUsageError |
| 79 |
} |
| 80 |
bucketURL := f.Arg(0) |
| 81 |
blobKey := f.Arg(1) |
| 82 |
|
| 83 |
// Open a *blob.Bucket using the bucketURL. |
| 84 |
bucket, err := blob.OpenBucket(ctx, bucketURL) |
| 85 |
if err != nil { |
| 86 |
log.Printf("Failed to open bucket: %v\n", err) |
| 87 |
return subcommands.ExitFailure |
| 88 |
} |
| 89 |
defer bucket.Close() |
| 90 |
|
| 91 |
// Open a *blob.Reader for the blob at blobKey. |
| 92 |
reader, err := bucket.NewReader(ctx, blobKey, nil) |
| 93 |
if err != nil { |
| 94 |
log.Printf("Failed to read %q: %v\n", blobKey, err) |
| 95 |
return subcommands.ExitFailure |
| 96 |
} |
| 97 |
defer reader.Close() |
| 98 |
|
| 99 |
// Copy the data. |
| 100 |
_, err = io.Copy(os.Stdout, reader) |
| 101 |
if err != nil { |
| 102 |
log.Printf("Failed to copy data: %v\n", err) |
| 103 |
return subcommands.ExitFailure |
| 104 |
} |
| 105 |
return subcommands.ExitSuccess |
| 106 |
} |
| 107 |
|
| 108 |
type listCmd struct { |
| 109 |
prefix string |
| 110 |
delimiter string |
| 111 |
} |
| 112 |
|
| 113 |
func (*listCmd) Name() string { return "ls" } |
| 114 |
func (*listCmd) Synopsis() string { return "List blobs in a bucket" } |
| 115 |
func (*listCmd) Usage() string { |
| 116 |
return `ls [-p <prefix>] [d <delimiter>] <bucket URL> |
| 117 |
|
| 118 |
List the blobs in <bucket URL>. |
| 119 |
|
| 120 |
Example: |
| 121 |
gocdk-blob ls -p "subdir/" gs://mybucket` + helpSuffix |
| 122 |
} |
| 123 |
|
| 124 |
func (cmd *listCmd) SetFlags(f *flag.FlagSet) { |
| 125 |
f.StringVar(&cmd.prefix, "p", "", "prefix to match") |
| 126 |
f.StringVar(&cmd.delimiter, "d", "/", "directory delimiter; empty string returns flattened listing") |
| 127 |
} |
| 128 |
|
| 129 |
func (cmd *listCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 130 |
if f.NArg() != 1 { |
| 131 |
f.Usage() |
| 132 |
return subcommands.ExitUsageError |
| 133 |
} |
| 134 |
bucketURL := f.Arg(0) |
| 135 |
|
| 136 |
// Open a *blob.Bucket using the bucketURL. |
| 137 |
bucket, err := blob.OpenBucket(ctx, bucketURL) |
| 138 |
if err != nil { |
| 139 |
log.Printf("Failed to open bucket: %v\n", err) |
| 140 |
return subcommands.ExitFailure |
| 141 |
} |
| 142 |
defer bucket.Close() |
| 143 |
|
| 144 |
opts := blob.ListOptions{ |
| 145 |
Prefix: cmd.prefix, |
| 146 |
Delimiter: cmd.delimiter, |
| 147 |
} |
| 148 |
iter := bucket.List(&opts) |
| 149 |
for { |
| 150 |
obj, err := iter.Next(ctx) |
| 151 |
if err == io.EOF { |
| 152 |
break |
| 153 |
} |
| 154 |
if err != nil { |
| 155 |
log.Printf("Failed to list: %v", err) |
| 156 |
return subcommands.ExitFailure |
| 157 |
} |
| 158 |
fmt.Println(obj.Key) |
| 159 |
} |
| 160 |
return subcommands.ExitSuccess |
| 161 |
} |
| 162 |
|
| 163 |
type uploadCmd struct{} |
| 164 |
|
| 165 |
func (*uploadCmd) Name() string { return "upload" } |
| 166 |
func (*uploadCmd) Synopsis() string { return "Upload a blob from stdin" } |
| 167 |
func (*uploadCmd) Usage() string { |
| 168 |
return `upload <bucket URL> <key> |
| 169 |
|
| 170 |
Read from stdin and write to the blob <key> in <bucket URL>. |
| 171 |
|
| 172 |
Example: |
| 173 |
cat foo.txt | gocdk-blob upload gs://mybucket my/gcs/file` + helpSuffix |
| 174 |
} |
| 175 |
|
| 176 |
func (*uploadCmd) SetFlags(_ *flag.FlagSet) {} |
| 177 |
|
| 178 |
func (*uploadCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) (status subcommands.ExitStatus) { |
| 179 |
if f.NArg() != 2 { |
| 180 |
f.Usage() |
| 181 |
return subcommands.ExitUsageError |
| 182 |
} |
| 183 |
bucketURL := f.Arg(0) |
| 184 |
blobKey := f.Arg(1) |
| 185 |
|
| 186 |
// Open a *blob.Bucket using the bucketURL. |
| 187 |
bucket, err := blob.OpenBucket(ctx, bucketURL) |
| 188 |
if err != nil { |
| 189 |
log.Printf("Failed to open bucket: %v\n", err) |
| 190 |
return subcommands.ExitFailure |
| 191 |
} |
| 192 |
defer bucket.Close() |
| 193 |
|
| 194 |
// Open a *blob.Writer for the blob at blobKey. |
| 195 |
writer, err := bucket.NewWriter(ctx, blobKey, nil) |
| 196 |
if err != nil { |
| 197 |
log.Printf("Failed to write %q: %v\n", blobKey, err) |
| 198 |
return subcommands.ExitFailure |
| 199 |
} |
| 200 |
defer func() { |
| 201 |
if err := writer.Close(); err != nil && status == subcommands.ExitSuccess { |
| 202 |
log.Printf("closing the writer: %v", err) |
| 203 |
status = subcommands.ExitFailure |
| 204 |
} |
| 205 |
}() |
| 206 |
|
| 207 |
// Copy the data. |
| 208 |
_, err = io.Copy(writer, os.Stdin) |
| 209 |
if err != nil { |
| 210 |
log.Printf("Failed to copy data: %v\n", err) |
| 211 |
return subcommands.ExitFailure |
| 212 |
} |
| 213 |
return subcommands.ExitSuccess |
| 214 |
} |