UNPKG

@aws-amplify/storage

Version:

Storage category of aws-amplify

75 lines (72 loc) 3.06 kB
import { parseMetadata } from '@aws-amplify/core/internals/aws-client-utils'; import { AmplifyUrl, AmplifyUrlSearchParams } from '@aws-amplify/core/internals/utils'; import { composeServiceApi } from '@aws-amplify/core/internals/aws-client-utils/composers'; import { s3TransferHandler } from '../runtime/s3TransferHandler/fetch.mjs'; import 'fast-xml-parser'; import '../runtime/s3TransferHandler/xhr.mjs'; import 'buffer'; import { buildStorageServiceError } from '../utils/deserializeHelpers.mjs'; import { validateS3RequiredParameter, assignStringVariables } from '../utils/serializeHelpers.mjs'; import { generateDeleteObjectsXml } from '../../generateDeleteObjectsXml.mjs'; import { calculateContentMd5 } from '../../md5.mjs'; import { defaultConfig, parseXmlError } from './base.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 const deleteObjectsSerializer = async (input, endpoint) => { const url = new AmplifyUrl(endpoint.url.toString()); validateS3RequiredParameter(!!input.Delete, 'Delete'); url.pathname = '/'; url.search = new AmplifyUrlSearchParams({ delete: '', }).toString(); const body = generateDeleteObjectsXml(input.Delete.Objects, input.Delete.Quiet ?? false); const contentMd5 = input.ContentMD5 ?? (await calculateContentMd5(body)); const headers = assignStringVariables({ 'x-amz-expected-bucket-owner': input.ExpectedBucketOwner, 'content-type': 'application/xml', 'content-md5': contentMd5, }); const request = { method: 'POST', headers, url, body, }; return request; }; const deleteObjectsDeserializer = async (response) => { if (response.statusCode >= 300) { const error = await parseXmlError(response); throw buildStorageServiceError(error); } const text = await response.body.text(); const deleted = []; const errors = []; const deletedMatches = text.matchAll(/<Deleted>[\s\S]*?<\/Deleted>/g); for (const match of deletedMatches) { const keyMatch = match[0].match(/<Key>(.*?)<\/Key>/); if (keyMatch) { deleted.push({ Key: keyMatch[1] }); } } const errorMatches = text.matchAll(/<Error>[\s\S]*?<\/Error>/g); for (const match of errorMatches) { const keyMatch = match[0].match(/<Key>(.*?)<\/Key>/); const codeMatch = match[0].match(/<Code>(.*?)<\/Code>/); const messageMatch = match[0].match(/<Message>(.*?)<\/Message>/); errors.push({ Key: keyMatch?.[1], Code: codeMatch?.[1], Message: messageMatch?.[1], }); } const result = { Deleted: deleted, Errors: errors, $metadata: parseMetadata(response), }; return result; }; const deleteObjects = composeServiceApi(s3TransferHandler, deleteObjectsSerializer, deleteObjectsDeserializer, { ...defaultConfig, responseType: 'text' }); export { deleteObjects }; //# sourceMappingURL=deleteObjects.mjs.map