@nextlyhq/storage-s3 0.0.2-alpha.73 → 0.0.2-alpha.74
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 +156 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +35 -3
- package/dist/index.d.ts +35 -3
- package/dist/index.mjs +156 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -553,8 +553,8 @@ interface ResolvedS3Config {
|
|
|
553
553
|
* @param config - S3 storage configuration
|
|
554
554
|
* @returns A StoragePlugin that MediaStorage can register
|
|
555
555
|
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
556
|
+
* Constructing the plugin never requires the bucket or region: a missing
|
|
557
|
+
* one is refused when storage is first used, not while the config loads.
|
|
558
558
|
*/
|
|
559
559
|
export declare function s3Storage(config: S3StorageConfig): StoragePlugin;
|
|
560
560
|
export declare class S3StorageAdapter implements IStorageAdapter {
|
|
@@ -565,10 +565,38 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
565
565
|
/**
|
|
566
566
|
* Create a new S3 storage adapter.
|
|
567
567
|
*
|
|
568
|
+
* The bucket and region are deliberately NOT checked here, and the SDK
|
|
569
|
+
* client is deliberately not built. This constructor runs while the host's
|
|
570
|
+
* config file is evaluated, so a throw would stop every command —
|
|
571
|
+
* including the schema-only ones that never touch storage. Both the checks
|
|
572
|
+
* and the client construction move to the first storage operation
|
|
573
|
+
* ({@link ensureClient} and {@link ensureConfigured}), which refuse with
|
|
574
|
+
* the same messages a missing bucket or region has always produced.
|
|
575
|
+
*
|
|
568
576
|
* @param config - S3 storage configuration
|
|
569
|
-
* @throws Error if bucket or region is not provided
|
|
570
577
|
*/
|
|
571
578
|
constructor(config: S3StorageConfig);
|
|
579
|
+
/**
|
|
580
|
+
* Validate that bucket and region are present.
|
|
581
|
+
*
|
|
582
|
+
* @throws Error if bucket is not provided
|
|
583
|
+
* @throws Error if region is not provided
|
|
584
|
+
*/
|
|
585
|
+
private ensureConfigured;
|
|
586
|
+
/**
|
|
587
|
+
* Validate the stored configuration and build the SDK client, at the
|
|
588
|
+
* first use that needs them.
|
|
589
|
+
*
|
|
590
|
+
* The refusals are the ones the constructor used to throw, in the same
|
|
591
|
+
* order, so an operator with a genuinely misconfigured deployment sees
|
|
592
|
+
* the message they have always seen — at the moment the value was
|
|
593
|
+
* actually needed rather than at config load. The client is built once
|
|
594
|
+
* and reused.
|
|
595
|
+
*
|
|
596
|
+
* @throws Error if bucket is not provided
|
|
597
|
+
* @throws Error if region is not provided
|
|
598
|
+
*/
|
|
599
|
+
private ensureClient;
|
|
572
600
|
/**
|
|
573
601
|
* Build AWS credentials from config.
|
|
574
602
|
* Supports explicit credentials or falls back to SDK default chain.
|
|
@@ -764,6 +792,10 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
764
792
|
/**
|
|
765
793
|
* Get the S3 client instance.
|
|
766
794
|
* Useful for advanced operations not covered by the adapter interface.
|
|
795
|
+
*
|
|
796
|
+
* Handing out the client is itself a use of the adapter, so a caller
|
|
797
|
+
* without the configuration a client needs is refused here like any
|
|
798
|
+
* other operation.
|
|
767
799
|
*/
|
|
768
800
|
getClient(): S3Client;
|
|
769
801
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -553,8 +553,8 @@ interface ResolvedS3Config {
|
|
|
553
553
|
* @param config - S3 storage configuration
|
|
554
554
|
* @returns A StoragePlugin that MediaStorage can register
|
|
555
555
|
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
556
|
+
* Constructing the plugin never requires the bucket or region: a missing
|
|
557
|
+
* one is refused when storage is first used, not while the config loads.
|
|
558
558
|
*/
|
|
559
559
|
export declare function s3Storage(config: S3StorageConfig): StoragePlugin;
|
|
560
560
|
export declare class S3StorageAdapter implements IStorageAdapter {
|
|
@@ -565,10 +565,38 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
565
565
|
/**
|
|
566
566
|
* Create a new S3 storage adapter.
|
|
567
567
|
*
|
|
568
|
+
* The bucket and region are deliberately NOT checked here, and the SDK
|
|
569
|
+
* client is deliberately not built. This constructor runs while the host's
|
|
570
|
+
* config file is evaluated, so a throw would stop every command —
|
|
571
|
+
* including the schema-only ones that never touch storage. Both the checks
|
|
572
|
+
* and the client construction move to the first storage operation
|
|
573
|
+
* ({@link ensureClient} and {@link ensureConfigured}), which refuse with
|
|
574
|
+
* the same messages a missing bucket or region has always produced.
|
|
575
|
+
*
|
|
568
576
|
* @param config - S3 storage configuration
|
|
569
|
-
* @throws Error if bucket or region is not provided
|
|
570
577
|
*/
|
|
571
578
|
constructor(config: S3StorageConfig);
|
|
579
|
+
/**
|
|
580
|
+
* Validate that bucket and region are present.
|
|
581
|
+
*
|
|
582
|
+
* @throws Error if bucket is not provided
|
|
583
|
+
* @throws Error if region is not provided
|
|
584
|
+
*/
|
|
585
|
+
private ensureConfigured;
|
|
586
|
+
/**
|
|
587
|
+
* Validate the stored configuration and build the SDK client, at the
|
|
588
|
+
* first use that needs them.
|
|
589
|
+
*
|
|
590
|
+
* The refusals are the ones the constructor used to throw, in the same
|
|
591
|
+
* order, so an operator with a genuinely misconfigured deployment sees
|
|
592
|
+
* the message they have always seen — at the moment the value was
|
|
593
|
+
* actually needed rather than at config load. The client is built once
|
|
594
|
+
* and reused.
|
|
595
|
+
*
|
|
596
|
+
* @throws Error if bucket is not provided
|
|
597
|
+
* @throws Error if region is not provided
|
|
598
|
+
*/
|
|
599
|
+
private ensureClient;
|
|
572
600
|
/**
|
|
573
601
|
* Build AWS credentials from config.
|
|
574
602
|
* Supports explicit credentials or falls back to SDK default chain.
|
|
@@ -764,6 +792,10 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
764
792
|
/**
|
|
765
793
|
* Get the S3 client instance.
|
|
766
794
|
* Useful for advanced operations not covered by the adapter interface.
|
|
795
|
+
*
|
|
796
|
+
* Handing out the client is itself a use of the adapter, so a caller
|
|
797
|
+
* without the configuration a client needs is refused here like any
|
|
798
|
+
* other operation.
|
|
767
799
|
*/
|
|
768
800
|
getClient(): S3Client;
|
|
769
801
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DeleteObjectCommand, DeleteObjectsCommand, GetObjectCommand, HeadObjectCommand, ListObjectsV2Command, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
2
2
|
import { Upload } from "@aws-sdk/lib-storage";
|
|
3
3
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
4
|
-
// ../nextly/dist/nextly-error-
|
|
4
|
+
// ../nextly/dist/nextly-error-D08zf_L_.mjs
|
|
5
5
|
function isDbError(err) {
|
|
6
6
|
if (!err || typeof err !== "object") return false;
|
|
7
7
|
const obj = err;
|
|
@@ -77,12 +77,15 @@ const NEXTLY_ERROR_STATUS = {
|
|
|
77
77
|
CSRF_FAILED: 403,
|
|
78
78
|
SETUP_TOKEN_REQUIRED: 403,
|
|
79
79
|
API_KEY_ROLE_ABOVE_CREATOR: 403,
|
|
80
|
+
ACTION_NOT_AVAILABLE: 403,
|
|
80
81
|
NOT_FOUND: 404,
|
|
81
82
|
ENTITY_REMOVED_FROM_CODE: 409,
|
|
82
83
|
ENTITY_NOT_MIGRATED: 409,
|
|
83
84
|
METHOD_NOT_ALLOWED: 405,
|
|
84
85
|
RANGE_NOT_SATISFIABLE: 416,
|
|
85
86
|
CONFLICT: 409,
|
|
87
|
+
STALE_VERSION: 409,
|
|
88
|
+
VERSION_REQUIRED: 428,
|
|
86
89
|
DUPLICATE: 409,
|
|
87
90
|
RATE_LIMITED: 429,
|
|
88
91
|
PAYLOAD_TOO_LARGE: 413,
|
|
@@ -91,6 +94,7 @@ const NEXTLY_ERROR_STATUS = {
|
|
|
91
94
|
INTERNAL_ERROR: 500,
|
|
92
95
|
DATABASE_ERROR: 500,
|
|
93
96
|
DATABASE_BUSY: 503,
|
|
97
|
+
RULE_COULD_NOT_RUN: 503,
|
|
94
98
|
EXTERNAL_SERVICE_ERROR: 502,
|
|
95
99
|
SERVICE_UNAVAILABLE: 503,
|
|
96
100
|
CONFIGURATION_ERROR: 500,
|
|
@@ -137,9 +141,11 @@ const NEXTLY_ERROR_STATUS = {
|
|
|
137
141
|
NEXTLY_PLUGIN_ADMIN_WIDGET_INVALID: 500,
|
|
138
142
|
NEXTLY_PLUGIN_REMOVED_WIDGET_CHANNEL: 500,
|
|
139
143
|
NEXTLY_PLUGIN_DOCUMENT_VIEW_INVALID: 500,
|
|
144
|
+
NEXTLY_PLUGIN_DOCUMENT_SURFACE_INVALID: 500,
|
|
140
145
|
PLUGIN_RESOLUTION_ERROR: 500,
|
|
141
146
|
PLUGIN_DEPENDENCY_NOT_INSTALLED: 409,
|
|
142
147
|
PLUGIN_HAS_DEPENDENTS: 409,
|
|
148
|
+
PLUGIN_UNINSTALL_WOULD_DROP_OTHERS: 409,
|
|
143
149
|
PLUGIN_UNINSTALL_IRREVERSIBLE: 409,
|
|
144
150
|
PLUGIN_UNINSTALL_UNCONFIRMED: 409,
|
|
145
151
|
PLUGIN_SCHEMA_BEHIND: 409,
|
|
@@ -163,6 +169,9 @@ const NEXTLY_ERROR_STATUS = {
|
|
|
163
169
|
NEXTLY_EMAIL_TRANSPORT_UNAVAILABLE: 503,
|
|
164
170
|
NEXTLY_CONFIG_TOOLING_UNAVAILABLE: 503,
|
|
165
171
|
NEXTLY_POSTGRES_SCHEMA_MISMATCH: 500,
|
|
172
|
+
NEXTLY_PER_LANGUAGE_STORAGE_CHANGED: 500,
|
|
173
|
+
NEXTLY_PER_LANGUAGE_STORAGE_MIXED: 500,
|
|
174
|
+
NEXTLY_PER_LANGUAGE_STORAGE_LANGUAGES_UNDECLARED: 500,
|
|
166
175
|
NEXTLY_POSTGRES_SCHEMA_UNSUPPORTED: 500
|
|
167
176
|
};
|
|
168
177
|
({ ...Object.fromEntries([
|
|
@@ -171,6 +180,7 @@ const NEXTLY_ERROR_STATUS = {
|
|
|
171
180
|
"FORBIDDEN",
|
|
172
181
|
"NOT_FOUND",
|
|
173
182
|
"CONFLICT",
|
|
183
|
+
"VERSION_REQUIRED",
|
|
174
184
|
"PAYLOAD_TOO_LARGE",
|
|
175
185
|
"UNSUPPORTED_MEDIA_TYPE",
|
|
176
186
|
"RATE_LIMITED",
|
|
@@ -194,6 +204,7 @@ const GENERIC_PUBLIC_MESSAGE = {
|
|
|
194
204
|
INVALID_INPUT: "The request could not be processed.",
|
|
195
205
|
AUTH_REQUIRED: "Authentication required.",
|
|
196
206
|
FORBIDDEN: "You don't have permission to perform this action.",
|
|
207
|
+
ACTION_NOT_AVAILABLE: "This action isn't available for this entry.",
|
|
197
208
|
NOT_FOUND: "Not found.",
|
|
198
209
|
METHOD_NOT_ALLOWED: "That method is not allowed on this address.",
|
|
199
210
|
RANGE_NOT_SATISFIABLE: "The requested range is outside the file.",
|
|
@@ -205,6 +216,7 @@ const GENERIC_PUBLIC_MESSAGE = {
|
|
|
205
216
|
EXTERNAL_SERVICE_ERROR: "An upstream service failed.",
|
|
206
217
|
SERVICE_UNAVAILABLE: "The service is temporarily unavailable.",
|
|
207
218
|
DATABASE_BUSY: "The database is busy. Please try again shortly.",
|
|
219
|
+
RULE_COULD_NOT_RUN: "A check this request needs could not run. Please try again shortly.",
|
|
208
220
|
INTERNAL_ERROR: "An unexpected error occurred."
|
|
209
221
|
};
|
|
210
222
|
/** The generic sentence for a code; the internal one for anything unlisted. */
|
|
@@ -412,6 +424,7 @@ var NextlyError = class NextlyError extends Error {
|
|
|
412
424
|
return new NextlyError({
|
|
413
425
|
code: "FORBIDDEN",
|
|
414
426
|
publicMessage: "You don't have permission to perform this action.",
|
|
427
|
+
publicData: opts?.publicData,
|
|
415
428
|
cause: opts?.cause,
|
|
416
429
|
logContext: opts?.logContext
|
|
417
430
|
});
|
|
@@ -613,6 +626,60 @@ var NextlyError = class NextlyError extends Error {
|
|
|
613
626
|
}
|
|
614
627
|
});
|
|
615
628
|
}
|
|
629
|
+
/**
|
|
630
|
+
* An update made from a version of the document that is no longer its
|
|
631
|
+
* current one. Never retried: the same version stays out of date, so the
|
|
632
|
+
* caller must read the document again and decide.
|
|
633
|
+
*
|
|
634
|
+
* @experimental
|
|
635
|
+
*/
|
|
636
|
+
/**
|
|
637
|
+
* An update that said neither which version of a document it was made from
|
|
638
|
+
* nor that it overwrites whatever is stored. Without `bulk`, an update of
|
|
639
|
+
* one document, which sends back the version it read. `bulk` names an
|
|
640
|
+
* update that writes the same values to many documents, which can carry no
|
|
641
|
+
* one document's version: by its ids, which can be given a version each
|
|
642
|
+
* (`keys.revs`), or by a query, which cannot. The keys are the write path's
|
|
643
|
+
* to name, so the message spells them as the request does.
|
|
644
|
+
*
|
|
645
|
+
* @experimental
|
|
646
|
+
*/
|
|
647
|
+
static versionRequired(opts) {
|
|
648
|
+
const { version, overwrite, revs } = opts.keys;
|
|
649
|
+
if (opts.bulk === void 0) {
|
|
650
|
+
const one = {
|
|
651
|
+
versionKey: version,
|
|
652
|
+
overwriteKey: overwrite
|
|
653
|
+
};
|
|
654
|
+
return new NextlyError({
|
|
655
|
+
code: "VERSION_REQUIRED",
|
|
656
|
+
publicMessage: `This update must say which version of the document it changes. Send back the \`${version}\` from your last read of it, or set \`${overwrite}: true\` to replace whatever is stored.`,
|
|
657
|
+
publicData: one,
|
|
658
|
+
logContext: opts.logContext
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
const byIds = opts.bulk === "ids";
|
|
662
|
+
const data = {
|
|
663
|
+
versionKey: version,
|
|
664
|
+
overwriteKey: overwrite,
|
|
665
|
+
...byIds ? { revsKey: revs } : {}
|
|
666
|
+
};
|
|
667
|
+
const message = `An update that writes the same values to many documents must set \`${overwrite}: true\`: it can't carry each document's version. Or update each document with its own \`${version}\`.`;
|
|
668
|
+
return new NextlyError({
|
|
669
|
+
code: "VERSION_REQUIRED",
|
|
670
|
+
publicMessage: byIds ? `${message} Or give each document's \`${version}\` in \`${revs}\`.` : message,
|
|
671
|
+
publicData: data,
|
|
672
|
+
logContext: opts.logContext
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
static staleVersion(opts) {
|
|
676
|
+
return new NextlyError({
|
|
677
|
+
code: "STALE_VERSION",
|
|
678
|
+
publicMessage: `This document changed since the version you gave. Its current version is ${opts.data.currentVersion}.`,
|
|
679
|
+
publicData: opts.data,
|
|
680
|
+
logContext: opts.logContext
|
|
681
|
+
});
|
|
682
|
+
}
|
|
616
683
|
static duplicate(opts) {
|
|
617
684
|
return new NextlyError({
|
|
618
685
|
code: "DUPLICATE",
|
|
@@ -646,6 +713,27 @@ var NextlyError = class NextlyError extends Error {
|
|
|
646
713
|
});
|
|
647
714
|
}
|
|
648
715
|
/**
|
|
716
|
+
* An access or field rule the request needs could not run, because its own
|
|
717
|
+
* database query waited out the bound for a connection. Not a refusal: the
|
|
718
|
+
* rule never answered. Names the collection or Single whose rule it is, and
|
|
719
|
+
* the field for a field's rule, and says when to retry.
|
|
720
|
+
*/
|
|
721
|
+
static ruleCouldNotRun(opts) {
|
|
722
|
+
const publicData = {
|
|
723
|
+
subject: opts.subject,
|
|
724
|
+
...opts.field !== void 0 ? { field: opts.field } : {},
|
|
725
|
+
retryAfterSeconds: 5
|
|
726
|
+
};
|
|
727
|
+
return new NextlyError({
|
|
728
|
+
code: "RULE_COULD_NOT_RUN",
|
|
729
|
+
statusCode: NEXTLY_ERROR_STATUS.RULE_COULD_NOT_RUN,
|
|
730
|
+
publicMessage: genericPublicMessage("RULE_COULD_NOT_RUN"),
|
|
731
|
+
publicData,
|
|
732
|
+
cause: opts.cause,
|
|
733
|
+
logContext: opts.logContext
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
649
737
|
* What a request boundary answers with for a thrown value that is not a
|
|
650
738
|
* NextlyError: "database busy" when the request waited out its bound for a
|
|
651
739
|
* connection, which is retryable and no fault of the server's code, and an
|
|
@@ -765,7 +853,7 @@ var NextlyError = class NextlyError extends Error {
|
|
|
765
853
|
return NextlyError.isCode(err, "RATE_LIMITED");
|
|
766
854
|
}
|
|
767
855
|
};
|
|
768
|
-
// ../nextly/dist/validate-external-url-
|
|
856
|
+
// ../nextly/dist/validate-external-url-BKFCMuwY.mjs
|
|
769
857
|
/** Default response body cap: reject anything larger before buffering it all. */
|
|
770
858
|
/**
|
|
771
859
|
* How many bytes a bounded external read may buffer, absent a caller's own cap.
|
|
@@ -933,19 +1021,24 @@ function abortAfter(options) {
|
|
|
933
1021
|
}
|
|
934
1022
|
var S3StorageAdapter = class {
|
|
935
1023
|
config;
|
|
936
|
-
client;
|
|
1024
|
+
client = null;
|
|
937
1025
|
resolvedConfig;
|
|
938
1026
|
isR2;
|
|
939
1027
|
/**
|
|
940
1028
|
* Create a new S3 storage adapter.
|
|
941
1029
|
*
|
|
1030
|
+
* The bucket and region are deliberately NOT checked here, and the SDK
|
|
1031
|
+
* client is deliberately not built. This constructor runs while the host's
|
|
1032
|
+
* config file is evaluated, so a throw would stop every command —
|
|
1033
|
+
* including the schema-only ones that never touch storage. Both the checks
|
|
1034
|
+
* and the client construction move to the first storage operation
|
|
1035
|
+
* ({@link ensureClient} and {@link ensureConfigured}), which refuse with
|
|
1036
|
+
* the same messages a missing bucket or region has always produced.
|
|
1037
|
+
*
|
|
942
1038
|
* @param config - S3 storage configuration
|
|
943
|
-
* @throws Error if bucket or region is not provided
|
|
944
1039
|
*/
|
|
945
1040
|
constructor(config) {
|
|
946
1041
|
this.config = config;
|
|
947
|
-
if (!config.bucket) throw new Error("@nextly/storage-s3: bucket is required");
|
|
948
|
-
if (!config.region) throw new Error("@nextly/storage-s3: region is required");
|
|
949
1042
|
this.isR2 = config.endpoint?.includes("r2.cloudflarestorage.com") ?? false;
|
|
950
1043
|
this.resolvedConfig = {
|
|
951
1044
|
bucket: config.bucket,
|
|
@@ -958,14 +1051,42 @@ var S3StorageAdapter = class {
|
|
|
958
1051
|
contentDisposition: config.contentDisposition,
|
|
959
1052
|
signedUrlExpiresIn: config.signedUrlExpiresIn ?? 3600
|
|
960
1053
|
};
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Validate that bucket and region are present.
|
|
1057
|
+
*
|
|
1058
|
+
* @throws Error if bucket is not provided
|
|
1059
|
+
* @throws Error if region is not provided
|
|
1060
|
+
*/
|
|
1061
|
+
ensureConfigured() {
|
|
1062
|
+
if (!this.config.bucket) throw new Error("@nextly/storage-s3: bucket is required");
|
|
1063
|
+
if (!this.config.region) throw new Error("@nextly/storage-s3: region is required");
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* Validate the stored configuration and build the SDK client, at the
|
|
1067
|
+
* first use that needs them.
|
|
1068
|
+
*
|
|
1069
|
+
* The refusals are the ones the constructor used to throw, in the same
|
|
1070
|
+
* order, so an operator with a genuinely misconfigured deployment sees
|
|
1071
|
+
* the message they have always seen — at the moment the value was
|
|
1072
|
+
* actually needed rather than at config load. The client is built once
|
|
1073
|
+
* and reused.
|
|
1074
|
+
*
|
|
1075
|
+
* @throws Error if bucket is not provided
|
|
1076
|
+
* @throws Error if region is not provided
|
|
1077
|
+
*/
|
|
1078
|
+
ensureClient() {
|
|
1079
|
+
if (this.client) return this.client;
|
|
1080
|
+
this.ensureConfigured();
|
|
961
1081
|
const credentials = this.buildCredentials();
|
|
962
1082
|
this.client = new S3Client({
|
|
963
|
-
region: config.region,
|
|
964
|
-
endpoint: config.endpoint,
|
|
1083
|
+
region: this.config.region,
|
|
1084
|
+
endpoint: this.config.endpoint,
|
|
965
1085
|
credentials,
|
|
966
1086
|
forcePathStyle: this.resolvedConfig.forcePathStyle,
|
|
967
|
-
...config.config
|
|
1087
|
+
...this.config.config
|
|
968
1088
|
});
|
|
1089
|
+
return this.client;
|
|
969
1090
|
}
|
|
970
1091
|
/**
|
|
971
1092
|
* Build AWS credentials from config.
|
|
@@ -992,6 +1113,7 @@ var S3StorageAdapter = class {
|
|
|
992
1113
|
* @returns Upload result with URL and storage path
|
|
993
1114
|
*/
|
|
994
1115
|
async upload(buffer, options) {
|
|
1116
|
+
const client = this.ensureClient();
|
|
995
1117
|
const key = this.generateKey(options.filename, options.folder);
|
|
996
1118
|
const uploadParams = {
|
|
997
1119
|
Bucket: this.resolvedConfig.bucket,
|
|
@@ -1008,7 +1130,7 @@ var S3StorageAdapter = class {
|
|
|
1008
1130
|
}
|
|
1009
1131
|
uploadParams.Metadata = { "original-filename": options.filename };
|
|
1010
1132
|
await new Upload({
|
|
1011
|
-
client
|
|
1133
|
+
client,
|
|
1012
1134
|
params: uploadParams
|
|
1013
1135
|
}).done();
|
|
1014
1136
|
return {
|
|
@@ -1022,11 +1144,12 @@ var S3StorageAdapter = class {
|
|
|
1022
1144
|
* @param filePath - Storage path/key to delete
|
|
1023
1145
|
*/
|
|
1024
1146
|
async delete(filePath) {
|
|
1147
|
+
const client = this.ensureClient();
|
|
1025
1148
|
const command = new DeleteObjectCommand({
|
|
1026
1149
|
Bucket: this.resolvedConfig.bucket,
|
|
1027
1150
|
Key: filePath
|
|
1028
1151
|
});
|
|
1029
|
-
await
|
|
1152
|
+
await client.send(command);
|
|
1030
1153
|
}
|
|
1031
1154
|
/**
|
|
1032
1155
|
* Bulk delete files from S3 using a single API call per 1000 keys.
|
|
@@ -1038,6 +1161,7 @@ var S3StorageAdapter = class {
|
|
|
1038
1161
|
* @returns Object with arrays of successful and failed deletions
|
|
1039
1162
|
*/
|
|
1040
1163
|
async bulkDelete(filePaths) {
|
|
1164
|
+
const client = this.ensureClient();
|
|
1041
1165
|
const successful = [];
|
|
1042
1166
|
const failed = [];
|
|
1043
1167
|
const maxBatchSize = 1e3;
|
|
@@ -1050,7 +1174,7 @@ var S3StorageAdapter = class {
|
|
|
1050
1174
|
Quiet: false
|
|
1051
1175
|
}
|
|
1052
1176
|
});
|
|
1053
|
-
const response = await
|
|
1177
|
+
const response = await client.send(command);
|
|
1054
1178
|
if (response.Errors && response.Errors.length > 0) {
|
|
1055
1179
|
for (const err of response.Errors) if (err.Key) failed.push({
|
|
1056
1180
|
filePath: err.Key,
|
|
@@ -1076,12 +1200,13 @@ var S3StorageAdapter = class {
|
|
|
1076
1200
|
* @returns true if file exists, false otherwise
|
|
1077
1201
|
*/
|
|
1078
1202
|
async exists(filePath) {
|
|
1203
|
+
const client = this.ensureClient();
|
|
1079
1204
|
try {
|
|
1080
1205
|
const command = new HeadObjectCommand({
|
|
1081
1206
|
Bucket: this.resolvedConfig.bucket,
|
|
1082
1207
|
Key: filePath
|
|
1083
1208
|
});
|
|
1084
|
-
await
|
|
1209
|
+
await client.send(command);
|
|
1085
1210
|
return true;
|
|
1086
1211
|
} catch (error) {
|
|
1087
1212
|
if (this.isNotFoundError(error)) return false;
|
|
@@ -1106,9 +1231,10 @@ var S3StorageAdapter = class {
|
|
|
1106
1231
|
* of up to 1,000 at a time.
|
|
1107
1232
|
*/
|
|
1108
1233
|
async *list() {
|
|
1234
|
+
const client = this.ensureClient();
|
|
1109
1235
|
let token;
|
|
1110
1236
|
do {
|
|
1111
|
-
const page = await
|
|
1237
|
+
const page = await client.send(new ListObjectsV2Command({
|
|
1112
1238
|
Bucket: this.resolvedConfig.bucket,
|
|
1113
1239
|
ContinuationToken: token
|
|
1114
1240
|
}));
|
|
@@ -1120,6 +1246,7 @@ var S3StorageAdapter = class {
|
|
|
1120
1246
|
} while (token);
|
|
1121
1247
|
}
|
|
1122
1248
|
getPublicUrl(filePath) {
|
|
1249
|
+
this.ensureConfigured();
|
|
1123
1250
|
if (this.resolvedConfig.publicUrl) return `${this.resolvedConfig.publicUrl.replace(/\/$/, "")}/${filePath}`;
|
|
1124
1251
|
if (this.isR2) throw new Error("@nextly/storage-s3: Cloudflare R2 requires publicUrl configuration.\n\nR2 does not have default public URLs like AWS S3. Configure one of:\n1. Public bucket URL: publicUrl: 'https://pub-xxx.r2.dev'\n2. Custom domain: publicUrl: 'https://cdn.example.com'\n\nSet up public access in the Cloudflare R2 dashboard.");
|
|
1125
1252
|
return `https://${this.resolvedConfig.bucket}.s3.${this.resolvedConfig.region}.amazonaws.com/${filePath}`;
|
|
@@ -1156,12 +1283,13 @@ var S3StorageAdapter = class {
|
|
|
1156
1283
|
* @returns File metadata or null if file not found
|
|
1157
1284
|
*/
|
|
1158
1285
|
async getMetadata(filePath) {
|
|
1286
|
+
const client = this.ensureClient();
|
|
1159
1287
|
try {
|
|
1160
1288
|
const command = new HeadObjectCommand({
|
|
1161
1289
|
Bucket: this.resolvedConfig.bucket,
|
|
1162
1290
|
Key: filePath
|
|
1163
1291
|
});
|
|
1164
|
-
const response = await
|
|
1292
|
+
const response = await client.send(command);
|
|
1165
1293
|
const filename = filePath.split("/").pop() || filePath;
|
|
1166
1294
|
return {
|
|
1167
1295
|
id: filePath,
|
|
@@ -1202,12 +1330,13 @@ var S3StorageAdapter = class {
|
|
|
1202
1330
|
* @returns The object's bytes, or `null` when no such key exists
|
|
1203
1331
|
*/
|
|
1204
1332
|
async read(filePath, options) {
|
|
1333
|
+
const client = this.ensureClient();
|
|
1205
1334
|
try {
|
|
1206
1335
|
const command = new GetObjectCommand({
|
|
1207
1336
|
Bucket: this.resolvedConfig.bucket,
|
|
1208
1337
|
Key: filePath
|
|
1209
1338
|
});
|
|
1210
|
-
const response = await
|
|
1339
|
+
const response = await client.send(command, abortAfter(options));
|
|
1211
1340
|
if (response.Body === void 0) return Buffer.alloc(0);
|
|
1212
1341
|
const cap = resolveReadBounds(options).maxBytes;
|
|
1213
1342
|
this.refuseOverCap(filePath, response.ContentLength, cap);
|
|
@@ -1229,11 +1358,12 @@ var S3StorageAdapter = class {
|
|
|
1229
1358
|
* @returns Pre-signed URL for downloading the file
|
|
1230
1359
|
*/
|
|
1231
1360
|
async getSignedUrl(filePath, expiresIn) {
|
|
1361
|
+
const client = this.ensureClient();
|
|
1232
1362
|
const command = new GetObjectCommand({
|
|
1233
1363
|
Bucket: this.resolvedConfig.bucket,
|
|
1234
1364
|
Key: filePath
|
|
1235
1365
|
});
|
|
1236
|
-
return getSignedUrl(
|
|
1366
|
+
return getSignedUrl(client, command, { expiresIn: expiresIn ?? this.resolvedConfig.signedUrlExpiresIn });
|
|
1237
1367
|
}
|
|
1238
1368
|
/**
|
|
1239
1369
|
* Generate pre-signed URL for client-side uploads.
|
|
@@ -1247,6 +1377,7 @@ var S3StorageAdapter = class {
|
|
|
1247
1377
|
* @returns Client upload data with URL, method, and headers
|
|
1248
1378
|
*/
|
|
1249
1379
|
async getPresignedUploadUrl(key, mimeType, expiresIn) {
|
|
1380
|
+
const client = this.ensureClient();
|
|
1250
1381
|
const expiration = expiresIn ?? this.resolvedConfig.signedUrlExpiresIn;
|
|
1251
1382
|
const commandParams = {
|
|
1252
1383
|
Bucket: this.resolvedConfig.bucket,
|
|
@@ -1257,7 +1388,7 @@ var S3StorageAdapter = class {
|
|
|
1257
1388
|
if (!this.isR2) commandParams.ACL = this.resolvedConfig.acl;
|
|
1258
1389
|
const command = new PutObjectCommand(commandParams);
|
|
1259
1390
|
return {
|
|
1260
|
-
uploadUrl: await getSignedUrl(
|
|
1391
|
+
uploadUrl: await getSignedUrl(client, command, { expiresIn: expiration }),
|
|
1261
1392
|
path: key,
|
|
1262
1393
|
method: "PUT",
|
|
1263
1394
|
headers: { "Content-Type": mimeType },
|
|
@@ -1356,9 +1487,13 @@ var S3StorageAdapter = class {
|
|
|
1356
1487
|
/**
|
|
1357
1488
|
* Get the S3 client instance.
|
|
1358
1489
|
* Useful for advanced operations not covered by the adapter interface.
|
|
1490
|
+
*
|
|
1491
|
+
* Handing out the client is itself a use of the adapter, so a caller
|
|
1492
|
+
* without the configuration a client needs is refused here like any
|
|
1493
|
+
* other operation.
|
|
1359
1494
|
*/
|
|
1360
1495
|
getClient() {
|
|
1361
|
-
return this.
|
|
1496
|
+
return this.ensureClient();
|
|
1362
1497
|
}
|
|
1363
1498
|
/**
|
|
1364
1499
|
* Get the bucket name.
|
|
@@ -1395,8 +1530,8 @@ var S3StorageAdapter = class {
|
|
|
1395
1530
|
* @param config - S3 storage configuration
|
|
1396
1531
|
* @returns A StoragePlugin that MediaStorage can register
|
|
1397
1532
|
*
|
|
1398
|
-
*
|
|
1399
|
-
*
|
|
1533
|
+
* Constructing the plugin never requires the bucket or region: a missing
|
|
1534
|
+
* one is refused when storage is first used, not while the config loads.
|
|
1400
1535
|
*/
|
|
1401
1536
|
function s3Storage(config) {
|
|
1402
1537
|
if (config.enabled === false) return {
|