@aws-sdk/s3-presigned-post 3.901.0 → 3.906.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-cjs/index.js +73 -115
- package/package.json +2 -2
package/dist-cjs/index.js
CHANGED
|
@@ -1,120 +1,78 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
|
|
21
|
-
// src/index.ts
|
|
22
|
-
var index_exports = {};
|
|
23
|
-
__export(index_exports, {
|
|
24
|
-
createPresignedPost: () => createPresignedPost
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(index_exports);
|
|
1
|
+
'use strict';
|
|
27
2
|
|
|
28
|
-
|
|
29
|
-
var
|
|
30
|
-
var
|
|
31
|
-
var
|
|
32
|
-
var
|
|
33
|
-
var
|
|
34
|
-
var import_util_utf8 = require("@smithy/util-utf8");
|
|
3
|
+
var clientS3 = require('@aws-sdk/client-s3');
|
|
4
|
+
var utilFormatUrl = require('@aws-sdk/util-format-url');
|
|
5
|
+
var middlewareEndpoint = require('@smithy/middleware-endpoint');
|
|
6
|
+
var signatureV4 = require('@smithy/signature-v4');
|
|
7
|
+
var utilHexEncoding = require('@smithy/util-hex-encoding');
|
|
8
|
+
var utilUtf8 = require('@smithy/util-utf8');
|
|
35
9
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
var ALGORITHM_IDENTIFIER = "AWS4-HMAC-SHA256";
|
|
10
|
+
const ALGORITHM_QUERY_PARAM = "X-Amz-Algorithm";
|
|
11
|
+
const CREDENTIAL_QUERY_PARAM = "X-Amz-Credential";
|
|
12
|
+
const AMZ_DATE_QUERY_PARAM = "X-Amz-Date";
|
|
13
|
+
const TOKEN_QUERY_PARAM = "X-Amz-Security-Token";
|
|
14
|
+
const SIGNATURE_QUERY_PARAM = "X-Amz-Signature";
|
|
15
|
+
const ALGORITHM_IDENTIFIER = "AWS4-HMAC-SHA256";
|
|
43
16
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
conditionsSet.add(stringifiedCondition);
|
|
67
|
-
}
|
|
68
|
-
for (const [k, v] of Object.entries(fields)) {
|
|
69
|
-
conditionsSet.add(JSON.stringify({ [k]: v }));
|
|
70
|
-
}
|
|
71
|
-
if (Key.endsWith("${filename}")) {
|
|
72
|
-
conditionsSet.add(JSON.stringify(["starts-with", "$key", Key.substring(0, Key.lastIndexOf("${filename}"))]));
|
|
73
|
-
} else {
|
|
74
|
-
conditionsSet.add(JSON.stringify({ key: Key }));
|
|
75
|
-
}
|
|
76
|
-
const conditions = Array.from(conditionsSet).map((item) => JSON.parse(item));
|
|
77
|
-
const encodedPolicy = base64Encoder(
|
|
78
|
-
utf8Decoder(
|
|
79
|
-
JSON.stringify({
|
|
80
|
-
expiration: iso8601(expiration),
|
|
81
|
-
conditions
|
|
82
|
-
})
|
|
83
|
-
)
|
|
84
|
-
);
|
|
85
|
-
const signingKey = await (0, import_signature_v4.getSigningKey)(sha256, clientCredentials, shortDate, clientRegion, "s3");
|
|
86
|
-
const signature = await hmac(sha256, signingKey, encodedPolicy);
|
|
87
|
-
const endpoint = (0, import_middleware_endpoint.toEndpointV1)(
|
|
88
|
-
await (0, import_middleware_endpoint.getEndpointFromInstructions)(
|
|
89
|
-
{ Bucket, Key },
|
|
90
|
-
import_client_s3.PutObjectCommand,
|
|
91
|
-
{
|
|
92
|
-
...client.config
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
logger: client.config.logger
|
|
96
|
-
}
|
|
97
|
-
)
|
|
98
|
-
);
|
|
99
|
-
return {
|
|
100
|
-
url: (0, import_util_format_url.formatUrl)(endpoint),
|
|
101
|
-
fields: {
|
|
102
|
-
...fields,
|
|
103
|
-
key: Key,
|
|
104
|
-
Policy: encodedPolicy,
|
|
105
|
-
[SIGNATURE_QUERY_PARAM]: (0, import_util_hex_encoding.toHex)(signature)
|
|
17
|
+
const createPresignedPost = async (client, { Bucket, Key, Conditions = [], Fields = {}, Expires = 3600 }) => {
|
|
18
|
+
const { systemClockOffset, base64Encoder, utf8Decoder, sha256 } = client.config;
|
|
19
|
+
const now = new Date(Date.now() + systemClockOffset);
|
|
20
|
+
const signingDate = iso8601(now).replace(/[\-:]/g, "");
|
|
21
|
+
const shortDate = signingDate.slice(0, 8);
|
|
22
|
+
const clientRegion = await client.config.region();
|
|
23
|
+
const credentialScope = signatureV4.createScope(shortDate, clientRegion, "s3");
|
|
24
|
+
const clientCredentials = await client.config.credentials();
|
|
25
|
+
const credential = `${clientCredentials.accessKeyId}/${credentialScope}`;
|
|
26
|
+
const fields = {
|
|
27
|
+
...Fields,
|
|
28
|
+
bucket: Bucket,
|
|
29
|
+
[ALGORITHM_QUERY_PARAM]: ALGORITHM_IDENTIFIER,
|
|
30
|
+
[CREDENTIAL_QUERY_PARAM]: credential,
|
|
31
|
+
[AMZ_DATE_QUERY_PARAM]: signingDate,
|
|
32
|
+
...(clientCredentials.sessionToken ? { [TOKEN_QUERY_PARAM]: clientCredentials.sessionToken } : {}),
|
|
33
|
+
};
|
|
34
|
+
const expiration = new Date(now.valueOf() + Expires * 1000);
|
|
35
|
+
const conditionsSet = new Set();
|
|
36
|
+
for (const condition of Conditions) {
|
|
37
|
+
const stringifiedCondition = JSON.stringify(condition);
|
|
38
|
+
conditionsSet.add(stringifiedCondition);
|
|
106
39
|
}
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
40
|
+
for (const [k, v] of Object.entries(fields)) {
|
|
41
|
+
conditionsSet.add(JSON.stringify({ [k]: v }));
|
|
42
|
+
}
|
|
43
|
+
if (Key.endsWith("${filename}")) {
|
|
44
|
+
conditionsSet.add(JSON.stringify(["starts-with", "$key", Key.substring(0, Key.lastIndexOf("${filename}"))]));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
conditionsSet.add(JSON.stringify({ key: Key }));
|
|
48
|
+
}
|
|
49
|
+
const conditions = Array.from(conditionsSet).map((item) => JSON.parse(item));
|
|
50
|
+
const encodedPolicy = base64Encoder(utf8Decoder(JSON.stringify({
|
|
51
|
+
expiration: iso8601(expiration),
|
|
52
|
+
conditions,
|
|
53
|
+
})));
|
|
54
|
+
const signingKey = await signatureV4.getSigningKey(sha256, clientCredentials, shortDate, clientRegion, "s3");
|
|
55
|
+
const signature = await hmac(sha256, signingKey, encodedPolicy);
|
|
56
|
+
const endpoint = middlewareEndpoint.toEndpointV1(await middlewareEndpoint.getEndpointFromInstructions({ Bucket, Key }, clientS3.PutObjectCommand, {
|
|
57
|
+
...client.config,
|
|
58
|
+
}, {
|
|
59
|
+
logger: client.config.logger,
|
|
60
|
+
}));
|
|
61
|
+
return {
|
|
62
|
+
url: utilFormatUrl.formatUrl(endpoint),
|
|
63
|
+
fields: {
|
|
64
|
+
...fields,
|
|
65
|
+
key: Key,
|
|
66
|
+
Policy: encodedPolicy,
|
|
67
|
+
[SIGNATURE_QUERY_PARAM]: utilHexEncoding.toHex(signature),
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const iso8601 = (date) => date.toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
72
|
+
const hmac = (ctor, secret, data) => {
|
|
73
|
+
const hash = new ctor(secret);
|
|
74
|
+
hash.update(utilUtf8.toUint8Array(data));
|
|
75
|
+
return hash.digest();
|
|
76
|
+
};
|
|
120
77
|
|
|
78
|
+
exports.createPresignedPost = createPresignedPost;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-sdk/s3-presigned-post",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.906.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
|
|
6
6
|
"build:cjs": "node ../../scripts/compilation/inline s3-presigned-post",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"license": "Apache-2.0",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@aws-sdk/client-s3": "3.
|
|
28
|
+
"@aws-sdk/client-s3": "3.906.0",
|
|
29
29
|
"@aws-sdk/types": "3.901.0",
|
|
30
30
|
"@aws-sdk/util-format-url": "3.901.0",
|
|
31
31
|
"@smithy/middleware-endpoint": "^4.3.0",
|