Skip to content

Commit

Permalink
fix(s3): address feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Jun 19, 2023
1 parent e287dfa commit 202b62a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/storage/__tests__/AwsClients/S3/cases/headObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const headObjectHappyCase: ApiFunctionalTestCase<typeof headObject> = [
'content-type': 'text/plain',
etag: 'etag',
'last-modified': 'Sun, 1 Jan 2006 12:00:00 GMT',
'x-amz-version-id': 'versionId',
},
body: '',
},
Expand All @@ -50,6 +51,7 @@ const headObjectHappyCase: ApiFunctionalTestCase<typeof headObject> = [
ContentType: 'text/plain',
ETag: 'etag',
LastModified: new Date('Sun, 1 Jan 2006 12:00:00 GMT'),
VersionId: 'versionId',
},
];

Expand Down
4 changes: 3 additions & 1 deletion packages/storage/src/AwsClients/S3/getObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const getObjectDeserializer = async (
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
throw error;
} else if (!response.body) {
throw new Error('Got empty response body.');
} else {
return {
...map(response.headers, {
Expand Down Expand Up @@ -113,7 +115,7 @@ const getObjectDeserializer = async (
}),
Metadata: deserializeMetadata(response.headers),
$metadata: parseMetadata(response),
Body: response.body!,
Body: response.body,
};
}
};
Expand Down
2 changes: 2 additions & 0 deletions packages/storage/src/AwsClients/S3/headObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type HeadObjectOutput = Pick<
| 'ETag'
| 'LastModified'
| 'Metadata'
| 'VersionId'
>;

const headObjectSerializer = (
Expand Down Expand Up @@ -67,6 +68,7 @@ const headObjectDeserializer = async (
ContentType: 'content-type',
ETag: 'etag',
LastModified: ['last-modified', deserializeTimestamp],
VersionId: 'x-amz-version-id',
}),
Metadata: deserializeMetadata(response.headers),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ export const emptyArrayGuard = <T extends Array<any>>(
export const deserializeMetadata = (
headers: Headers
): Record<string, string> => {
const objectMetadataHeaderPrefix = 'x-amz-meta-';
const deserialized = Object.keys(headers)
.filter(header => header.startsWith('x-amz-meta-'))
.filter(header => header.startsWith(objectMetadataHeaderPrefix))
.reduce((acc, header) => {
acc[header.substring(11)] = headers[header];
acc[header.replace(objectMetadataHeaderPrefix, '')] = headers[header];
return acc;
}, {} as any);
return Object.keys(deserialized).length > 0 ? deserialized : undefined;
Expand Down

0 comments on commit 202b62a

Please sign in to comment.