eip-cloud-services 1.6.0 → 1.7.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/CHANGELOG.md +1 -0
- package/README.md +2 -0
- package/package.json +2 -1
- package/src/s3.js +35 -0
- package/src/s3MultipartUpload.js +154 -0
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
|
|
|
6
6
|
## Unreleased
|
|
7
7
|
|
|
8
8
|
### Added
|
|
9
|
+
- Added browser-safe S3 multipart upload initiation, part signing, completion, and abort helpers for files above the single-PUT limit.
|
|
9
10
|
- Added staging-safe OnDemand Content Ingest, Content Discovery, and Azure Blob upload clients with injectable fetch, timeouts, production-host guards, and signed-query redaction.
|
|
10
11
|
- Added bounded S3 object streams and resumable Azure block-blob uploads for large server-to-server media transfers without temporary disk.
|
|
11
12
|
- Added callback-scoped loopback S3 range reads for seekable media inspection and server-side multipart copy for objects above 5 GiB.
|
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
The EIP Cloud Services Module is a comprehensive Node.js package that provides seamless integration with various cloud services, including Redis, AWS S3, CDN, AWS Lambda, AWS SQS, and MySQL. This module is designed to simplify the complexities of interacting with these cloud services, offering a range of functionalities from data caching and storage to content delivery and serverless computing.
|
|
6
6
|
|
|
7
|
+
The S3 service supports multipart browser upload orchestration through `createMultipartUpload`, `signMultipartUploadParts`, `completeMultipartUpload`, and `abortMultipartUpload`. Completion lists the uploaded parts from S3 so clients do not need access to response ETags.
|
|
8
|
+
|
|
7
9
|
## Installation and Import
|
|
8
10
|
|
|
9
11
|
To use this module, first install it in your Node.js project. Then, import the required services as follows:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eip-cloud-services",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Houses a collection of helpers for connecting with Cloud services.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"@aws-sdk/client-cloudfront": "^3.354.0",
|
|
19
19
|
"@aws-sdk/client-lambda": "^3.356.0",
|
|
20
20
|
"@aws-sdk/client-s3": "^3.354.0",
|
|
21
|
+
"@aws-sdk/s3-request-presigner": "^3.354.0",
|
|
21
22
|
"@aws-sdk/client-sqs": "^3.356.0",
|
|
22
23
|
"@aws-sdk/client-secrets-manager": "^3.354.0",
|
|
23
24
|
"config": "^3.3.9",
|
package/src/s3.js
CHANGED
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
const { S3Client, HeadObjectCommand, GetObjectCommand, PutObjectCommand, DeleteObjectCommand, CopyObjectCommand, ListObjectsV2Command } = require ( '@aws-sdk/client-s3' );
|
|
42
42
|
const { copyLargeObject } = require ( './s3LargeObject' );
|
|
43
43
|
const { withLocalReadUrl } = require ( './s3LocalReadServer' );
|
|
44
|
+
const multipartUpload = require ( './s3MultipartUpload' );
|
|
44
45
|
const fs = require ( 'fs' );
|
|
45
46
|
const path = require ( 'path' );
|
|
46
47
|
let config = {};
|
|
@@ -238,6 +239,40 @@ exports.withLocalReadUrl = async ( key, bucket = config?.s3?.Bucket, callback )
|
|
|
238
239
|
bucket
|
|
239
240
|
}, callback );
|
|
240
241
|
|
|
242
|
+
exports.createMultipartUpload = async ( key, bucket = config?.s3?.Bucket, options = {} ) =>
|
|
243
|
+
multipartUpload.createMultipartUpload ( {
|
|
244
|
+
client: S3,
|
|
245
|
+
key,
|
|
246
|
+
bucket,
|
|
247
|
+
...options
|
|
248
|
+
} );
|
|
249
|
+
|
|
250
|
+
exports.signMultipartUploadParts = async ( key, uploadId, partNumbers, bucket = config?.s3?.Bucket, options = {} ) =>
|
|
251
|
+
multipartUpload.signMultipartUploadParts ( {
|
|
252
|
+
client: S3,
|
|
253
|
+
key,
|
|
254
|
+
bucket,
|
|
255
|
+
uploadId,
|
|
256
|
+
partNumbers,
|
|
257
|
+
...options
|
|
258
|
+
} );
|
|
259
|
+
|
|
260
|
+
exports.completeMultipartUpload = async ( key, uploadId, bucket = config?.s3?.Bucket ) =>
|
|
261
|
+
multipartUpload.completeMultipartUpload ( {
|
|
262
|
+
client: S3,
|
|
263
|
+
key,
|
|
264
|
+
bucket,
|
|
265
|
+
uploadId
|
|
266
|
+
} );
|
|
267
|
+
|
|
268
|
+
exports.abortMultipartUpload = async ( key, uploadId, bucket = config?.s3?.Bucket ) =>
|
|
269
|
+
multipartUpload.abortMultipartUpload ( {
|
|
270
|
+
client: S3,
|
|
271
|
+
key,
|
|
272
|
+
bucket,
|
|
273
|
+
uploadId
|
|
274
|
+
} );
|
|
275
|
+
|
|
241
276
|
/**
|
|
242
277
|
* Set an object in S3.
|
|
243
278
|
*
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
const {
|
|
2
|
+
AbortMultipartUploadCommand,
|
|
3
|
+
CompleteMultipartUploadCommand,
|
|
4
|
+
CreateMultipartUploadCommand,
|
|
5
|
+
ListPartsCommand,
|
|
6
|
+
UploadPartCommand
|
|
7
|
+
} = require ( '@aws-sdk/client-s3' );
|
|
8
|
+
const { getSignedUrl } = require ( '@aws-sdk/s3-request-presigner' );
|
|
9
|
+
|
|
10
|
+
const MIB = 1024 * 1024;
|
|
11
|
+
const DEFAULT_PART_BYTES = 128 * MIB;
|
|
12
|
+
const MAX_MULTIPART_PARTS = 10000;
|
|
13
|
+
const MAX_PART_BYTES = 5 * 1024 * 1024 * 1024;
|
|
14
|
+
|
|
15
|
+
const resolveUploadPartSize = ( contentLength, requestedPartSize = DEFAULT_PART_BYTES ) => {
|
|
16
|
+
if ( !Number.isSafeInteger ( contentLength ) || contentLength <= 0 ) {
|
|
17
|
+
throw new Error ( 'S3 multipart upload requires a positive, safe content length' );
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const minimumForPartLimit = Math.ceil ( contentLength / MAX_MULTIPART_PARTS );
|
|
21
|
+
const selected = Math.max ( Number ( requestedPartSize ) || DEFAULT_PART_BYTES, minimumForPartLimit );
|
|
22
|
+
const rounded = Math.ceil ( selected / MIB ) * MIB;
|
|
23
|
+
|
|
24
|
+
if ( rounded > MAX_PART_BYTES ) {
|
|
25
|
+
throw new Error ( `S3 object is too large to upload in ${MAX_MULTIPART_PARTS} parts` );
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return rounded;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const createMultipartUpload = async ( {
|
|
32
|
+
client,
|
|
33
|
+
bucket,
|
|
34
|
+
key,
|
|
35
|
+
contentLength,
|
|
36
|
+
contentType,
|
|
37
|
+
acl = 'public-read',
|
|
38
|
+
metadata = {},
|
|
39
|
+
partSize
|
|
40
|
+
} ) => {
|
|
41
|
+
const resolvedPartSize = resolveUploadPartSize ( contentLength, partSize );
|
|
42
|
+
const created = await client.send ( new CreateMultipartUploadCommand ( {
|
|
43
|
+
Bucket: bucket,
|
|
44
|
+
Key: key,
|
|
45
|
+
ContentType: contentType,
|
|
46
|
+
ACL: acl,
|
|
47
|
+
Metadata: metadata
|
|
48
|
+
} ) );
|
|
49
|
+
|
|
50
|
+
if ( !created.UploadId ) {
|
|
51
|
+
throw new Error ( 'S3 did not return an upload id for multipart upload' );
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
uploadId: created.UploadId,
|
|
56
|
+
partSize: resolvedPartSize,
|
|
57
|
+
partCount: Math.ceil ( contentLength / resolvedPartSize )
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const signMultipartUploadParts = async ( {
|
|
62
|
+
client,
|
|
63
|
+
bucket,
|
|
64
|
+
key,
|
|
65
|
+
uploadId,
|
|
66
|
+
partNumbers,
|
|
67
|
+
expiresIn = 1800
|
|
68
|
+
} ) => {
|
|
69
|
+
if ( !Array.isArray ( partNumbers ) || partNumbers.length === 0 ) {
|
|
70
|
+
throw new Error ( 'At least one multipart upload part number is required' );
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return Promise.all ( partNumbers.map ( async partNumber => {
|
|
74
|
+
if ( !Number.isInteger ( partNumber ) || partNumber < 1 || partNumber > MAX_MULTIPART_PARTS ) {
|
|
75
|
+
throw new Error ( `Invalid multipart upload part number: ${partNumber}` );
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const signedRequest = await getSignedUrl ( client, new UploadPartCommand ( {
|
|
79
|
+
Bucket: bucket,
|
|
80
|
+
Key: key,
|
|
81
|
+
UploadId: uploadId,
|
|
82
|
+
PartNumber: partNumber
|
|
83
|
+
} ), { expiresIn } );
|
|
84
|
+
|
|
85
|
+
return { partNumber, signedRequest };
|
|
86
|
+
} ) );
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const listAllParts = async ( { client, bucket, key, uploadId } ) => {
|
|
90
|
+
const parts = [];
|
|
91
|
+
let partNumberMarker;
|
|
92
|
+
|
|
93
|
+
do {
|
|
94
|
+
const page = await client.send ( new ListPartsCommand ( {
|
|
95
|
+
Bucket: bucket,
|
|
96
|
+
Key: key,
|
|
97
|
+
UploadId: uploadId,
|
|
98
|
+
...( partNumberMarker ? { PartNumberMarker: partNumberMarker } : {} )
|
|
99
|
+
} ) );
|
|
100
|
+
|
|
101
|
+
for ( const part of page.Parts || [] ) {
|
|
102
|
+
if ( !part.ETag || !part.PartNumber ) {
|
|
103
|
+
throw new Error ( 'S3 returned an incomplete multipart upload part' );
|
|
104
|
+
}
|
|
105
|
+
parts.push ( { ETag: part.ETag, PartNumber: part.PartNumber } );
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
partNumberMarker = page.IsTruncated ? page.NextPartNumberMarker : undefined;
|
|
109
|
+
} while ( partNumberMarker );
|
|
110
|
+
|
|
111
|
+
if ( parts.length === 0 ) {
|
|
112
|
+
throw new Error ( 'S3 multipart upload contains no completed parts' );
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
parts.sort ( ( left, right ) => left.PartNumber - right.PartNumber );
|
|
116
|
+
parts.forEach ( ( part, index ) => {
|
|
117
|
+
if ( part.PartNumber !== index + 1 ) {
|
|
118
|
+
throw new Error ( `S3 multipart upload is missing part ${index + 1}` );
|
|
119
|
+
}
|
|
120
|
+
} );
|
|
121
|
+
|
|
122
|
+
return parts;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const completeMultipartUpload = async params => {
|
|
126
|
+
const parts = await listAllParts ( params );
|
|
127
|
+
await params.client.send ( new CompleteMultipartUploadCommand ( {
|
|
128
|
+
Bucket: params.bucket,
|
|
129
|
+
Key: params.key,
|
|
130
|
+
UploadId: params.uploadId,
|
|
131
|
+
MultipartUpload: { Parts: parts }
|
|
132
|
+
} ) );
|
|
133
|
+
|
|
134
|
+
return { parts: parts.length };
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const abortMultipartUpload = async ( { client, bucket, key, uploadId } ) => {
|
|
138
|
+
await client.send ( new AbortMultipartUploadCommand ( {
|
|
139
|
+
Bucket: bucket,
|
|
140
|
+
Key: key,
|
|
141
|
+
UploadId: uploadId
|
|
142
|
+
} ) );
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
module.exports = {
|
|
146
|
+
DEFAULT_PART_BYTES,
|
|
147
|
+
MAX_MULTIPART_PARTS,
|
|
148
|
+
abortMultipartUpload,
|
|
149
|
+
completeMultipartUpload,
|
|
150
|
+
createMultipartUpload,
|
|
151
|
+
listAllParts,
|
|
152
|
+
resolveUploadPartSize,
|
|
153
|
+
signMultipartUploadParts
|
|
154
|
+
};
|