forked from losthismind/aws-apps-scripts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
s3.test.js
98 lines (91 loc) · 3.05 KB
/
s3.test.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Set the following to values that make sense for testing
function testS3Config_() {
return {
"access_key": "",
"secret_key": "",
"bucket_name": "",
"region": "",
"name_of_object_that_exists": "", // do NOT include leading slash
"name_of_object_that_does_not_exist": "", // do NOT include leading slash
"name_of_file_create": "" // do NOT include leading slash
}
}
// Initialize connection:
// S3.init(access_key, secret_key);
function testS3_() {
testS3ListAllBuckets_();
testS3GetObjectExists_();
testS3GetObjectNotExist_();
testS3PutObject_();
testS3DeleteObject_();
}
function testS3ListAllBuckets_() {
var config = testS3Config_();
try {
S3.init(config.access_key, config.secret_key);
var result = S3.listAllMyBuckets();
Logger.log("testS3ListAllBuckets - Result:");
Logger.log(result);
Logger.log("testS3ListAllBuckets - PASS");
} catch(e) {
var message = e.toString();
Logger.log("testS3ListAllBuckets - FAIL - unexpected message [" + message + "]");
Logger.log(e);
}
}
function testS3GetObjectExists_() {
var config = testS3Config_();
try {
S3.init(config.access_key, config.secret_key);
var contents = S3.getObject(config.bucket_name, config.name_of_object_that_exists, config.region);
if (contents == "file") {
Logger.log("testS3GetObjectExists - PASS");
} else {
Logger.log("testS3GetObjectExists - FAIL - contents [" + contents + "] did not match expected result");
}
} catch(e) {
var message = e.toString();
Logger.log("testS3GetObjectExists - FAIL - unexpected message [" + message + "]");
Logger.log(e);
}
}
function testS3GetObjectNotExist_() {
var config = testS3Config_();
try {
S3.init(config.access_key, config.secret_key);
S3.getObject(config.bucket_name, config.name_of_object_that_does_not_exist, config.region);
Logger.log("testS3GetObjectNotExist - FAIL - exception should have been thrown");
} catch(e) {
var message = e.toString();
if (message == "AWS Error - NoSuchKey: The specified key does not exist.") {
Logger.log("testS3GetObjectNotExist - PASS");
} else {
Logger.log("testS3GetObjectNotExist - FAIL - unexpected message [" + message + "]");
Logger.log(e);
}
}
}
function testS3PutObject_() {
var config = testS3Config_();
try {
S3.init(config.access_key, config.secret_key);
S3.putObject(config.bucket_name, config.name_of_file_create, { "data": "contents" }, config.region);
Logger.log("testS3PutObject - PASS");
} catch(e) {
var message = e.toString();
Logger.log("testS3PutObject - FAIL - unexpected message [" + message + "]");
Logger.log(e);
}
}
function testS3DeleteObject_() {
var config = testS3Config_();
try {
S3.init(config.access_key, config.secret_key);
S3.deleteObject(config.bucket_name, config.name_of_file_create, config.region);
Logger.log("testS3DeleteObject - PASS");
} catch(e) {
var message = e.toString();
Logger.log("testS3DeleteObject - FAIL - unexpected message [" + message + "]");
Logger.log(e);
}
}