Debian Codegraph @

samples/tutorial/main.go

raw
1 // Copyright 2018 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 // Command upload saves files to blob storage on GCP, AWS, and Azure.
16 package main
17
18 import (
19 "context"
20 "log"
21 "os"
22
23 "gocloud.dev/blob"
24
25 // Import the blob packages we want to be able to open.
26 _ "gocloud.dev/blob/azureblob"
27 _ "gocloud.dev/blob/gcsblob"
28 _ "gocloud.dev/blob/s3blob"
29 )
30
31 func main() {
32 // Define our input.
33 if len(os.Args) != 3 {
34 log.Fatal("usage: upload BUCKET_URL FILE")
35 }
36 bucketURL := os.Args[1]
37 file := os.Args[2]
38
39 ctx := context.Background()
40 // Open a connection to the bucket.
41 b, err := blob.OpenBucket(ctx, bucketURL)
42 if err != nil {
43 log.Fatalf("Failed to setup bucket: %s", err)
44 }
45 defer b.Close()
46
47 // Prepare the file for upload.
48 data, err := os.ReadFile(file)
49 if err != nil {
50 log.Fatalf("Failed to read file: %s", err)
51 }
52
53 w, err := b.NewWriter(ctx, file, nil)
54 if err != nil {
55 log.Fatalf("Failed to obtain writer: %s", err)
56 }
57 _, err = w.Write(data)
58 if err != nil {
59 log.Fatalf("Failed to write to bucket: %s", err)
60 }
61 if err = w.Close(); err != nil {
62 log.Fatalf("Failed to close: %s", err)
63 }
64 }