Skip to content

Commit

Permalink
Merge pull request #200 from pioug/new-sdk
Browse files Browse the repository at this point in the history
Migrate to @aws-sdk/client-s3
  • Loading branch information
pioug authored Jan 23, 2022
2 parents d3e3ed1 + 5becd6a commit f728cd4
Show file tree
Hide file tree
Showing 6 changed files with 1,066 additions and 205 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ var publisher = awspublish.create({
params: {
Bucket: "..."
},
accessKeyId: "akid",
secretAccessKey: "secret"
credentials: {
accessKeyId: "akid",
secretAccessKey: "secret"
}
});
```

Expand Down
95 changes: 54 additions & 41 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var AWS = require('aws-sdk'),
s3pagingStream = require('./s3-paging-stream'),
var { S3 } = require('@aws-sdk/client-s3'),
Stream = require('stream'),
fs = require('fs'),
through = require('through2'),
Expand Down Expand Up @@ -45,7 +44,7 @@ function getContentType(file) {
* Turn the HTTP style headers into AWS Object params
*/

function toAwsParams(file) {
function toAwsParams(bucket, file) {
var params = {};

var headers = file.s3.headers || {};
Expand All @@ -60,6 +59,7 @@ function toAwsParams(file) {
}
}

params.Bucket = bucket;
params.Key = file.s3.path;
params.Body = file.contents;

Expand Down Expand Up @@ -114,7 +114,7 @@ function fileShouldBeDeleted(key, whitelist) {
return true;
}

function buildDeleteMultiple(keys) {
function buildDeleteMultiple(Bucket, keys) {
if (!keys || !keys.length) return;

var deleteObjects = keys.map(function (k) {
Expand All @@ -123,6 +123,7 @@ function buildDeleteMultiple(keys) {
var chunks = chunk(deleteObjects, 1000);
return chunks.map(function (each) {
return {
Bucket,
Delete: {
Objects: each,
},
Expand Down Expand Up @@ -202,8 +203,12 @@ module.exports.reporter = function (param) {
*/

function Publisher(AWSConfig, cacheOptions) {
if (!AWSConfig.region) {
AWSConfig.region = 'aws-global';
}

this.config = AWSConfig;
this.client = new AWS.S3(AWSConfig);
this.client = new S3(AWSConfig);
var bucket = this.config.params.Bucket;

if (!bucket) {
Expand Down Expand Up @@ -348,9 +353,14 @@ Publisher.prototype.publish = function (headers, options) {
if (options.simulate) return cb(null, file);

// get s3 headers
_this.client.headObject({ Key: file.s3.path }, function (err, res) {
const params = {
Bucket: _this.client.config.params.Bucket,
Key: file.s3.path,
};
_this.client.headObject(params, function (err, res) {
//ignore 403 and 404 errors since we're checking if a file exists on s3
if (err && [403, 404].indexOf(err.statusCode) < 0) return cb(err);
if (err && [403, 404].indexOf(err.$response.statusCode) < 0)
return cb(err);

res = res || {};

Expand All @@ -370,7 +380,8 @@ Publisher.prototype.publish = function (headers, options) {
} else {
file.s3.state = res.ETag ? 'update' : 'create';

_this.client.putObject(toAwsParams(file), function (err) {
const params = toAwsParams(_this.client.config.params.Bucket, file);
_this.client.putObject(params, function (err) {
if (err) return cb(err);

file.s3.date = new Date();
Expand Down Expand Up @@ -405,46 +416,48 @@ Publisher.prototype.sync = function (prefix, whitelistedFiles) {
cb();
};

stream._flush = function (cb) {
var toDelete = [],
lister;

lister = s3pagingStream(
client.listObjectsV2({ Prefix: prefix }),
function (_in) {
return _in.Key;
}
);
stream._flush = async function (cb) {
const toDelete = [];
let objects = [];
let token = void 0;

lister.on('data', function (key) {
var deleteFile;
if (newFiles[key]) return;
if (!fileShouldBeDeleted(key, whitelistedFiles)) return;
deleteFile = new Vinyl({});
do {
const { Contents, NextContinuationToken } = await client.listObjectsV2({
Bucket: client.config.params.Bucket,
Prefix: prefix,
ContinuationToken: token,
});
objects = objects.concat(Contents);
token = NextContinuationToken;
} while (token);

for (const { Key } of objects) {
if (newFiles[Key]) continue;
if (!fileShouldBeDeleted(Key, whitelistedFiles)) continue;
const deleteFile = new Vinyl({});
deleteFile.s3 = {
path: key,
path: Key,
state: 'delete',
headers: {},
};

stream.push(deleteFile);
toDelete.push(key);
});

lister.on('end', function () {
if (!toDelete.length) return cb();
Promise.all(
buildDeleteMultiple(toDelete).map(function (each) {
return client.deleteObjects(each).promise();
})
)
.then(function () {
cb();
})
.catch(function (e) {
cb(e);
});
});
toDelete.push(Key);
}

Promise.all(
buildDeleteMultiple(client.config.params.Bucket, toDelete).map(function (
each
) {
return client.deleteObjects(each);
})
)
.then(function () {
cb();
})
.catch(function (e) {
cb(e);
});
};

return stream;
Expand Down
42 changes: 0 additions & 42 deletions lib/s3-paging-stream.js

This file was deleted.

Loading

0 comments on commit f728cd4

Please sign in to comment.