Repository navigation
Expand file tree
/
Copy pathAPIExampleResponse.js
More file actions
77 lines (65 loc) · 2.44 KB
/
Copy pathAPIExampleResponse.js
File metadata and controls
77 lines (65 loc) · 2.44 KB
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
module.exports = function s3PolicyGenerator(projectId) {
return {
s3Signature: s3Signature,
signatureKey: signatureKey,
base64Policy: base64Policy,
accessKeyId: accessKeyId,
folder: folder,
xAmzDate: xAmzDateWSuffix,
xAmzCredential: xAmzCredential,
xAmzAlgorithm: xAmzAlgorithm,
xAmzServerSideEncryption: xAmzServerSideEncryption,
xAmzAcl: xAmzAcl,
xAmzBucket: xAmzBucket,
xAmzMetaUuid: xAmzMetaUuid
};
};
var CryptoJS = require("crypto-js"),
serviceName = 's3',
// Config
accessKeyId = 'YOUR AWS PUBLIC KEY',
folder = 'YOUR S3 FOLDER',
region = 'YOUR S3 REGION ex. us-west-2',
secretAccessKey = 'YOUR SECRET KEY',
xAmzBucket = 'YOUR S3 BUCKET NAME',
// Policy credentials
expiration = '2020-01-01T12:00:00.000Z',
xAmzDate = buildTimestamp(),
xAmzDateWSuffix = xAmzDate + 'T000000Z',
xAmzServerSideEncryption = 'AES256',
xAmzAlgorithm = "AWS4-HMAC-SHA256",
xAmzAcl = "public-read",
expiration = '2020-01-01T12:00:00.000Z',
xAmzMetaUuid = '14365123651274',
xAmzCredential = accessKeyId + "/" + xAmzDate + "/" + region + "/" + serviceName + "/aws4_request",
policy = {
"expiration": expiration,
"conditions": [
{"bucket": xAmzBucket},
["starts-with", "$key", folder + "/"],
{"acl": xAmzAcl},
["starts-with", "$Content-Type", ""],
{"x-amz-server-side-encryption": xAmzServerSideEncryption},
{"x-amz-credential": xAmzCredential},
{"x-amz-algorithm": xAmzAlgorithm},
{"x-amz-meta-uuid": xAmzMetaUuid},
{"x-amz-date": xAmzDateWSuffix }
]
},
base64Policy = new Buffer(JSON.stringify(policy), "utf-8").toString("base64"),
signatureKey = getSignatureKey(secretAccessKey, xAmzDate, region, serviceName),
s3Signature = CryptoJS.HmacSHA256(base64Policy, signatureKey).toString(CryptoJS.enc.Hex);
function getSignatureKey(key, dateStamp, regionName, serviceName) {
var kDate = CryptoJS.HmacSHA256(dateStamp, "AWS4" + key);
var kRegion = CryptoJS.HmacSHA256(regionName, kDate);
var kService = CryptoJS.HmacSHA256(serviceName, kRegion);
var kSigning = CryptoJS.HmacSHA256("aws4_request", kService);
return kSigning;
}
function buildTimestamp() {
var date = new Date();
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
return year + month + day;
}