forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetUrl.ts
87 lines (79 loc) · 2.88 KB
/
getUrl.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { AmplifyClassV6 } from '@aws-amplify/core';
import { StorageAction } from '@aws-amplify/core/internals/utils';
import { GetUrlInput, GetUrlOutput, GetUrlWithPathOutput } from '../../types';
import { StorageValidationErrorCode } from '../../../../errors/types/validation';
import { getPresignedGetObjectUrl } from '../../utils/client/s3data';
import {
resolveS3ConfigAndInput,
validateStorageOperationInput,
} from '../../utils';
import { assertValidationError } from '../../../../errors/utils/assertValidationError';
import {
DEFAULT_PRESIGN_EXPIRATION,
MAX_URL_EXPIRATION,
STORAGE_INPUT_KEY,
} from '../../utils/constants';
import { constructContentDisposition } from '../../utils/constructContentDisposition';
// TODO: Remove this interface when we move to public advanced APIs.
import { GetUrlInput as GetUrlWithPathInputWithAdvancedOptions } from '../../../../internals';
import { getProperties } from './getProperties';
export const getUrl = async (
amplify: AmplifyClassV6,
input: GetUrlInput | GetUrlWithPathInputWithAdvancedOptions,
): Promise<GetUrlOutput | GetUrlWithPathOutput> => {
const { options: getUrlOptions } = input;
const { s3Config, keyPrefix, bucket, identityId } =
await resolveS3ConfigAndInput(amplify, input);
const { inputType, objectKey } = validateStorageOperationInput(
input,
identityId,
);
const finalKey =
inputType === STORAGE_INPUT_KEY ? keyPrefix + objectKey : objectKey;
if (getUrlOptions?.validateObjectExistence) {
await getProperties(amplify, input, StorageAction.GetUrl);
}
let urlExpirationInSec =
getUrlOptions?.expiresIn ?? DEFAULT_PRESIGN_EXPIRATION;
const resolvedCredential =
typeof s3Config.credentials === 'function'
? await s3Config.credentials()
: s3Config.credentials;
const awsCredExpiration = resolvedCredential.expiration;
if (awsCredExpiration) {
const awsCredExpirationInSec = Math.floor(
(awsCredExpiration.getTime() - Date.now()) / 1000,
);
urlExpirationInSec = Math.min(awsCredExpirationInSec, urlExpirationInSec);
}
const maxUrlExpirationInSec = MAX_URL_EXPIRATION / 1000;
assertValidationError(
urlExpirationInSec <= maxUrlExpirationInSec,
StorageValidationErrorCode.UrlExpirationMaxLimitExceed,
);
// expiresAt is the minimum of credential expiration and url expiration
return {
url: await getPresignedGetObjectUrl(
{
...s3Config,
credentials: resolvedCredential,
expiration: urlExpirationInSec,
},
{
Bucket: bucket,
Key: finalKey,
...(getUrlOptions?.contentDisposition && {
ResponseContentDisposition: constructContentDisposition(
getUrlOptions.contentDisposition,
),
}),
...(getUrlOptions?.contentType && {
ResponseContentType: getUrlOptions.contentType,
}),
},
),
expiresAt: new Date(Date.now() + urlExpirationInSec * 1000),
};
};