-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
putObject.ts
99 lines (92 loc) · 2.31 KB
/
putObject.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
Endpoint,
HttpRequest,
HttpResponse,
parseMetadata,
} from '@aws-amplify/core/internals/aws-client-utils';
import { composeServiceApi } from '@aws-amplify/core/internals/aws-client-utils/composers';
import { defaultConfig } from './base';
import type { PutObjectCommandInput, PutObjectCommandOutput } from './types';
import {
assignStringVariables,
map,
parseXmlError,
s3TransferHandler,
serializeObjectConfigsToHeaders,
serializePathnameObjectKey,
} from './utils';
import type { S3ProviderPutConfig } from '../../types';
/**
* Reference: {@link S3ProviderPutConfig}
*/
export type PutObjectInput = Pick<
PutObjectCommandInput,
| 'Bucket'
| 'Key'
| 'Body'
| 'ServerSideEncryption'
| 'SSECustomerAlgorithm'
| 'SSECustomerKey'
// TODO(AllanZhengYP): remove in V6.
| 'SSECustomerKeyMD5'
| 'SSEKMSKeyId'
| 'ACL'
| 'CacheControl'
| 'ContentDisposition'
| 'ContentEncoding'
| 'ContentType'
| 'ContentMD5'
| 'Expires'
| 'Metadata'
| 'Tagging'
>;
export type PutObjectOutput = Pick<
PutObjectCommandOutput,
// PutObject output is not exposed in public API, but only logged in the debug mode
// so we only expose $metadata, ETag and VersionId for debug purpose.
'$metadata' | 'ETag' | 'VersionId'
>;
const putObjectSerializer = async (
input: PutObjectInput,
endpoint: Endpoint
): Promise<HttpRequest> => {
const headers = {
...(await serializeObjectConfigsToHeaders({
...input,
ContentType: input.ContentType ?? 'application/octet-stream',
})),
...assignStringVariables({ 'content-md5': input.ContentMD5 }),
};
const url = new URL(endpoint.url.toString());
url.pathname = serializePathnameObjectKey(url, input.Key);
return {
method: 'PUT',
headers,
url,
body: input.Body,
};
};
const putObjectDeserializer = async (
response: HttpResponse
): Promise<PutObjectOutput> => {
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
throw error;
} else {
return {
...map(response.headers, {
ETag: 'etag',
VersionId: 'x-amz-version-id',
}),
$metadata: parseMetadata(response),
};
}
};
export const putObject = composeServiceApi(
s3TransferHandler,
putObjectSerializer,
putObjectDeserializer,
{ ...defaultConfig, responseType: 'text' }
);