-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
65 lines (51 loc) · 1.27 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
65
package aefire
import (
"cloud.google.com/go/storage"
"context"
"fmt"
"io"
"io/ioutil"
"net/url"
"strings"
)
func (a *AEFire) StorageBucket() *storage.BucketHandle {
bucket, err := a.Storage.Bucket(GAEAppID() + ".appspot.com")
PanicIfError(err)
return bucket
}
func (a *AEFire) StorageObject(storageUrl string) *storage.ObjectHandle {
if strings.HasPrefix(storageUrl, "gs://") {
u, e := url.Parse(storageUrl)
PanicIfError(e)
b, e := a.Storage.Bucket(u.Hostname())
PanicIfError(e)
return b.Object(strings.TrimLeft(u.Path, "/"))
} else {
return a.StorageBucket().Object(strings.TrimLeft(storageUrl, "/"))
}
}
func (a *AEFire) StoreReader(c context.Context, reader io.ReadCloser, filePath string) error {
b, err := ioutil.ReadAll(reader)
defer reader.Close()
if err != nil {
return err
}
return a.StoreBytes(c, b, filePath)
}
func (a *AEFire) StoreBytes(c context.Context, b []byte, filePath string) error {
bucket, err := a.Storage.Bucket(GAEAppID() + ".appspot.com")
if err != nil {
return err
}
obj := bucket.Object(filePath)
writer := obj.NewWriter(c)
defer writer.Close()
_, err = writer.Write(b)
return err
}
func StorageObjectUrl(obj *storage.ObjectHandle) string {
return fmt.Sprintf(
"gs://%s/%s",
obj.BucketName(),
obj.ObjectName())
}