@aws-amplify/storage
Version:
Storage category of aws-amplify
1 lines • 9.81 kB
Source Map (JSON)
{"version":3,"file":"resolveS3ConfigAndInput.mjs","sources":["../../../../../src/providers/s3/utils/resolveS3ConfigAndInput.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { assertValidationError } from '../../../errors/utils/assertValidationError';\nimport { StorageValidationErrorCode } from '../../../errors/types/validation';\nimport { resolvePrefix as defaultPrefixResolver } from '../../../utils/resolvePrefix';\nimport { StorageError } from '../../../errors/StorageError';\nimport { INVALID_STORAGE_INPUT } from '../../../errors/constants';\nimport { DEFAULT_ACCESS_LEVEL, LOCAL_TESTING_S3_ENDPOINT } from './constants';\n/**\n * resolve the common input options for S3 API handlers from Amplify configuration and library options.\n *\n * @param {AmplifyClassV6} amplify The Amplify instance.\n * @param {S3ApiOptions} apiOptions The input options for S3 provider.\n * @returns {Promise<ResolvedS3ConfigAndInput>} The resolved common input options for S3 API handlers.\n * @throws A `StorageError` with `error.name` from `StorageValidationErrorCode` indicating invalid\n * configurations or Amplify library options.\n *\n * @internal\n */\nexport const resolveS3ConfigAndInput = async (amplify, apiInput) => {\n const { options: apiOptions } = apiInput ?? {};\n /**\n * IdentityId is always cached in memory so we can safely make calls here. It\n * should be stable even for unauthenticated users, regardless of credentials.\n */\n const { identityId } = await amplify.Auth.fetchAuthSession();\n /**\n * A credentials provider function instead of a static credentials object is\n * used because the long-running tasks like multipart upload may span over the\n * credentials expiry. Auth.fetchAuthSession() automatically refreshes the\n * credentials if they are expired.\n *\n * The optional forceRefresh option is set when the S3 service returns expired\n * tokens error in the previous API call attempt.\n */\n const credentialsProvider = async (options) => {\n if (isLocationCredentialsProvider(apiOptions)) {\n assertStorageInput(apiInput);\n }\n // TODO: forceRefresh option of fetchAuthSession would refresh both tokens and\n // AWS credentials. So we do not support forceRefreshing from the Auth until\n // we support refreshing only the credentials.\n const { credentials } = isLocationCredentialsProvider(apiOptions)\n ? await apiOptions.locationCredentialsProvider(options)\n : await amplify.Auth.fetchAuthSession();\n assertValidationError(!!credentials, StorageValidationErrorCode.NoCredentials);\n return credentials;\n };\n const { bucket: defaultBucket, region: defaultRegion, dangerouslyConnectToHttpEndpointForTesting, buckets, } = amplify.getConfig()?.Storage?.S3 ?? {};\n const { bucket = defaultBucket, region = defaultRegion } = (apiOptions?.bucket && resolveBucketConfig(apiOptions, buckets)) || {};\n assertValidationError(!!bucket, StorageValidationErrorCode.NoBucket);\n assertValidationError(!!region, StorageValidationErrorCode.NoRegion);\n const { defaultAccessLevel, prefixResolver = defaultPrefixResolver, isObjectLockEnabled, } = amplify.libraryOptions?.Storage?.S3 ?? {};\n const accessLevel = apiOptions?.accessLevel ?? defaultAccessLevel ?? DEFAULT_ACCESS_LEVEL;\n const targetIdentityId = accessLevel === 'protected'\n ? (apiOptions?.targetIdentityId ?? identityId)\n : identityId;\n const keyPrefix = await prefixResolver({ accessLevel, targetIdentityId });\n return {\n s3Config: {\n credentials: credentialsProvider,\n region,\n useAccelerateEndpoint: apiOptions?.useAccelerateEndpoint,\n ...(apiOptions?.customEndpoint\n ? { customEndpoint: apiOptions.customEndpoint }\n : {}),\n ...(dangerouslyConnectToHttpEndpointForTesting\n ? {\n customEndpoint: LOCAL_TESTING_S3_ENDPOINT,\n forcePathStyle: true,\n }\n : {}),\n },\n bucket,\n keyPrefix,\n identityId,\n isObjectLockEnabled,\n };\n};\nconst isLocationCredentialsProvider = (options) => {\n return !!options?.locationCredentialsProvider;\n};\nconst isInputWithCallbackPath = (input) => {\n return ((input?.path &&\n typeof input.path === 'function') ||\n (input?.destination?.path &&\n typeof input.destination?.path === 'function') ||\n (input?.source?.path &&\n typeof input.source?.path === 'function'));\n};\nconst isDeprecatedInput = (input) => {\n return (isInputWithKey(input) ||\n isInputWithPrefix(input) ||\n isInputWithCopySourceOrDestination(input));\n};\nconst assertStorageInput = (input) => {\n if (isDeprecatedInput(input) || isInputWithCallbackPath(input)) {\n throw new StorageError({\n name: INVALID_STORAGE_INPUT,\n message: 'The input needs to have a path as a string value.',\n recoverySuggestion: 'Please provide a valid path as a string value for the input.',\n });\n }\n};\nconst isInputWithKey = (input) => {\n return !!(typeof input.key === 'string');\n};\nconst isInputWithPrefix = (input) => {\n return !!(typeof input.prefix === 'string');\n};\nconst isInputWithCopySourceOrDestination = (input) => {\n return !!(typeof input.source?.key === 'string' ||\n typeof input.destination?.key === 'string');\n};\nconst resolveBucketConfig = (apiOptions, buckets) => {\n if (typeof apiOptions.bucket === 'string') {\n const bucketConfig = buckets?.[apiOptions.bucket];\n assertValidationError(!!bucketConfig, StorageValidationErrorCode.InvalidStorageBucket);\n return { bucket: bucketConfig.bucketName, region: bucketConfig.region };\n }\n if (typeof apiOptions.bucket === 'object') {\n return {\n bucket: apiOptions.bucket.bucketName,\n region: apiOptions.bucket.region,\n };\n }\n};\n"],"names":["defaultPrefixResolver"],"mappings":";;;;;;;AAAA;AACA;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,uBAAuB,GAAG,OAAO,OAAO,EAAE,QAAQ,KAAK;AACpE,IAAI,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,QAAQ,IAAI,EAAE;AAClD;AACA;AACA;AACA;AACA,IAAI,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE;AAChE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,MAAM,mBAAmB,GAAG,OAAO,OAAO,KAAK;AACnD,QAAQ,IAAI,6BAA6B,CAAC,UAAU,CAAC,EAAE;AACvD,YAAY,kBAAkB,CAAC,QAAQ,CAAC;AACxC;AACA;AACA;AACA;AACA,QAAQ,MAAM,EAAE,WAAW,EAAE,GAAG,6BAA6B,CAAC,UAAU;AACxE,cAAc,MAAM,UAAU,CAAC,2BAA2B,CAAC,OAAO;AAClE,cAAc,MAAM,OAAO,CA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