-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add internal getUrl API (#13882)
- Loading branch information
Showing
6 changed files
with
129 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { AmplifyClassV6 } from '@aws-amplify/core'; | ||
|
||
import { getUrl as advancedGetUrl } from '../../../src/internals'; | ||
import { getUrl as getUrlInternal } from '../../../src/providers/s3/apis/internal/getUrl'; | ||
|
||
jest.mock('../../../src/providers/s3/apis/internal/getUrl'); | ||
const mockedGetUrlInternal = jest.mocked(getUrlInternal); | ||
|
||
const MOCK_URL = new URL('https://s3.aws/mock-presigned-url'); | ||
const MOCK_DATE = new Date(); | ||
MOCK_DATE.setMonth(MOCK_DATE.getMonth() + 1); | ||
|
||
describe('getUrl (internal)', () => { | ||
beforeEach(() => { | ||
mockedGetUrlInternal.mockResolvedValue({ | ||
url: MOCK_URL, | ||
expiresAt: MOCK_DATE, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should pass through advanced options to the internal getUrl', async () => { | ||
const useAccelerateEndpoint = true; | ||
const validateObjectExistence = false; | ||
const expiresIn = 300; // seconds | ||
const contentDisposition = 'inline; filename="example.jpg"'; | ||
const contentType = 'image/jpeg'; | ||
const bucket = { bucketName: 'bucket', region: 'us-east-1' }; | ||
const locationCredentialsProvider = async () => ({ | ||
credentials: { | ||
accessKeyId: 'akid', | ||
secretAccessKey: 'secret', | ||
sessionToken: 'token', | ||
expiration: new Date(), | ||
}, | ||
}); | ||
const result = await advancedGetUrl({ | ||
path: 'input/path/to/mock/object', | ||
options: { | ||
useAccelerateEndpoint, | ||
bucket, | ||
validateObjectExistence, | ||
expiresIn, | ||
contentDisposition, | ||
contentType, | ||
locationCredentialsProvider, | ||
}, | ||
}); | ||
expect(mockedGetUrlInternal).toHaveBeenCalledTimes(1); | ||
expect(mockedGetUrlInternal).toHaveBeenCalledWith( | ||
expect.any(AmplifyClassV6), | ||
{ | ||
path: 'input/path/to/mock/object', | ||
options: { | ||
useAccelerateEndpoint, | ||
bucket, | ||
validateObjectExistence, | ||
expiresIn, | ||
contentDisposition, | ||
contentType, | ||
locationCredentialsProvider, | ||
}, | ||
}, | ||
); | ||
expect(result).toEqual({ | ||
url: MOCK_URL, | ||
expiresAt: MOCK_DATE, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Amplify } from '@aws-amplify/core'; | ||
|
||
import { getUrl as getUrlInternal } from '../../providers/s3/apis/internal/getUrl'; | ||
import { GetUrlInput } from '../types/inputs'; | ||
import { GetUrlOutput } from '../types/outputs'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function getUrl(input: GetUrlInput) { | ||
return getUrlInternal(Amplify, { | ||
path: input.path, | ||
options: { | ||
useAccelerateEndpoint: input?.options?.useAccelerateEndpoint, | ||
bucket: input?.options?.bucket, | ||
validateObjectExistence: input?.options?.validateObjectExistence, | ||
expiresIn: input?.options?.expiresIn, | ||
contentDisposition: input?.options?.contentDisposition, | ||
contentType: input?.options?.contentType, | ||
|
||
// Advanced options | ||
locationCredentialsProvider: input?.options?.locationCredentialsProvider, | ||
}, | ||
// Type casting is necessary because `getPropertiesInternal` supports both Gen1 and Gen2 signatures, but here | ||
// given in input can only be Gen2 signature, the return can only ben Gen2 signature. | ||
}) as Promise<GetUrlOutput>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters