UNPKG

@aws-amplify/storage

Version:

Storage category of aws-amplify

68 lines (66 loc) 2.92 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.remove = remove; const utils_1 = require("@aws-amplify/core/internals/utils"); const utils_2 = require("../../utils"); const s3data_1 = require("../../utils/client/s3data"); const userAgent_1 = require("../../utils/userAgent"); const constants_1 = require("../../utils/constants"); const createAbortableTask_1 = require("../../utils/createAbortableTask"); const CanceledError_1 = require("../../../../errors/CanceledError"); function remove(amplify, input) { return (0, createAbortableTask_1.createAbortableTask)(executeRemove(amplify, input)); } const executeRemove = (amplify, input) => async (abortController) => { try { const { s3Config, keyPrefix, bucket, identityId } = await (0, utils_2.resolveS3ConfigAndInput)(amplify, input); const { inputType, objectKey } = (0, utils_2.validateStorageOperationInput)(input, identityId); (0, utils_2.validateBucketOwnerID)(input.options?.expectedBucketOwner); const finalKey = (0, utils_2.resolveFinalKey)(inputType, objectKey, keyPrefix); (0, utils_2.validateRemovePath)(finalKey); const isFolder = finalKey.endsWith('/') || (await (0, utils_2.isPathFolder)({ s3Config, bucket, key: finalKey, expectedBucketOwner: input.options?.expectedBucketOwner, })); if (isFolder) { return (0, utils_2.deleteFolderContents)({ s3Config, bucket, folderKey: finalKey, expectedBucketOwner: input.options?.expectedBucketOwner, onProgress: input.options?.onProgress, abortSignal: abortController.signal, }); } else { if (abortController.signal.aborted) { throw new CanceledError_1.CanceledError({ message: 'Operation was cancelled' }); } await (0, s3data_1.deleteObject)({ ...s3Config, userAgentValue: (0, userAgent_1.getStorageUserAgentValue)(utils_1.StorageAction.Remove), abortSignal: abortController.signal, }, { Bucket: bucket, Key: finalKey, ExpectedBucketOwner: input.options?.expectedBucketOwner, }); const result = inputType === constants_1.STORAGE_INPUT_KEY ? { key: objectKey } : { path: objectKey }; return result; } } catch (error) { if (abortController.signal.aborted) { throw new CanceledError_1.CanceledError({ message: 'Operation was cancelled' }); } throw error; } }; //# sourceMappingURL=remove.js.map