eip-cloud-services 1.5.1 → 1.6.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 +29 -0
- package/package.json +1 -1
- package/src/s3.js +44 -0
- package/src/s3LargeObject.js +142 -0
- package/src/s3LocalReadServer.js +111 -0
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
|
|
|
8
8
|
### Added
|
|
9
9
|
- 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
10
|
- Added bounded S3 object streams and resumable Azure block-blob uploads for large server-to-server media transfers without temporary disk.
|
|
11
|
+
- Added callback-scoped loopback S3 range reads for seekable media inspection and server-side multipart copy for objects above 5 GiB.
|
|
11
12
|
|
|
12
13
|
## [1.2.5] - 2026-03-02
|
|
13
14
|
|
package/README.md
CHANGED
|
@@ -429,6 +429,35 @@ await s3.del('myObjectKey');
|
|
|
429
429
|
await s3.copy('sourceObjectKey', 'destinationObjectKey');
|
|
430
430
|
```
|
|
431
431
|
|
|
432
|
+
For objects that may exceed S3's 5 GiB atomic-copy limit, use the large-object
|
|
433
|
+
copy helper. It automatically switches to multipart server-side copy and does
|
|
434
|
+
not download the object through the caller:
|
|
435
|
+
|
|
436
|
+
```javascript
|
|
437
|
+
await s3.copyLargeObject(
|
|
438
|
+
'temporary/video.mp4',
|
|
439
|
+
'permanent/video.mp4',
|
|
440
|
+
'source-bucket',
|
|
441
|
+
'destination-bucket',
|
|
442
|
+
{ contentType: 'video/mp4' }
|
|
443
|
+
);
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
### Seekable Local Access to a Private Large Object
|
|
447
|
+
|
|
448
|
+
Media tools such as FFmpeg often require byte-range seeking. `withLocalReadUrl`
|
|
449
|
+
provides a callback-scoped loopback URL and translates HTTP range requests into
|
|
450
|
+
authenticated S3 reads. The source is never written to local disk:
|
|
451
|
+
|
|
452
|
+
```javascript
|
|
453
|
+
const metadata = await s3.withLocalReadUrl('temporary/video.mp4', 'source-bucket', async url => {
|
|
454
|
+
return inspectVideo(url);
|
|
455
|
+
});
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
The URL is bound to `127.0.0.1`, contains an unguessable path, and is closed as
|
|
459
|
+
soon as the callback settles.
|
|
460
|
+
|
|
432
461
|
### Move an Object
|
|
433
462
|
|
|
434
463
|
```javascript
|
package/package.json
CHANGED
package/src/s3.js
CHANGED
|
@@ -39,6 +39,8 @@
|
|
|
39
39
|
*/
|
|
40
40
|
|
|
41
41
|
const { S3Client, HeadObjectCommand, GetObjectCommand, PutObjectCommand, DeleteObjectCommand, CopyObjectCommand, ListObjectsV2Command } = require ( '@aws-sdk/client-s3' );
|
|
42
|
+
const { copyLargeObject } = require ( './s3LargeObject' );
|
|
43
|
+
const { withLocalReadUrl } = require ( './s3LocalReadServer' );
|
|
42
44
|
const fs = require ( 'fs' );
|
|
43
45
|
const path = require ( 'path' );
|
|
44
46
|
let config = {};
|
|
@@ -220,6 +222,22 @@ exports.getStream = async ( key, bucket = config?.s3?.Bucket ) => {
|
|
|
220
222
|
};
|
|
221
223
|
};
|
|
222
224
|
|
|
225
|
+
/**
|
|
226
|
+
* Expose a private S3 object through a short-lived loopback HTTP URL. Range
|
|
227
|
+
* requests are served from S3 so local media tools can seek without writing
|
|
228
|
+
* the full object to disk or requiring public S3 network access.
|
|
229
|
+
*
|
|
230
|
+
* @param {string} key - The source object key.
|
|
231
|
+
* @param {string} [bucket=config?.s3?.Bucket] - The source bucket.
|
|
232
|
+
* @param {(url:string)=>Promise<*>} callback - Work performed while the URL is available.
|
|
233
|
+
* @returns {Promise<*>} The callback result.
|
|
234
|
+
*/
|
|
235
|
+
exports.withLocalReadUrl = async ( key, bucket = config?.s3?.Bucket, callback ) => withLocalReadUrl ( {
|
|
236
|
+
client: S3,
|
|
237
|
+
key,
|
|
238
|
+
bucket
|
|
239
|
+
}, callback );
|
|
240
|
+
|
|
223
241
|
/**
|
|
224
242
|
* Set an object in S3.
|
|
225
243
|
*
|
|
@@ -361,6 +379,32 @@ exports.copy = async ( sourceKey, destinationKey, sourceBucket = config?.s3?.Buc
|
|
|
361
379
|
}
|
|
362
380
|
};
|
|
363
381
|
|
|
382
|
+
/**
|
|
383
|
+
* Copy an S3 object without downloading it. Objects above S3's 5 GiB atomic
|
|
384
|
+
* copy limit are copied with multipart upload-part-copy requests.
|
|
385
|
+
*
|
|
386
|
+
* @param {string} sourceKey - The source object key.
|
|
387
|
+
* @param {string} destinationKey - The destination object key.
|
|
388
|
+
* @param {string} [sourceBucket=config?.s3?.Bucket] - The source bucket.
|
|
389
|
+
* @param {string} [destinationBucket=config?.s3?.Bucket] - The destination bucket.
|
|
390
|
+
* @param {object} [options] - Destination and multipart options.
|
|
391
|
+
* @returns {Promise<{contentLength:number,multipart:boolean,parts:number}>} Copy summary.
|
|
392
|
+
*/
|
|
393
|
+
exports.copyLargeObject = async (
|
|
394
|
+
sourceKey,
|
|
395
|
+
destinationKey,
|
|
396
|
+
sourceBucket = config?.s3?.Bucket,
|
|
397
|
+
destinationBucket = config?.s3?.Bucket,
|
|
398
|
+
options = {}
|
|
399
|
+
) => copyLargeObject ( {
|
|
400
|
+
client: S3,
|
|
401
|
+
sourceKey,
|
|
402
|
+
destinationKey,
|
|
403
|
+
sourceBucket,
|
|
404
|
+
destinationBucket,
|
|
405
|
+
...options
|
|
406
|
+
} );
|
|
407
|
+
|
|
364
408
|
/**
|
|
365
409
|
* Move an object within S3 to a different location. (This deletes the original like a CUT / PASTE operation)
|
|
366
410
|
*
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const {
|
|
2
|
+
AbortMultipartUploadCommand,
|
|
3
|
+
CompleteMultipartUploadCommand,
|
|
4
|
+
CopyObjectCommand,
|
|
5
|
+
CreateMultipartUploadCommand,
|
|
6
|
+
HeadObjectCommand,
|
|
7
|
+
UploadPartCopyCommand
|
|
8
|
+
} = require ( '@aws-sdk/client-s3' );
|
|
9
|
+
|
|
10
|
+
const GIB = 1024 * 1024 * 1024;
|
|
11
|
+
const MIB = 1024 * 1024;
|
|
12
|
+
const SINGLE_COPY_LIMIT_BYTES = 5 * GIB;
|
|
13
|
+
const MAX_COPY_PART_BYTES = 5 * GIB;
|
|
14
|
+
const DEFAULT_COPY_PART_BYTES = 512 * MIB;
|
|
15
|
+
const MAX_MULTIPART_PARTS = 10000;
|
|
16
|
+
|
|
17
|
+
const encodeCopySource = ( bucket, key ) => encodeURIComponent ( `${bucket}/${key}` ).replace ( /%2F/g, '/' );
|
|
18
|
+
|
|
19
|
+
const resolvePartSize = ( contentLength, requestedPartSize = DEFAULT_COPY_PART_BYTES ) => {
|
|
20
|
+
const minimumForPartLimit = Math.ceil ( contentLength / MAX_MULTIPART_PARTS );
|
|
21
|
+
const selected = Math.max ( Number ( requestedPartSize ) || DEFAULT_COPY_PART_BYTES, minimumForPartLimit );
|
|
22
|
+
const rounded = Math.ceil ( selected / MIB ) * MIB;
|
|
23
|
+
|
|
24
|
+
if ( rounded > MAX_COPY_PART_BYTES ) {
|
|
25
|
+
throw new Error ( `S3 object is too large to copy in ${MAX_MULTIPART_PARTS} parts` );
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return rounded;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const buildCopyRanges = ( contentLength, requestedPartSize ) => {
|
|
32
|
+
if ( !Number.isSafeInteger ( contentLength ) || contentLength <= 0 ) {
|
|
33
|
+
throw new Error ( 'S3 copy source must have a positive, safe content length' );
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const partSize = resolvePartSize ( contentLength, requestedPartSize );
|
|
37
|
+
const ranges = [];
|
|
38
|
+
|
|
39
|
+
for ( let start = 0, partNumber = 1; start < contentLength; start += partSize, partNumber += 1 ) {
|
|
40
|
+
ranges.push ( {
|
|
41
|
+
partNumber,
|
|
42
|
+
start,
|
|
43
|
+
end: Math.min ( start + partSize, contentLength ) - 1
|
|
44
|
+
} );
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return ranges;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const copyLargeObject = async ( {
|
|
51
|
+
client,
|
|
52
|
+
sourceKey,
|
|
53
|
+
destinationKey,
|
|
54
|
+
sourceBucket,
|
|
55
|
+
destinationBucket,
|
|
56
|
+
acl = 'public-read',
|
|
57
|
+
contentType,
|
|
58
|
+
partSize
|
|
59
|
+
} ) => {
|
|
60
|
+
const source = encodeCopySource ( sourceBucket, sourceKey );
|
|
61
|
+
const head = await client.send ( new HeadObjectCommand ( {
|
|
62
|
+
Bucket: sourceBucket,
|
|
63
|
+
Key: sourceKey
|
|
64
|
+
} ) );
|
|
65
|
+
const contentLength = Number ( head.ContentLength );
|
|
66
|
+
|
|
67
|
+
if ( !Number.isSafeInteger ( contentLength ) || contentLength <= 0 ) {
|
|
68
|
+
throw new Error ( `S3 object ${sourceBucket}/${sourceKey} did not return a valid content length` );
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if ( contentLength <= SINGLE_COPY_LIMIT_BYTES ) {
|
|
72
|
+
await client.send ( new CopyObjectCommand ( {
|
|
73
|
+
CopySource: source,
|
|
74
|
+
Bucket: destinationBucket,
|
|
75
|
+
Key: destinationKey,
|
|
76
|
+
ACL: acl,
|
|
77
|
+
ContentType: contentType || head.ContentType,
|
|
78
|
+
MetadataDirective: contentType ? 'REPLACE' : 'COPY',
|
|
79
|
+
...( contentType ? { Metadata: head.Metadata || {} } : {} )
|
|
80
|
+
} ) );
|
|
81
|
+
|
|
82
|
+
return { contentLength, multipart: false, parts: 1 };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const created = await client.send ( new CreateMultipartUploadCommand ( {
|
|
86
|
+
Bucket: destinationBucket,
|
|
87
|
+
Key: destinationKey,
|
|
88
|
+
ACL: acl,
|
|
89
|
+
ContentType: contentType || head.ContentType,
|
|
90
|
+
CacheControl: head.CacheControl,
|
|
91
|
+
ContentDisposition: head.ContentDisposition,
|
|
92
|
+
ContentEncoding: head.ContentEncoding,
|
|
93
|
+
ContentLanguage: head.ContentLanguage,
|
|
94
|
+
Metadata: head.Metadata || {}
|
|
95
|
+
} ) );
|
|
96
|
+
const uploadId = created.UploadId;
|
|
97
|
+
|
|
98
|
+
if ( !uploadId ) {
|
|
99
|
+
throw new Error ( 'S3 did not return an upload id for multipart copy' );
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const completedParts = [];
|
|
104
|
+
for ( const range of buildCopyRanges ( contentLength, partSize ) ) {
|
|
105
|
+
const copied = await client.send ( new UploadPartCopyCommand ( {
|
|
106
|
+
Bucket: destinationBucket,
|
|
107
|
+
Key: destinationKey,
|
|
108
|
+
CopySource: source,
|
|
109
|
+
CopySourceRange: `bytes=${range.start}-${range.end}`,
|
|
110
|
+
PartNumber: range.partNumber,
|
|
111
|
+
UploadId: uploadId
|
|
112
|
+
} ) );
|
|
113
|
+
const etag = copied.CopyPartResult?.ETag;
|
|
114
|
+
if ( !etag ) throw new Error ( `S3 multipart copy part ${range.partNumber} did not return an ETag` );
|
|
115
|
+
completedParts.push ( { ETag: etag, PartNumber: range.partNumber } );
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
await client.send ( new CompleteMultipartUploadCommand ( {
|
|
119
|
+
Bucket: destinationBucket,
|
|
120
|
+
Key: destinationKey,
|
|
121
|
+
UploadId: uploadId,
|
|
122
|
+
MultipartUpload: { Parts: completedParts }
|
|
123
|
+
} ) );
|
|
124
|
+
|
|
125
|
+
return { contentLength, multipart: true, parts: completedParts.length };
|
|
126
|
+
}
|
|
127
|
+
catch ( error ) {
|
|
128
|
+
await client.send ( new AbortMultipartUploadCommand ( {
|
|
129
|
+
Bucket: destinationBucket,
|
|
130
|
+
Key: destinationKey,
|
|
131
|
+
UploadId: uploadId
|
|
132
|
+
} ) ).catch ( () => {} );
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
module.exports = {
|
|
138
|
+
DEFAULT_COPY_PART_BYTES,
|
|
139
|
+
SINGLE_COPY_LIMIT_BYTES,
|
|
140
|
+
buildCopyRanges,
|
|
141
|
+
copyLargeObject
|
|
142
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const crypto = require ( 'crypto' );
|
|
2
|
+
const http = require ( 'http' );
|
|
3
|
+
const { GetObjectCommand, HeadObjectCommand } = require ( '@aws-sdk/client-s3' );
|
|
4
|
+
|
|
5
|
+
const parseRange = ( value, contentLength ) => {
|
|
6
|
+
const match = /^bytes=(\d*)-(\d*)$/i.exec ( value || '' );
|
|
7
|
+
if ( !match ) return null;
|
|
8
|
+
|
|
9
|
+
let start;
|
|
10
|
+
let end;
|
|
11
|
+
if ( match[ 1 ] ) {
|
|
12
|
+
start = Number ( match[ 1 ] );
|
|
13
|
+
end = match[ 2 ] ? Number ( match[ 2 ] ) : contentLength - 1;
|
|
14
|
+
}
|
|
15
|
+
else if ( match[ 2 ] ) {
|
|
16
|
+
const suffixLength = Number ( match[ 2 ] );
|
|
17
|
+
start = Math.max ( contentLength - suffixLength, 0 );
|
|
18
|
+
end = contentLength - 1;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (
|
|
22
|
+
!Number.isSafeInteger ( start )
|
|
23
|
+
|| !Number.isSafeInteger ( end )
|
|
24
|
+
|| start < 0
|
|
25
|
+
|| end < start
|
|
26
|
+
|| start >= contentLength
|
|
27
|
+
) return null;
|
|
28
|
+
|
|
29
|
+
return { start, end: Math.min ( end, contentLength - 1 ) };
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const listen = server => new Promise ( ( resolve, reject ) => {
|
|
33
|
+
server.once ( 'error', reject );
|
|
34
|
+
server.listen ( 0, '127.0.0.1', () => {
|
|
35
|
+
server.off ( 'error', reject );
|
|
36
|
+
resolve ();
|
|
37
|
+
} );
|
|
38
|
+
} );
|
|
39
|
+
|
|
40
|
+
const close = server => new Promise ( resolve => server.close ( resolve ) );
|
|
41
|
+
|
|
42
|
+
const withLocalReadUrl = async ( { client, key, bucket }, callback ) => {
|
|
43
|
+
const head = await client.send ( new HeadObjectCommand ( { Bucket: bucket, Key: key } ) );
|
|
44
|
+
const contentLength = Number ( head.ContentLength );
|
|
45
|
+
if ( !Number.isSafeInteger ( contentLength ) || contentLength <= 0 ) {
|
|
46
|
+
throw new Error ( `S3 object ${bucket}/${key} did not return a valid content length` );
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const pathname = `/${crypto.randomUUID ()}`;
|
|
50
|
+
const server = http.createServer ( async ( request, response ) => {
|
|
51
|
+
if ( request.url !== pathname || ![ 'GET', 'HEAD' ].includes ( request.method ) ) {
|
|
52
|
+
response.writeHead ( 404 ).end ();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const range = request.headers.range ? parseRange ( request.headers.range, contentLength ) : null;
|
|
57
|
+
if ( request.headers.range && !range ) {
|
|
58
|
+
response.writeHead ( 416, { 'Content-Range': `bytes */${contentLength}` } ).end ();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const responseLength = range ? range.end - range.start + 1 : contentLength;
|
|
63
|
+
const headers = {
|
|
64
|
+
'Accept-Ranges': 'bytes',
|
|
65
|
+
'Content-Length': responseLength,
|
|
66
|
+
'Content-Type': head.ContentType || 'application/octet-stream',
|
|
67
|
+
...( range ? { 'Content-Range': `bytes ${range.start}-${range.end}/${contentLength}` } : {} )
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
if ( request.method === 'HEAD' ) {
|
|
71
|
+
response.writeHead ( range ? 206 : 200, headers ).end ();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const object = await client.send ( new GetObjectCommand ( {
|
|
77
|
+
Bucket: bucket,
|
|
78
|
+
Key: key,
|
|
79
|
+
...( range ? { Range: `bytes=${range.start}-${range.end}` } : {} )
|
|
80
|
+
} ) );
|
|
81
|
+
if ( !object.Body || typeof object.Body.pipe !== 'function' ) {
|
|
82
|
+
throw new Error ( 'S3 did not return a readable object body' );
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
response.writeHead ( range ? 206 : 200, headers );
|
|
86
|
+
response.once ( 'close', () => object.Body.destroy?. () );
|
|
87
|
+
object.Body.once ( 'error', () => response.destroy () );
|
|
88
|
+
object.Body.pipe ( response );
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
if ( !response.headersSent ) response.writeHead ( 502 ).end ();
|
|
92
|
+
else response.destroy ();
|
|
93
|
+
}
|
|
94
|
+
} );
|
|
95
|
+
|
|
96
|
+
await listen ( server );
|
|
97
|
+
const address = server.address ();
|
|
98
|
+
const url = `http://127.0.0.1:${address.port}${pathname}`;
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
return await callback ( url );
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
await close ( server );
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
module.exports = {
|
|
109
|
+
parseRange,
|
|
110
|
+
withLocalReadUrl
|
|
111
|
+
};
|