-
Notifications
You must be signed in to change notification settings - Fork 3
Updating aws sdk to v3 #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0fbd28d
bdd260b
ea731a7
b2a9285
d8689fa
c9fff39
c99000d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,25 @@ | ||
| const AWS = require('aws-sdk'); | ||
| const { S3Client, DeleteObjectCommand, S3ServiceException} = require('@aws-sdk/client-s3'); | ||
|
|
||
| const { NodeHttpHandler } = require('@smithy/node-http-handler'); | ||
| const { Agent } = require('http'); | ||
| const awsError = require('./error.js'); | ||
| const parallel = require('parallel-stream'); | ||
|
|
||
| module.exports = function(bucket, options) { | ||
| options = options || {}; | ||
|
|
||
| const s3config = { | ||
| maxRetries: 10, | ||
| httpOptions: { connectTimeout: 3000 } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. connectTimeout and requestTimeout would not be the same thing. Please refer to here for example https://github.com/mapbox/aws-logs/blob/main/cdk/lib/forwarders/clients/s3.ts#L39-L43 |
||
| maxAttempts: 11, // Allows for 10 retries + 1 initial attempt | ||
| requestTimeout: 3000, | ||
| requestHandler: new NodeHttpHandler({ | ||
| httpAgent: new Agent({}), | ||
| connectionTimeout: 20 * 1000, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we keep this 3000 like before? |
||
| requestTimeout: 900 * 1000, | ||
| }), | ||
| }; | ||
| if (options.logger) s3config.logger = options.logger; | ||
| if (options.agent) s3config.httpOptions.agent = options.agent; | ||
| const s3 = options.s3 || new AWS.S3(s3config); | ||
| if (options.agent) s3config.requestHandler = { httpsAgent: options.agent }; | ||
| const s3 = options.s3 || new S3Client(s3config); | ||
|
|
||
| function write(key, enc, callback) { | ||
| key = key.toString(); | ||
|
|
@@ -23,14 +31,25 @@ module.exports = function(bucket, options) { | |
| }; | ||
|
|
||
| function removed(err) { | ||
| if (err && err.statusCode !== 404) return callback(awsError(err, params)); | ||
| if (err && err instanceof S3ServiceException && ( | ||
| err.name === 'NoSuchBucket' || | ||
| err.name === 'NoSuchKey' || | ||
| err.name === 'AccessDenied' | ||
| )) return callback(awsError(err, params)); | ||
| deleteStream.deleted++; | ||
| deleteStream.emit('deleted', key); | ||
| callback(); | ||
| } | ||
|
|
||
| if (options.dryrun) return removed(); | ||
| s3.deleteObject(params, removed); | ||
|
|
||
| s3.send(new DeleteObjectCommand(params)) | ||
| .then(function() { | ||
| removed(); | ||
| }) | ||
| .catch(function(err) { | ||
| removed(err); | ||
| }); | ||
| } | ||
|
|
||
| var starttime = Date.now(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * Simple S3 URL parser to replace @mapbox/s3urls dependency | ||
| * This avoids the transitive dependency on aws-sdk v2 through @mapbox/s3signed | ||
| */ | ||
|
|
||
| function fromUrl(s3url) { | ||
| if (!s3url || typeof s3url !== 'string') { | ||
| throw new Error('Invalid S3 URL'); | ||
| } | ||
|
|
||
| // Remove s3:// prefix | ||
| if (!s3url.startsWith('s3://')) { | ||
| throw new Error('URL must start with s3://'); | ||
| } | ||
|
|
||
| const urlWithoutProtocol = s3url.slice(5); // Remove 's3://' | ||
| const firstSlashIndex = urlWithoutProtocol.indexOf('/'); | ||
|
|
||
| let bucket, key; | ||
|
|
||
| if (firstSlashIndex === -1) { | ||
| // No slash found, entire string is bucket name | ||
| bucket = urlWithoutProtocol; | ||
| key = ''; | ||
| } else { | ||
| // Split at first slash | ||
| bucket = urlWithoutProtocol.slice(0, firstSlashIndex); | ||
| key = urlWithoutProtocol.slice(firstSlashIndex + 1); | ||
| } | ||
|
|
||
| return { | ||
| Bucket: bucket, | ||
| Key: key | ||
| }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| fromUrl: fromUrl | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a deprecated library so I just implemented the necessary functions in s3url-parser