@hdriel/aws-utils 1.0.2 → 1.0.3
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/dist/index.cjs +143 -67
- package/dist/index.d.cts +18 -1
- package/dist/index.d.ts +18 -1
- package/dist/index.js +143 -67
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -298,6 +298,7 @@ var parseRangeHeader = (range, contentLength, chunkSize) => {
|
|
|
298
298
|
}
|
|
299
299
|
return [start, Math.min(end, end)];
|
|
300
300
|
};
|
|
301
|
+
var getNormalizedPath = (directoryPath) => decodeURIComponent((directoryPath == null ? void 0 : directoryPath.replace(/^\/+/, "").replace(/\/+$/, "")) || "");
|
|
301
302
|
var S3BucketUtil = class _S3BucketUtil {
|
|
302
303
|
constructor({
|
|
303
304
|
logger: logger2,
|
|
@@ -606,7 +607,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
606
607
|
// ##### DIRECTORY BLOCK ##########################
|
|
607
608
|
createDirectory(directoryPath) {
|
|
608
609
|
return __async(this, null, function* () {
|
|
609
|
-
let normalizedPath =
|
|
610
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
610
611
|
if (!normalizedPath) throw new Error("No directory path provided");
|
|
611
612
|
if (normalizedPath === "/") normalizedPath = "";
|
|
612
613
|
const command = new import_client_s3.PutObjectCommand({ Bucket: this.bucket, Key: `${normalizedPath}/` });
|
|
@@ -617,10 +618,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
617
618
|
deleteDirectory(directoryPath) {
|
|
618
619
|
return __async(this, null, function* () {
|
|
619
620
|
var _a2, _b, _c, _d, _e, _f, _g;
|
|
620
|
-
let normalizedPath =
|
|
621
|
-
if (!normalizedPath)
|
|
622
|
-
throw new Error("No directory path provided");
|
|
623
|
-
}
|
|
621
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
622
|
+
if (!normalizedPath) throw new Error("No directory path provided");
|
|
624
623
|
if (normalizedPath === "/") normalizedPath = "";
|
|
625
624
|
let totalDeletedCount = 0;
|
|
626
625
|
let ContinuationToken = void 0;
|
|
@@ -687,8 +686,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
687
686
|
}
|
|
688
687
|
directoryList(directoryPath) {
|
|
689
688
|
return __async(this, null, function* () {
|
|
690
|
-
let normalizedPath =
|
|
691
|
-
if (
|
|
689
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
690
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
692
691
|
else normalizedPath = "";
|
|
693
692
|
const result = yield this.execute(
|
|
694
693
|
new import_client_s3.ListObjectsV2Command({
|
|
@@ -712,14 +711,63 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
712
711
|
return { directories, files };
|
|
713
712
|
});
|
|
714
713
|
}
|
|
714
|
+
directoryListPaginated(_0) {
|
|
715
|
+
return __async(this, arguments, function* (directoryPath, {
|
|
716
|
+
pageSize = 100,
|
|
717
|
+
pageNumber = 0
|
|
718
|
+
// 0-based: page 0 = items 0-99, page 1 = items 100-199, page 2 = items 200-299
|
|
719
|
+
} = {}) {
|
|
720
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
721
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
722
|
+
else normalizedPath = "";
|
|
723
|
+
let continuationToken = void 0;
|
|
724
|
+
let currentPage = 0;
|
|
725
|
+
let allDirectories = [];
|
|
726
|
+
let allFiles = [];
|
|
727
|
+
while (currentPage <= pageNumber) {
|
|
728
|
+
const result = yield this.execute(
|
|
729
|
+
new import_client_s3.ListObjectsV2Command({
|
|
730
|
+
Bucket: this.bucket,
|
|
731
|
+
Prefix: normalizedPath,
|
|
732
|
+
Delimiter: "/",
|
|
733
|
+
MaxKeys: pageSize,
|
|
734
|
+
ContinuationToken: continuationToken
|
|
735
|
+
})
|
|
736
|
+
);
|
|
737
|
+
if (currentPage === pageNumber) {
|
|
738
|
+
allDirectories = (result.CommonPrefixes || []).map((prefix) => prefix.Prefix).map((prefix) => {
|
|
739
|
+
const relativePath = prefix.replace(normalizedPath, "");
|
|
740
|
+
return relativePath.replace(/\/$/, "");
|
|
741
|
+
}).filter((dir) => dir);
|
|
742
|
+
allFiles = (result.Contents || []).filter((content) => {
|
|
743
|
+
var _a2;
|
|
744
|
+
return content.Key !== normalizedPath && !((_a2 = content.Key) == null ? void 0 : _a2.endsWith("/"));
|
|
745
|
+
}).map((content) => __spreadProps(__spreadValues({}, content), {
|
|
746
|
+
Name: content.Key.replace(normalizedPath, "") || content.Key,
|
|
747
|
+
LastModified: new Date(content.LastModified)
|
|
748
|
+
}));
|
|
749
|
+
}
|
|
750
|
+
continuationToken = result.NextContinuationToken;
|
|
751
|
+
if (!result.IsTruncated || !continuationToken) {
|
|
752
|
+
break;
|
|
753
|
+
}
|
|
754
|
+
currentPage++;
|
|
755
|
+
}
|
|
756
|
+
return {
|
|
757
|
+
directories: allDirectories,
|
|
758
|
+
files: allFiles,
|
|
759
|
+
totalFetched: allFiles.length + allDirectories.length
|
|
760
|
+
};
|
|
761
|
+
});
|
|
762
|
+
}
|
|
715
763
|
/**
|
|
716
764
|
* Get all files recursively (example for search/indexing)
|
|
717
765
|
* @param directoryPath
|
|
718
766
|
*/
|
|
719
767
|
directoryListRecursive(directoryPath) {
|
|
720
768
|
return __async(this, null, function* () {
|
|
721
|
-
let normalizedPath =
|
|
722
|
-
if (
|
|
769
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
770
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
723
771
|
else normalizedPath = "";
|
|
724
772
|
const allDirectories = [];
|
|
725
773
|
const allFiles = [];
|
|
@@ -783,7 +831,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
783
831
|
*/
|
|
784
832
|
directoryTree(directoryPath) {
|
|
785
833
|
return __async(this, null, function* () {
|
|
786
|
-
let normalizedPath =
|
|
834
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
787
835
|
const lastDirectory = directoryPath == null ? void 0 : directoryPath.split("/").pop();
|
|
788
836
|
const { directories, files } = yield this.directoryList(normalizedPath);
|
|
789
837
|
if (directoryPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
@@ -814,7 +862,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
814
862
|
// ##### FILES BLOCK ##########################
|
|
815
863
|
fileInfo(filePath) {
|
|
816
864
|
return __async(this, null, function* () {
|
|
817
|
-
const normalizedKey =
|
|
865
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
818
866
|
if (!normalizedKey || normalizedKey === "/") {
|
|
819
867
|
throw new Error("No file key provided");
|
|
820
868
|
}
|
|
@@ -825,8 +873,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
825
873
|
fileListInfo(directoryPath, fileNamePrefix) {
|
|
826
874
|
return __async(this, null, function* () {
|
|
827
875
|
var _a2, _b;
|
|
828
|
-
let normalizedPath =
|
|
829
|
-
if (
|
|
876
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
877
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
830
878
|
else normalizedPath = "";
|
|
831
879
|
const prefix = normalizedPath + (fileNamePrefix || "");
|
|
832
880
|
const command = new import_client_s3.ListObjectsCommand({
|
|
@@ -848,13 +896,65 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
848
896
|
return files;
|
|
849
897
|
});
|
|
850
898
|
}
|
|
899
|
+
fileListInfoPaginated(_0) {
|
|
900
|
+
return __async(this, arguments, function* (directoryPath, {
|
|
901
|
+
fileNamePrefix,
|
|
902
|
+
pageNumber = 0,
|
|
903
|
+
// 0-based: page 0 = items 0-99, page 1 = items 100-199, page 2 = items 200-299
|
|
904
|
+
pageSize = 100
|
|
905
|
+
} = {}) {
|
|
906
|
+
var _a2, _b;
|
|
907
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
908
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
909
|
+
else normalizedPath = "";
|
|
910
|
+
const prefix = normalizedPath + (fileNamePrefix || "");
|
|
911
|
+
let continuationToken;
|
|
912
|
+
let currentPage = 0;
|
|
913
|
+
let resultFiles = [];
|
|
914
|
+
while (currentPage <= pageNumber) {
|
|
915
|
+
const result = yield this.execute(
|
|
916
|
+
new import_client_s3.ListObjectsV2Command({
|
|
917
|
+
Bucket: this.bucket,
|
|
918
|
+
Prefix: prefix,
|
|
919
|
+
Delimiter: "/",
|
|
920
|
+
MaxKeys: pageSize,
|
|
921
|
+
ContinuationToken: continuationToken
|
|
922
|
+
})
|
|
923
|
+
);
|
|
924
|
+
if (currentPage === pageNumber) {
|
|
925
|
+
resultFiles = ((_a2 = result.Contents) != null ? _a2 : []).filter((v) => v).map(
|
|
926
|
+
(content) => {
|
|
927
|
+
var _a3, _b2;
|
|
928
|
+
return __spreadProps(__spreadValues({}, content), {
|
|
929
|
+
Name: (_b2 = (_a3 = content.Key) == null ? void 0 : _a3.replace(prefix, "")) != null ? _b2 : content.Key,
|
|
930
|
+
LastModified: content.LastModified ? new Date(content.LastModified) : null
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
).filter((content) => content.Name);
|
|
934
|
+
}
|
|
935
|
+
continuationToken = result.NextContinuationToken;
|
|
936
|
+
if (!result.IsTruncated || !continuationToken) {
|
|
937
|
+
break;
|
|
938
|
+
}
|
|
939
|
+
currentPage++;
|
|
940
|
+
}
|
|
941
|
+
(_b = this.logger) == null ? void 0 : _b.debug(null, "file list info paginated", {
|
|
942
|
+
prefix,
|
|
943
|
+
pageNumber,
|
|
944
|
+
pageSize,
|
|
945
|
+
fileCount: resultFiles.length
|
|
946
|
+
});
|
|
947
|
+
return {
|
|
948
|
+
files: resultFiles,
|
|
949
|
+
totalFetched: resultFiles.length
|
|
950
|
+
};
|
|
951
|
+
});
|
|
952
|
+
}
|
|
851
953
|
taggingFile(filePath, tagVersion = "1.0.0") {
|
|
852
954
|
return __async(this, null, function* () {
|
|
853
955
|
try {
|
|
854
|
-
const normalizedKey =
|
|
855
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
856
|
-
throw new Error("No file key provided");
|
|
857
|
-
}
|
|
956
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
957
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
858
958
|
const command = new import_client_s3.PutObjectTaggingCommand({
|
|
859
959
|
Bucket: this.bucket,
|
|
860
960
|
Key: normalizedKey,
|
|
@@ -870,10 +970,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
870
970
|
fileVersion(filePath) {
|
|
871
971
|
return __async(this, null, function* () {
|
|
872
972
|
var _a2, _b;
|
|
873
|
-
const normalizedKey =
|
|
874
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
875
|
-
throw new Error("No file key provided");
|
|
876
|
-
}
|
|
973
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
974
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
877
975
|
const command = new import_client_s3.GetObjectTaggingCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
878
976
|
const result = yield this.execute(command);
|
|
879
977
|
const tag = (_a2 = result.TagSet) == null ? void 0 : _a2.find((tag2) => tag2.Key === "version");
|
|
@@ -883,10 +981,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
883
981
|
fileUrl(filePath, expiresIn = "15m") {
|
|
884
982
|
return __async(this, null, function* () {
|
|
885
983
|
var _a2;
|
|
886
|
-
const normalizedKey =
|
|
887
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
888
|
-
throw new Error("No file key provided");
|
|
889
|
-
}
|
|
984
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
985
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
890
986
|
const expiresInSeconds = typeof expiresIn === "number" ? expiresIn : (0, import_ms.default)(expiresIn) / 1e3;
|
|
891
987
|
const command = new import_client_s3.GetObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
892
988
|
const url = yield (0, import_s3_request_presigner.getSignedUrl)(this.s3Client, command, {
|
|
@@ -900,10 +996,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
900
996
|
sizeOf(filePath, unit = "bytes") {
|
|
901
997
|
return __async(this, null, function* () {
|
|
902
998
|
var _a2, _b, _c;
|
|
903
|
-
const normalizedKey =
|
|
904
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
905
|
-
throw new Error("No file key provided");
|
|
906
|
-
}
|
|
999
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1000
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
907
1001
|
try {
|
|
908
1002
|
const command = new import_client_s3.HeadObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
909
1003
|
const headObject = yield this.execute(command);
|
|
@@ -931,10 +1025,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
931
1025
|
return __async(this, null, function* () {
|
|
932
1026
|
var _a2;
|
|
933
1027
|
try {
|
|
934
|
-
const normalizedKey =
|
|
935
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
936
|
-
throw new Error("No file key provided");
|
|
937
|
-
}
|
|
1028
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1029
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
938
1030
|
const command = new import_client_s3.HeadObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
939
1031
|
yield this.execute(command);
|
|
940
1032
|
return true;
|
|
@@ -948,10 +1040,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
948
1040
|
}
|
|
949
1041
|
fileContent(filePath, format = "buffer") {
|
|
950
1042
|
return __async(this, null, function* () {
|
|
951
|
-
const normalizedKey =
|
|
952
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
953
|
-
throw new Error("No file key provided");
|
|
954
|
-
}
|
|
1043
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1044
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
955
1045
|
const command = new import_client_s3.GetObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
956
1046
|
const result = yield this.execute(command);
|
|
957
1047
|
if (!result.Body) {
|
|
@@ -983,10 +1073,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
983
1073
|
}
|
|
984
1074
|
uploadFile(_0, _1) {
|
|
985
1075
|
return __async(this, arguments, function* (filePath, fileData, acl = "private" /* private */, version = "1.0.0") {
|
|
986
|
-
const normalizedKey =
|
|
987
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
988
|
-
throw new Error("No file key provided");
|
|
989
|
-
}
|
|
1076
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1077
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
990
1078
|
const upload = new import_lib_storage.Upload({
|
|
991
1079
|
client: this.s3Client,
|
|
992
1080
|
params: {
|
|
@@ -1008,10 +1096,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1008
1096
|
}
|
|
1009
1097
|
deleteFile(filePath) {
|
|
1010
1098
|
return __async(this, null, function* () {
|
|
1011
|
-
const normalizedKey =
|
|
1012
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
1013
|
-
throw new Error("No file key provided");
|
|
1014
|
-
}
|
|
1099
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1100
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
1015
1101
|
const command = new import_client_s3.DeleteObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
1016
1102
|
return yield this.execute(command);
|
|
1017
1103
|
});
|
|
@@ -1023,10 +1109,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1023
1109
|
checkFileExists = true,
|
|
1024
1110
|
abortSignal
|
|
1025
1111
|
} = {}) {
|
|
1026
|
-
const normalizedKey =
|
|
1027
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
1028
|
-
throw new Error("No file key provided");
|
|
1029
|
-
}
|
|
1112
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1113
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
1030
1114
|
if (checkFileExists) {
|
|
1031
1115
|
const isExists = yield this.fileExists(normalizedKey);
|
|
1032
1116
|
if (!isExists) return null;
|
|
@@ -1049,10 +1133,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1049
1133
|
abortSignal
|
|
1050
1134
|
}) {
|
|
1051
1135
|
var _a2;
|
|
1052
|
-
const normalizedKey =
|
|
1053
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
1054
|
-
throw new Error("No file key provided");
|
|
1055
|
-
}
|
|
1136
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1137
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
1056
1138
|
try {
|
|
1057
1139
|
const cmd = new import_client_s3.GetObjectCommand(__spreadValues({
|
|
1058
1140
|
Bucket: this.bucket,
|
|
@@ -1091,9 +1173,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1091
1173
|
}) {
|
|
1092
1174
|
return (req, res, next) => __async(this, null, function* () {
|
|
1093
1175
|
var _a2, _b, _c, _d, _e;
|
|
1094
|
-
const filePaths = [].concat(filePath).map((filePath2) =>
|
|
1095
|
-
return decodeURIComponent((filePath2 == null ? void 0 : filePath2.replace(/^\//, "").replace(/\/$/, "")) || "");
|
|
1096
|
-
}).filter((v) => v && v !== "/");
|
|
1176
|
+
const filePaths = [].concat(filePath).map((filePath2) => getNormalizedPath(filePath2)).filter((v) => v && v !== "/");
|
|
1097
1177
|
if (!filePaths.length) {
|
|
1098
1178
|
throw new Error("No file keys provided");
|
|
1099
1179
|
}
|
|
@@ -1234,10 +1314,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1234
1314
|
(_a3 = stream == null ? void 0 : stream.destroy) == null ? void 0 : _a3.call(stream);
|
|
1235
1315
|
};
|
|
1236
1316
|
req.once("close", onClose);
|
|
1237
|
-
const normalizedKey =
|
|
1238
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
1239
|
-
throw new Error("No file key provided");
|
|
1240
|
-
}
|
|
1317
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1318
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
1241
1319
|
try {
|
|
1242
1320
|
const isExists = yield this.fileExists(normalizedKey);
|
|
1243
1321
|
if (!isExists) {
|
|
@@ -1308,10 +1386,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1308
1386
|
}) {
|
|
1309
1387
|
return (req, res, next) => __async(this, null, function* () {
|
|
1310
1388
|
var _a2, _b, _c, _d, _e, _f;
|
|
1311
|
-
const normalizedKey =
|
|
1312
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
1313
|
-
throw new Error("No file key provided");
|
|
1314
|
-
}
|
|
1389
|
+
const normalizedKey = getNormalizedPath(fileKey);
|
|
1390
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
1315
1391
|
const isExists = yield this.fileExists(normalizedKey);
|
|
1316
1392
|
const fileSize = yield this.sizeOf(normalizedKey);
|
|
1317
1393
|
let Range;
|
|
@@ -1439,7 +1515,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1439
1515
|
}
|
|
1440
1516
|
return fileSize;
|
|
1441
1517
|
}
|
|
1442
|
-
getUploadFileMW(
|
|
1518
|
+
getUploadFileMW(directoryPath, {
|
|
1443
1519
|
acl = "private" /* private */,
|
|
1444
1520
|
maxFileSize,
|
|
1445
1521
|
filename: _filename,
|
|
@@ -1447,8 +1523,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1447
1523
|
fileExt = [],
|
|
1448
1524
|
metadata: customMetadata
|
|
1449
1525
|
} = {}) {
|
|
1450
|
-
let normalizedPath =
|
|
1451
|
-
if (
|
|
1526
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
1527
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
1452
1528
|
else normalizedPath = "";
|
|
1453
1529
|
const fileSize = this.getFileSize(maxFileSize);
|
|
1454
1530
|
const fileTypes = [].concat(fileType);
|
package/dist/index.d.cts
CHANGED
|
@@ -179,6 +179,14 @@ declare class S3BucketUtil {
|
|
|
179
179
|
directories: string[];
|
|
180
180
|
files: ContentFile[];
|
|
181
181
|
}>;
|
|
182
|
+
directoryListPaginated(directoryPath?: string, { pageSize, pageNumber, }?: {
|
|
183
|
+
pageSize?: number;
|
|
184
|
+
pageNumber?: number;
|
|
185
|
+
}): Promise<{
|
|
186
|
+
directories: string[];
|
|
187
|
+
files: ContentFile[];
|
|
188
|
+
totalFetched: number;
|
|
189
|
+
}>;
|
|
182
190
|
/**
|
|
183
191
|
* Get all files recursively (example for search/indexing)
|
|
184
192
|
* @param directoryPath
|
|
@@ -221,6 +229,15 @@ declare class S3BucketUtil {
|
|
|
221
229
|
directoryTree(directoryPath?: string): Promise<TreeDirectoryItem>;
|
|
222
230
|
fileInfo(filePath: string): Promise<HeadObjectCommandOutput>;
|
|
223
231
|
fileListInfo(directoryPath?: string, fileNamePrefix?: string): Promise<ContentFile[]>;
|
|
232
|
+
fileListInfoPaginated(directoryPath?: string, { fileNamePrefix, pageNumber, // 0-based: page 0 = items 0-99, page 1 = items 100-199, page 2 = items 200-299
|
|
233
|
+
pageSize, }?: {
|
|
234
|
+
fileNamePrefix?: string;
|
|
235
|
+
pageSize?: number;
|
|
236
|
+
pageNumber?: number;
|
|
237
|
+
}): Promise<{
|
|
238
|
+
files: ContentFile[];
|
|
239
|
+
totalFetched: number;
|
|
240
|
+
}>;
|
|
224
241
|
taggingFile(filePath: string, tagVersion?: string): Promise<boolean>;
|
|
225
242
|
fileVersion(filePath: string): Promise<string>;
|
|
226
243
|
fileUrl(filePath: string, expiresIn?: number | StringValue): Promise<string>;
|
|
@@ -249,7 +266,7 @@ declare class S3BucketUtil {
|
|
|
249
266
|
}): Promise<(req: Request$1 & any, res: Response & any, next: NextFunction & any) => Promise<any>>;
|
|
250
267
|
private static fileFilter;
|
|
251
268
|
private getFileSize;
|
|
252
|
-
getUploadFileMW(
|
|
269
|
+
getUploadFileMW(directoryPath?: string, { acl, maxFileSize, filename: _filename, fileType, fileExt, metadata: customMetadata, }?: S3UploadOptions): Multer;
|
|
253
270
|
/**
|
|
254
271
|
* Middleware for uploading a single file
|
|
255
272
|
* Adds the uploaded file info to req.s3File
|
package/dist/index.d.ts
CHANGED
|
@@ -179,6 +179,14 @@ declare class S3BucketUtil {
|
|
|
179
179
|
directories: string[];
|
|
180
180
|
files: ContentFile[];
|
|
181
181
|
}>;
|
|
182
|
+
directoryListPaginated(directoryPath?: string, { pageSize, pageNumber, }?: {
|
|
183
|
+
pageSize?: number;
|
|
184
|
+
pageNumber?: number;
|
|
185
|
+
}): Promise<{
|
|
186
|
+
directories: string[];
|
|
187
|
+
files: ContentFile[];
|
|
188
|
+
totalFetched: number;
|
|
189
|
+
}>;
|
|
182
190
|
/**
|
|
183
191
|
* Get all files recursively (example for search/indexing)
|
|
184
192
|
* @param directoryPath
|
|
@@ -221,6 +229,15 @@ declare class S3BucketUtil {
|
|
|
221
229
|
directoryTree(directoryPath?: string): Promise<TreeDirectoryItem>;
|
|
222
230
|
fileInfo(filePath: string): Promise<HeadObjectCommandOutput>;
|
|
223
231
|
fileListInfo(directoryPath?: string, fileNamePrefix?: string): Promise<ContentFile[]>;
|
|
232
|
+
fileListInfoPaginated(directoryPath?: string, { fileNamePrefix, pageNumber, // 0-based: page 0 = items 0-99, page 1 = items 100-199, page 2 = items 200-299
|
|
233
|
+
pageSize, }?: {
|
|
234
|
+
fileNamePrefix?: string;
|
|
235
|
+
pageSize?: number;
|
|
236
|
+
pageNumber?: number;
|
|
237
|
+
}): Promise<{
|
|
238
|
+
files: ContentFile[];
|
|
239
|
+
totalFetched: number;
|
|
240
|
+
}>;
|
|
224
241
|
taggingFile(filePath: string, tagVersion?: string): Promise<boolean>;
|
|
225
242
|
fileVersion(filePath: string): Promise<string>;
|
|
226
243
|
fileUrl(filePath: string, expiresIn?: number | StringValue): Promise<string>;
|
|
@@ -249,7 +266,7 @@ declare class S3BucketUtil {
|
|
|
249
266
|
}): Promise<(req: Request$1 & any, res: Response & any, next: NextFunction & any) => Promise<any>>;
|
|
250
267
|
private static fileFilter;
|
|
251
268
|
private getFileSize;
|
|
252
|
-
getUploadFileMW(
|
|
269
|
+
getUploadFileMW(directoryPath?: string, { acl, maxFileSize, filename: _filename, fileType, fileExt, metadata: customMetadata, }?: S3UploadOptions): Multer;
|
|
253
270
|
/**
|
|
254
271
|
* Middleware for uploading a single file
|
|
255
272
|
* Adds the uploaded file info to req.s3File
|
package/dist/index.js
CHANGED
|
@@ -282,6 +282,7 @@ var parseRangeHeader = (range, contentLength, chunkSize) => {
|
|
|
282
282
|
}
|
|
283
283
|
return [start, Math.min(end, end)];
|
|
284
284
|
};
|
|
285
|
+
var getNormalizedPath = (directoryPath) => decodeURIComponent((directoryPath == null ? void 0 : directoryPath.replace(/^\/+/, "").replace(/\/+$/, "")) || "");
|
|
285
286
|
var S3BucketUtil = class _S3BucketUtil {
|
|
286
287
|
constructor({
|
|
287
288
|
logger: logger2,
|
|
@@ -590,7 +591,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
590
591
|
// ##### DIRECTORY BLOCK ##########################
|
|
591
592
|
createDirectory(directoryPath) {
|
|
592
593
|
return __async(this, null, function* () {
|
|
593
|
-
let normalizedPath =
|
|
594
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
594
595
|
if (!normalizedPath) throw new Error("No directory path provided");
|
|
595
596
|
if (normalizedPath === "/") normalizedPath = "";
|
|
596
597
|
const command = new PutObjectCommand({ Bucket: this.bucket, Key: `${normalizedPath}/` });
|
|
@@ -601,10 +602,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
601
602
|
deleteDirectory(directoryPath) {
|
|
602
603
|
return __async(this, null, function* () {
|
|
603
604
|
var _a2, _b, _c, _d, _e, _f, _g;
|
|
604
|
-
let normalizedPath =
|
|
605
|
-
if (!normalizedPath)
|
|
606
|
-
throw new Error("No directory path provided");
|
|
607
|
-
}
|
|
605
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
606
|
+
if (!normalizedPath) throw new Error("No directory path provided");
|
|
608
607
|
if (normalizedPath === "/") normalizedPath = "";
|
|
609
608
|
let totalDeletedCount = 0;
|
|
610
609
|
let ContinuationToken = void 0;
|
|
@@ -671,8 +670,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
671
670
|
}
|
|
672
671
|
directoryList(directoryPath) {
|
|
673
672
|
return __async(this, null, function* () {
|
|
674
|
-
let normalizedPath =
|
|
675
|
-
if (
|
|
673
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
674
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
676
675
|
else normalizedPath = "";
|
|
677
676
|
const result = yield this.execute(
|
|
678
677
|
new ListObjectsV2Command({
|
|
@@ -696,14 +695,63 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
696
695
|
return { directories, files };
|
|
697
696
|
});
|
|
698
697
|
}
|
|
698
|
+
directoryListPaginated(_0) {
|
|
699
|
+
return __async(this, arguments, function* (directoryPath, {
|
|
700
|
+
pageSize = 100,
|
|
701
|
+
pageNumber = 0
|
|
702
|
+
// 0-based: page 0 = items 0-99, page 1 = items 100-199, page 2 = items 200-299
|
|
703
|
+
} = {}) {
|
|
704
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
705
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
706
|
+
else normalizedPath = "";
|
|
707
|
+
let continuationToken = void 0;
|
|
708
|
+
let currentPage = 0;
|
|
709
|
+
let allDirectories = [];
|
|
710
|
+
let allFiles = [];
|
|
711
|
+
while (currentPage <= pageNumber) {
|
|
712
|
+
const result = yield this.execute(
|
|
713
|
+
new ListObjectsV2Command({
|
|
714
|
+
Bucket: this.bucket,
|
|
715
|
+
Prefix: normalizedPath,
|
|
716
|
+
Delimiter: "/",
|
|
717
|
+
MaxKeys: pageSize,
|
|
718
|
+
ContinuationToken: continuationToken
|
|
719
|
+
})
|
|
720
|
+
);
|
|
721
|
+
if (currentPage === pageNumber) {
|
|
722
|
+
allDirectories = (result.CommonPrefixes || []).map((prefix) => prefix.Prefix).map((prefix) => {
|
|
723
|
+
const relativePath = prefix.replace(normalizedPath, "");
|
|
724
|
+
return relativePath.replace(/\/$/, "");
|
|
725
|
+
}).filter((dir) => dir);
|
|
726
|
+
allFiles = (result.Contents || []).filter((content) => {
|
|
727
|
+
var _a2;
|
|
728
|
+
return content.Key !== normalizedPath && !((_a2 = content.Key) == null ? void 0 : _a2.endsWith("/"));
|
|
729
|
+
}).map((content) => __spreadProps(__spreadValues({}, content), {
|
|
730
|
+
Name: content.Key.replace(normalizedPath, "") || content.Key,
|
|
731
|
+
LastModified: new Date(content.LastModified)
|
|
732
|
+
}));
|
|
733
|
+
}
|
|
734
|
+
continuationToken = result.NextContinuationToken;
|
|
735
|
+
if (!result.IsTruncated || !continuationToken) {
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
currentPage++;
|
|
739
|
+
}
|
|
740
|
+
return {
|
|
741
|
+
directories: allDirectories,
|
|
742
|
+
files: allFiles,
|
|
743
|
+
totalFetched: allFiles.length + allDirectories.length
|
|
744
|
+
};
|
|
745
|
+
});
|
|
746
|
+
}
|
|
699
747
|
/**
|
|
700
748
|
* Get all files recursively (example for search/indexing)
|
|
701
749
|
* @param directoryPath
|
|
702
750
|
*/
|
|
703
751
|
directoryListRecursive(directoryPath) {
|
|
704
752
|
return __async(this, null, function* () {
|
|
705
|
-
let normalizedPath =
|
|
706
|
-
if (
|
|
753
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
754
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
707
755
|
else normalizedPath = "";
|
|
708
756
|
const allDirectories = [];
|
|
709
757
|
const allFiles = [];
|
|
@@ -767,7 +815,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
767
815
|
*/
|
|
768
816
|
directoryTree(directoryPath) {
|
|
769
817
|
return __async(this, null, function* () {
|
|
770
|
-
let normalizedPath =
|
|
818
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
771
819
|
const lastDirectory = directoryPath == null ? void 0 : directoryPath.split("/").pop();
|
|
772
820
|
const { directories, files } = yield this.directoryList(normalizedPath);
|
|
773
821
|
if (directoryPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
@@ -798,7 +846,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
798
846
|
// ##### FILES BLOCK ##########################
|
|
799
847
|
fileInfo(filePath) {
|
|
800
848
|
return __async(this, null, function* () {
|
|
801
|
-
const normalizedKey =
|
|
849
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
802
850
|
if (!normalizedKey || normalizedKey === "/") {
|
|
803
851
|
throw new Error("No file key provided");
|
|
804
852
|
}
|
|
@@ -809,8 +857,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
809
857
|
fileListInfo(directoryPath, fileNamePrefix) {
|
|
810
858
|
return __async(this, null, function* () {
|
|
811
859
|
var _a2, _b;
|
|
812
|
-
let normalizedPath =
|
|
813
|
-
if (
|
|
860
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
861
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
814
862
|
else normalizedPath = "";
|
|
815
863
|
const prefix = normalizedPath + (fileNamePrefix || "");
|
|
816
864
|
const command = new ListObjectsCommand({
|
|
@@ -832,13 +880,65 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
832
880
|
return files;
|
|
833
881
|
});
|
|
834
882
|
}
|
|
883
|
+
fileListInfoPaginated(_0) {
|
|
884
|
+
return __async(this, arguments, function* (directoryPath, {
|
|
885
|
+
fileNamePrefix,
|
|
886
|
+
pageNumber = 0,
|
|
887
|
+
// 0-based: page 0 = items 0-99, page 1 = items 100-199, page 2 = items 200-299
|
|
888
|
+
pageSize = 100
|
|
889
|
+
} = {}) {
|
|
890
|
+
var _a2, _b;
|
|
891
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
892
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
893
|
+
else normalizedPath = "";
|
|
894
|
+
const prefix = normalizedPath + (fileNamePrefix || "");
|
|
895
|
+
let continuationToken;
|
|
896
|
+
let currentPage = 0;
|
|
897
|
+
let resultFiles = [];
|
|
898
|
+
while (currentPage <= pageNumber) {
|
|
899
|
+
const result = yield this.execute(
|
|
900
|
+
new ListObjectsV2Command({
|
|
901
|
+
Bucket: this.bucket,
|
|
902
|
+
Prefix: prefix,
|
|
903
|
+
Delimiter: "/",
|
|
904
|
+
MaxKeys: pageSize,
|
|
905
|
+
ContinuationToken: continuationToken
|
|
906
|
+
})
|
|
907
|
+
);
|
|
908
|
+
if (currentPage === pageNumber) {
|
|
909
|
+
resultFiles = ((_a2 = result.Contents) != null ? _a2 : []).filter((v) => v).map(
|
|
910
|
+
(content) => {
|
|
911
|
+
var _a3, _b2;
|
|
912
|
+
return __spreadProps(__spreadValues({}, content), {
|
|
913
|
+
Name: (_b2 = (_a3 = content.Key) == null ? void 0 : _a3.replace(prefix, "")) != null ? _b2 : content.Key,
|
|
914
|
+
LastModified: content.LastModified ? new Date(content.LastModified) : null
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
).filter((content) => content.Name);
|
|
918
|
+
}
|
|
919
|
+
continuationToken = result.NextContinuationToken;
|
|
920
|
+
if (!result.IsTruncated || !continuationToken) {
|
|
921
|
+
break;
|
|
922
|
+
}
|
|
923
|
+
currentPage++;
|
|
924
|
+
}
|
|
925
|
+
(_b = this.logger) == null ? void 0 : _b.debug(null, "file list info paginated", {
|
|
926
|
+
prefix,
|
|
927
|
+
pageNumber,
|
|
928
|
+
pageSize,
|
|
929
|
+
fileCount: resultFiles.length
|
|
930
|
+
});
|
|
931
|
+
return {
|
|
932
|
+
files: resultFiles,
|
|
933
|
+
totalFetched: resultFiles.length
|
|
934
|
+
};
|
|
935
|
+
});
|
|
936
|
+
}
|
|
835
937
|
taggingFile(filePath, tagVersion = "1.0.0") {
|
|
836
938
|
return __async(this, null, function* () {
|
|
837
939
|
try {
|
|
838
|
-
const normalizedKey =
|
|
839
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
840
|
-
throw new Error("No file key provided");
|
|
841
|
-
}
|
|
940
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
941
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
842
942
|
const command = new PutObjectTaggingCommand({
|
|
843
943
|
Bucket: this.bucket,
|
|
844
944
|
Key: normalizedKey,
|
|
@@ -854,10 +954,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
854
954
|
fileVersion(filePath) {
|
|
855
955
|
return __async(this, null, function* () {
|
|
856
956
|
var _a2, _b;
|
|
857
|
-
const normalizedKey =
|
|
858
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
859
|
-
throw new Error("No file key provided");
|
|
860
|
-
}
|
|
957
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
958
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
861
959
|
const command = new GetObjectTaggingCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
862
960
|
const result = yield this.execute(command);
|
|
863
961
|
const tag = (_a2 = result.TagSet) == null ? void 0 : _a2.find((tag2) => tag2.Key === "version");
|
|
@@ -867,10 +965,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
867
965
|
fileUrl(filePath, expiresIn = "15m") {
|
|
868
966
|
return __async(this, null, function* () {
|
|
869
967
|
var _a2;
|
|
870
|
-
const normalizedKey =
|
|
871
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
872
|
-
throw new Error("No file key provided");
|
|
873
|
-
}
|
|
968
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
969
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
874
970
|
const expiresInSeconds = typeof expiresIn === "number" ? expiresIn : ms(expiresIn) / 1e3;
|
|
875
971
|
const command = new GetObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
876
972
|
const url = yield getSignedUrl(this.s3Client, command, {
|
|
@@ -884,10 +980,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
884
980
|
sizeOf(filePath, unit = "bytes") {
|
|
885
981
|
return __async(this, null, function* () {
|
|
886
982
|
var _a2, _b, _c;
|
|
887
|
-
const normalizedKey =
|
|
888
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
889
|
-
throw new Error("No file key provided");
|
|
890
|
-
}
|
|
983
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
984
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
891
985
|
try {
|
|
892
986
|
const command = new HeadObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
893
987
|
const headObject = yield this.execute(command);
|
|
@@ -915,10 +1009,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
915
1009
|
return __async(this, null, function* () {
|
|
916
1010
|
var _a2;
|
|
917
1011
|
try {
|
|
918
|
-
const normalizedKey =
|
|
919
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
920
|
-
throw new Error("No file key provided");
|
|
921
|
-
}
|
|
1012
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1013
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
922
1014
|
const command = new HeadObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
923
1015
|
yield this.execute(command);
|
|
924
1016
|
return true;
|
|
@@ -932,10 +1024,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
932
1024
|
}
|
|
933
1025
|
fileContent(filePath, format = "buffer") {
|
|
934
1026
|
return __async(this, null, function* () {
|
|
935
|
-
const normalizedKey =
|
|
936
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
937
|
-
throw new Error("No file key provided");
|
|
938
|
-
}
|
|
1027
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1028
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
939
1029
|
const command = new GetObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
940
1030
|
const result = yield this.execute(command);
|
|
941
1031
|
if (!result.Body) {
|
|
@@ -967,10 +1057,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
967
1057
|
}
|
|
968
1058
|
uploadFile(_0, _1) {
|
|
969
1059
|
return __async(this, arguments, function* (filePath, fileData, acl = "private" /* private */, version = "1.0.0") {
|
|
970
|
-
const normalizedKey =
|
|
971
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
972
|
-
throw new Error("No file key provided");
|
|
973
|
-
}
|
|
1060
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1061
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
974
1062
|
const upload = new Upload({
|
|
975
1063
|
client: this.s3Client,
|
|
976
1064
|
params: {
|
|
@@ -992,10 +1080,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
992
1080
|
}
|
|
993
1081
|
deleteFile(filePath) {
|
|
994
1082
|
return __async(this, null, function* () {
|
|
995
|
-
const normalizedKey =
|
|
996
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
997
|
-
throw new Error("No file key provided");
|
|
998
|
-
}
|
|
1083
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1084
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
999
1085
|
const command = new DeleteObjectCommand({ Bucket: this.bucket, Key: normalizedKey });
|
|
1000
1086
|
return yield this.execute(command);
|
|
1001
1087
|
});
|
|
@@ -1007,10 +1093,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1007
1093
|
checkFileExists = true,
|
|
1008
1094
|
abortSignal
|
|
1009
1095
|
} = {}) {
|
|
1010
|
-
const normalizedKey =
|
|
1011
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
1012
|
-
throw new Error("No file key provided");
|
|
1013
|
-
}
|
|
1096
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1097
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
1014
1098
|
if (checkFileExists) {
|
|
1015
1099
|
const isExists = yield this.fileExists(normalizedKey);
|
|
1016
1100
|
if (!isExists) return null;
|
|
@@ -1033,10 +1117,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1033
1117
|
abortSignal
|
|
1034
1118
|
}) {
|
|
1035
1119
|
var _a2;
|
|
1036
|
-
const normalizedKey =
|
|
1037
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
1038
|
-
throw new Error("No file key provided");
|
|
1039
|
-
}
|
|
1120
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1121
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
1040
1122
|
try {
|
|
1041
1123
|
const cmd = new GetObjectCommand(__spreadValues({
|
|
1042
1124
|
Bucket: this.bucket,
|
|
@@ -1075,9 +1157,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1075
1157
|
}) {
|
|
1076
1158
|
return (req, res, next) => __async(this, null, function* () {
|
|
1077
1159
|
var _a2, _b, _c, _d, _e;
|
|
1078
|
-
const filePaths = [].concat(filePath).map((filePath2) =>
|
|
1079
|
-
return decodeURIComponent((filePath2 == null ? void 0 : filePath2.replace(/^\//, "").replace(/\/$/, "")) || "");
|
|
1080
|
-
}).filter((v) => v && v !== "/");
|
|
1160
|
+
const filePaths = [].concat(filePath).map((filePath2) => getNormalizedPath(filePath2)).filter((v) => v && v !== "/");
|
|
1081
1161
|
if (!filePaths.length) {
|
|
1082
1162
|
throw new Error("No file keys provided");
|
|
1083
1163
|
}
|
|
@@ -1218,10 +1298,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1218
1298
|
(_a3 = stream == null ? void 0 : stream.destroy) == null ? void 0 : _a3.call(stream);
|
|
1219
1299
|
};
|
|
1220
1300
|
req.once("close", onClose);
|
|
1221
|
-
const normalizedKey =
|
|
1222
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
1223
|
-
throw new Error("No file key provided");
|
|
1224
|
-
}
|
|
1301
|
+
const normalizedKey = getNormalizedPath(filePath);
|
|
1302
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
1225
1303
|
try {
|
|
1226
1304
|
const isExists = yield this.fileExists(normalizedKey);
|
|
1227
1305
|
if (!isExists) {
|
|
@@ -1292,10 +1370,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1292
1370
|
}) {
|
|
1293
1371
|
return (req, res, next) => __async(this, null, function* () {
|
|
1294
1372
|
var _a2, _b, _c, _d, _e, _f;
|
|
1295
|
-
const normalizedKey =
|
|
1296
|
-
if (!normalizedKey || normalizedKey === "/")
|
|
1297
|
-
throw new Error("No file key provided");
|
|
1298
|
-
}
|
|
1373
|
+
const normalizedKey = getNormalizedPath(fileKey);
|
|
1374
|
+
if (!normalizedKey || normalizedKey === "/") throw new Error("No file key provided");
|
|
1299
1375
|
const isExists = yield this.fileExists(normalizedKey);
|
|
1300
1376
|
const fileSize = yield this.sizeOf(normalizedKey);
|
|
1301
1377
|
let Range;
|
|
@@ -1423,7 +1499,7 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1423
1499
|
}
|
|
1424
1500
|
return fileSize;
|
|
1425
1501
|
}
|
|
1426
|
-
getUploadFileMW(
|
|
1502
|
+
getUploadFileMW(directoryPath, {
|
|
1427
1503
|
acl = "private" /* private */,
|
|
1428
1504
|
maxFileSize,
|
|
1429
1505
|
filename: _filename,
|
|
@@ -1431,8 +1507,8 @@ var S3BucketUtil = class _S3BucketUtil {
|
|
|
1431
1507
|
fileExt = [],
|
|
1432
1508
|
metadata: customMetadata
|
|
1433
1509
|
} = {}) {
|
|
1434
|
-
let normalizedPath =
|
|
1435
|
-
if (
|
|
1510
|
+
let normalizedPath = getNormalizedPath(directoryPath);
|
|
1511
|
+
if (normalizedPath !== "/" && directoryPath !== "" && directoryPath !== void 0) normalizedPath += "/";
|
|
1436
1512
|
else normalizedPath = "";
|
|
1437
1513
|
const fileSize = this.getFileSize(maxFileSize);
|
|
1438
1514
|
const fileTypes = [].concat(fileType);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hdriel/aws-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Simplified AWS SDK (v3) utilities for S3 (upload, download, streaming) with TypeScript support",
|
|
5
5
|
"author": "Hadriel Benjo (https://github.com/hdriel)",
|
|
6
6
|
"type": "module",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"build:watch": "tsc --watch",
|
|
32
32
|
"format": "prettier --write .",
|
|
33
33
|
"clean": "rimraf ./dist",
|
|
34
|
-
"yalc:publish": "pnpm run build && yalc publish --private --push && cd demo && npm run yalc:attach",
|
|
34
|
+
"yalc:publish": "pnpm run build && yalc publish --private --push && cd demo && pnpm add @hdriel/aws-utils && cd ../demo && npm run yalc:attach",
|
|
35
35
|
"docker:localstack:restart": "cross-env NODE_ENV=localhost && docker-compose -f docker-compose.localstack.yml --env-file .env.local up -d --build",
|
|
36
36
|
"docker:localstack:down": "docker-compose -f docker-compose.localstack.yml down"
|
|
37
37
|
},
|