This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
aws_test.go
83 lines (73 loc) · 1.73 KB
/
aws_test.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
81
82
83
package spotmc
import (
"fmt"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/s3"
"io/ioutil"
"os"
"testing"
"time"
)
func TestPutGet(t *testing.T) {
uti := time.Now().Unix()
ut := fmt.Sprintf("%010d", uti)
bucketName := "spotmc-test-" + ut
// Create test bucket
s3cli := s3Client()
cbConf := s3.CreateBucketConfiguration{
LocationConstraint: aws.String("us-west-2")}
cbReq := s3.CreateBucketInput{
Bucket: aws.String(bucketName),
CreateBucketConfiguration: &cbConf,
}
_, err := s3cli.CreateBucket(&cbReq)
if err != nil {
t.Fatalf("CreateBucket failed(%s): %s", bucketName, err)
}
// Put
testDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
filePath1 := testDir + "/test1.txt"
f, err := os.Create(filePath1)
if err != nil {
t.Fatal(err)
}
f.Write([]byte("abcdefg"))
f.Write([]byte("ABCDEFG"))
f.Close()
err = S3Put("s3://"+bucketName+"/foo/bar.txt", filePath1)
if err != nil {
t.Fatal("S3Put failed", err)
}
// Get
filePath2 := testDir + "/test2.txt"
err = S3Get("s3://"+bucketName+"/foo/bar.txt", filePath2)
if err != nil {
t.Fatal("S3Get failed", err)
}
// Compare
data, err := ioutil.ReadFile(filePath2)
if err != nil {
t.Fatal("ReadFile failed", err)
}
if string(data) != "abcdefgABCDEFG" {
t.Fatalf("Data doesn't match: %s", data)
}
// Delete object from S3
doReq := s3.DeleteObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String("foo/bar.txt"),
}
_, err = s3cli.DeleteObject(&doReq)
if err != nil {
t.Fatal("DeleteObject failed")
}
// Delete test bucket
dbReq := s3.DeleteBucketInput{Bucket: aws.String(bucketName)}
_, err = s3cli.DeleteBucket(&dbReq)
if err != nil {
t.Fatalf("DeleteBucket failed: %s", bucketName)
}
}