Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions packages/uploadfs/lib/storage/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
S3Client,
GetObjectCommand,
DeleteObjectCommand,
CopyObjectCommand,
PutObjectAclCommand
} = require('@aws-sdk/client-s3');
const { Upload } = require('@aws-sdk/lib-storage');
Expand Down Expand Up @@ -48,7 +49,7 @@ module.exports = function() {
}

bucket = options.bucket;
bucketObjectsACL = options.bucketObjectsACL || 'public-read';
bucketObjectsACL = (options.bucketObjectsACL === false) ? false : (options.bucketObjectsACL || 'public-read');
disabledBucketObjectsACL = options.disabledBucketObjectsACL || 'private';
noGzipContentTypes = options.noGzipContentTypes || require('./noGzipContentTypes');
addNoGzipContentTypes = options.addNoGzipContentTypes || [];
Expand Down Expand Up @@ -116,12 +117,15 @@ module.exports = function() {

const params = {
Bucket: bucket,
ACL: bucketObjectsACL,
Key: utils.removeLeadingSlash(self.options, path),
Body: inputStream,
ContentType: contentType
};

if (bucketObjectsACL !== false) {
params.ACL = bucketObjectsACL;
}

if (gzipAppropriate(contentType)) {
params.ContentEncoding = 'gzip';
const gzip = require('zlib').createGzip();
Expand Down Expand Up @@ -230,24 +234,36 @@ module.exports = function() {
},

enable: function(path, callback) {
if (self.options.disabledFileKey) {
const dPath = utils.getDisabledPath(path, self.options.disabledFileKey);
return self._copyObject(dPath, path, function(err) {
if (err) return callback(err);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the convention (no single-line statements in if, use {})

return self.remove(dPath, callback);
});
}
const command = new PutObjectAclCommand({
Bucket: bucket,
ACL: bucketObjectsACL,
Key: utils.removeLeadingSlash(self.options, path)
});

client.send(command)
.then(result => callback(null, result))
.catch(err => callback(err));
},

disable: function(path, callback) {
if (self.options.disabledFileKey) {
const dPath = utils.getDisabledPath(path, self.options.disabledFileKey);
return self._copyObject(path, dPath, function(err) {
if (err) return callback(err);
return self.remove(path, callback);
});
}
const command = new PutObjectAclCommand({
Bucket: bucket,
ACL: disabledBucketObjectsACL,
Key: utils.removeLeadingSlash(self.options, path)
});

client.send(command)
.then(result => callback(null, result))
.catch(err => callback(err));
Expand All @@ -264,6 +280,17 @@ module.exports = function() {
return utils.addPathToUrl(self.options, url, path);
},

_copyObject: function(srcPath, destPath, callback) {
const command = new CopyObjectCommand({
Bucket: bucket,
CopySource: bucket + '/' + utils.removeLeadingSlash(self.options, srcPath),
Key: utils.removeLeadingSlash(self.options, destPath)
});
client.send(command)
.then(result => callback(null, result))
.catch(err => callback(err));
},

destroy: function(callback) {
// No file descriptors or timeouts held
return callback(null);
Expand Down