-
Notifications
You must be signed in to change notification settings - Fork 231
/
sign_url.go
executable file
·80 lines (66 loc) · 1.53 KB
/
sign_url.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package sample
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// SignURLSample signs URL sample
func SignURLSample() {
// Create bucket
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
// Put object
signedURL, err := bucket.SignURL(objectKey, oss.HTTPPut, 60)
if err != nil {
HandleError(err)
}
var val = "花间一壶酒,独酌无相亲。 举杯邀明月,对影成三人。"
err = bucket.PutObjectWithURL(signedURL, strings.NewReader(val))
if err != nil {
HandleError(err)
}
// Put object with option
options := []oss.Option{
oss.Meta("myprop", "mypropval"),
oss.ContentType("image/tiff"),
}
signedURL, err = bucket.SignURL(objectKey, oss.HTTPPut, 60, options...)
if err != nil {
HandleError(err)
}
err = bucket.PutObjectFromFileWithURL(signedURL, localFile, options...)
if err != nil {
HandleError(err)
}
// Get object
signedURL, err = bucket.SignURL(objectKey, oss.HTTPGet, 60)
if err != nil {
HandleError(err)
}
body, err := bucket.GetObjectWithURL(signedURL)
if err != nil {
HandleError(err)
}
defer body.Close()
// Read content
data, err := ioutil.ReadAll(body)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("data:", string(data))
err = bucket.GetObjectToFileWithURL(signedURL, "mynewfile-1.jpg")
if err != nil {
HandleError(err)
}
// Delete the object and bucket
err = DeleteTestBucketAndObject(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("SignURLSample completed")
}