1 | import { assertValidationError } from '../../../errors/utils/assertValidationError.mjs';
|
2 | import { StorageValidationErrorCode } from '../../../errors/types/validation.mjs';
|
3 | import { resolvePrefix } from '../../../utils/resolvePrefix.mjs';
|
4 | import { DEFAULT_ACCESS_LEVEL, LOCAL_TESTING_S3_ENDPOINT } from './constants.mjs';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const resolveS3ConfigAndInput = async (amplify, apiOptions) => {
|
20 | |
21 |
|
22 |
|
23 |
|
24 | const { identityId } = await amplify.Auth.fetchAuthSession();
|
25 | assertValidationError(!!identityId, StorageValidationErrorCode.NoIdentityId);
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | const credentialsProvider = async () => {
|
33 | const { credentials } = await amplify.Auth.fetchAuthSession();
|
34 | assertValidationError(!!credentials, StorageValidationErrorCode.NoCredentials);
|
35 | return credentials;
|
36 | };
|
37 | const { bucket: defaultBucket, region: defaultRegion, dangerouslyConnectToHttpEndpointForTesting, buckets, } = amplify.getConfig()?.Storage?.S3 ?? {};
|
38 | const { bucket = defaultBucket, region = defaultRegion } = (apiOptions?.bucket && resolveBucketConfig(apiOptions, buckets)) || {};
|
39 | assertValidationError(!!bucket, StorageValidationErrorCode.NoBucket);
|
40 | assertValidationError(!!region, StorageValidationErrorCode.NoRegion);
|
41 | const { defaultAccessLevel, prefixResolver = resolvePrefix, isObjectLockEnabled, } = amplify.libraryOptions?.Storage?.S3 ?? {};
|
42 | const keyPrefix = await prefixResolver({
|
43 | accessLevel: apiOptions?.accessLevel ?? defaultAccessLevel ?? DEFAULT_ACCESS_LEVEL,
|
44 |
|
45 | targetIdentityId: apiOptions?.accessLevel === 'protected'
|
46 | ? (apiOptions?.targetIdentityId ?? identityId)
|
47 | : identityId,
|
48 | });
|
49 | return {
|
50 | s3Config: {
|
51 | credentials: credentialsProvider,
|
52 | region,
|
53 | useAccelerateEndpoint: apiOptions?.useAccelerateEndpoint,
|
54 | ...(dangerouslyConnectToHttpEndpointForTesting
|
55 | ? {
|
56 | customEndpoint: LOCAL_TESTING_S3_ENDPOINT,
|
57 | forcePathStyle: true,
|
58 | }
|
59 | : {}),
|
60 | },
|
61 | bucket,
|
62 | keyPrefix,
|
63 | identityId,
|
64 | isObjectLockEnabled,
|
65 | };
|
66 | };
|
67 | const resolveBucketConfig = (apiOptions, buckets) => {
|
68 | if (typeof apiOptions.bucket === 'string') {
|
69 | const bucketConfig = buckets?.[apiOptions.bucket];
|
70 | assertValidationError(!!bucketConfig, StorageValidationErrorCode.InvalidStorageBucket);
|
71 | return { bucket: bucketConfig.bucketName, region: bucketConfig.region };
|
72 | }
|
73 | if (typeof apiOptions.bucket === 'object') {
|
74 | return {
|
75 | bucket: apiOptions.bucket.bucketName,
|
76 | region: apiOptions.bucket.region,
|
77 | };
|
78 | }
|
79 | };
|
80 |
|
81 | export { resolveS3ConfigAndInput };
|
82 |
|