UNPKG

@aws-amplify/storage

Version:

Storage category of aws-amplify

1 lines 11.4 kB
{"version":3,"file":"copy.mjs","sources":["../../../../../../src/providers/s3/apis/internal/copy.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { StorageAction } from '@aws-amplify/core/internals/utils';\nimport { isInputWithPath, resolveS3ConfigAndInput, validateBucketOwnerID, validateStorageOperationInput, } from '../../utils';\nimport { StorageValidationErrorCode } from '../../../../errors/types/validation';\nimport { assertValidationError } from '../../../../errors/utils/assertValidationError';\nimport { copyObject } from '../../utils/client/s3data';\nimport { getStorageUserAgentValue } from '../../utils/userAgent';\nimport { logger } from '../../../../utils';\nconst isCopyInputWithPath = (input) => isInputWithPath(input.source);\nconst storageBucketAssertion = (sourceBucket, destBucket) => {\n /** For multi-bucket, both source and destination bucket needs to be passed in\n * or both can be undefined and we fallback to singleton's default value\n */\n assertValidationError(\n // Both src & dest bucket option is present is acceptable\n (sourceBucket !== undefined && destBucket !== undefined) ||\n // or both are undefined is also acceptable\n (!destBucket && !sourceBucket), StorageValidationErrorCode.InvalidCopyOperationStorageBucket);\n};\nexport const copy = async (amplify, input) => {\n return isCopyInputWithPath(input)\n ? copyWithPath(amplify, input)\n : copyWithKey(amplify, input);\n};\nconst copyWithPath = async (amplify, input) => {\n const { source, destination } = input;\n storageBucketAssertion(source.bucket, destination.bucket);\n const { bucket: sourceBucket } = await resolveS3ConfigAndInput(amplify, {\n path: input.source.path,\n options: {\n locationCredentialsProvider: input.options?.locationCredentialsProvider,\n ...input.source,\n },\n });\n // The bucket, region, credentials of s3 client are resolved from destination.\n // Whereas the source bucket and path are a input parameter of S3 copy operation.\n const { s3Config, bucket: destBucket, identityId, } = await resolveS3ConfigAndInput(amplify, {\n path: input.destination.path,\n options: {\n locationCredentialsProvider: input.options?.locationCredentialsProvider,\n customEndpoint: input.options?.customEndpoint,\n ...input.destination,\n },\n }); // resolveS3ConfigAndInput does not make extra API calls or storage access if called repeatedly.\n assertValidationError(!!source.path, StorageValidationErrorCode.NoSourcePath);\n assertValidationError(!!destination.path, StorageValidationErrorCode.NoDestinationPath);\n const { objectKey: sourcePath } = validateStorageOperationInput(source, identityId);\n const { objectKey: destinationPath } = validateStorageOperationInput(destination, identityId);\n validateBucketOwnerID(source.expectedBucketOwner);\n validateBucketOwnerID(destination.expectedBucketOwner);\n const finalCopySource = `${sourceBucket}/${sourcePath}`;\n const finalCopyDestination = destinationPath;\n logger.debug(`copying \"${finalCopySource}\" to \"${finalCopyDestination}\".`);\n await serviceCopy({\n source: finalCopySource,\n destination: finalCopyDestination,\n bucket: destBucket,\n s3Config,\n notModifiedSince: input.source.notModifiedSince,\n eTag: input.source.eTag,\n expectedSourceBucketOwner: input.source?.expectedBucketOwner,\n expectedBucketOwner: input.destination?.expectedBucketOwner,\n });\n return { path: finalCopyDestination };\n};\n/** @deprecated Use {@link copyWithPath} instead. */\nexport const copyWithKey = async (amplify, input) => {\n const { source, destination } = input;\n storageBucketAssertion(source.bucket, destination.bucket);\n assertValidationError(!!source.key, StorageValidationErrorCode.NoSourceKey);\n assertValidationError(!!destination.key, StorageValidationErrorCode.NoDestinationKey);\n validateBucketOwnerID(source.expectedBucketOwner);\n validateBucketOwnerID(destination.expectedBucketOwner);\n const { bucket: sourceBucket, keyPrefix: sourceKeyPrefix } = await resolveS3ConfigAndInput(amplify, {\n ...input,\n options: {\n // @ts-expect-error: 'options' does not exist on type 'CopyInput'. In case of JS users set the location\n // credentials provider option, resolveS3ConfigAndInput will throw validation error.\n locationCredentialsProvider: input.options?.locationCredentialsProvider,\n ...input.source,\n },\n });\n // The bucket, region, credentials of s3 client are resolved from destination.\n // Whereas the source bucket and path are a input parameter of S3 copy operation.\n const { s3Config, bucket: destBucket, keyPrefix: destinationKeyPrefix, } = await resolveS3ConfigAndInput(amplify, {\n ...input,\n options: {\n // @ts-expect-error: 'options' does not exist on type 'CopyInput'. In case of JS users set the location\n // credentials provider option, resolveS3ConfigAndInput will throw validation error.\n locationCredentialsProvider: input.options?.locationCredentialsProvider,\n ...input.destination,\n },\n }); // resolveS3ConfigAndInput does not make extra API calls or storage access if called repeatedly.\n // TODO(ashwinkumar6) V6-logger: warn `You may copy files from another user if the source level is \"protected\", currently it's ${srcLevel}`\n const finalCopySource = `${sourceBucket}/${sourceKeyPrefix}${source.key}`;\n const finalCopyDestination = `${destinationKeyPrefix}${destination.key}`;\n logger.debug(`copying \"${finalCopySource}\" to \"${finalCopyDestination}\".`);\n await serviceCopy({\n source: finalCopySource,\n destination: finalCopyDestination,\n bucket: destBucket,\n s3Config,\n notModifiedSince: input.source.notModifiedSince,\n eTag: input.source.eTag,\n expectedSourceBucketOwner: input.source?.expectedBucketOwner,\n expectedBucketOwner: input.destination?.expectedBucketOwner,\n });\n return {\n key: destination.key,\n };\n};\nconst serviceCopy = async ({ source, destination, bucket, s3Config, notModifiedSince, eTag, expectedSourceBucketOwner, expectedBucketOwner, }) => {\n await copyObject({\n ...s3Config,\n userAgentValue: getStorageUserAgentValue(StorageAction.Copy),\n }, {\n Bucket: bucket,\n CopySource: source,\n Key: destination,\n MetadataDirective: 'COPY', // Copies over metadata like contentType as well\n CopySourceIfMatch: eTag,\n CopySourceIfUnmodifiedSince: notModifiedSince,\n ExpectedSourceBucketOwner: expectedSourceBucketOwner,\n ExpectedBucketOwner: expectedBucketOwner,\n });\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AACA;AAQA,MAAM,mBAAmB,GAAG,CAAC,KAAK,KAAK,eAAe,CAAC,KAAK,CAAC,MAAM,CAAC;AACpE,MAAM,sBAAsB,GAAG,CAAC,YAAY,EAAE,UAAU,KAAK;AAC7D;AACA;AACA;AACA,IAAI,qBAAqB;AACzB;AACA,IAAI,CAAC,YAAY,KAAK,SAAS,IAAI,UAAU,KAAK,SAAS;AAC3D;AACA,SAAS,CAAC,UAAU,IAAI,CAAC,YAAY,CAAC,EAAE,0BAA0B,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