forked from googleapis/google-api-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
64 lines (53 loc) · 1.56 KB
/
storage.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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"google.golang.org/api/googleapi"
storage "google.golang.org/api/storage/v1"
)
func init() {
registerDemo("storage", storage.DevstorageReadWriteScope, storageMain)
}
func storageMain(client *http.Client, argv []string) {
if len(argv) != 2 {
fmt.Fprintln(os.Stderr, "Usage: storage filename bucket (to upload an object)")
return
}
service, err := storage.New(client)
if err != nil {
log.Fatalf("Unable to create Storage service: %v", err)
}
filename := argv[0]
bucket := argv[1]
goFile, err := os.Open(filename)
if err != nil {
log.Fatalf("error opening %q: %v", filename, err)
}
storageObject, err := service.Objects.Insert(bucket, &storage.Object{Name: filename}).Media(goFile).Do()
log.Printf("Got storage.Object, err: %#v, %v", storageObject, err)
if err != nil {
return
}
resp, err := service.Objects.Get(bucket, filename).Download()
if err != nil {
log.Fatalf("error downloading %q: %v", filename, err)
}
defer resp.Body.Close()
n, err := io.Copy(ioutil.Discard, resp.Body)
if err != nil {
log.Fatalf("error downloading %q: %v", filename, err)
}
log.Printf("Downloaded %d bytes", n)
// Test If-None-Match - should get a "HTTP 304 Not Modified" response.
obj, err := service.Objects.Get(bucket, filename).IfNoneMatch(storageObject.Etag).Do()
log.Printf("Got obj, err: %#v, %v", obj, err)
if googleapi.IsNotModified(err) {
log.Printf("Success. Object not modified since upload.")
} else {
log.Printf("Error: expected object to not be modified since upload.")
}
}