Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gen2 path parameter to getProperties and getUrl #13144

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3392532
feat: add gen2 path to getProperties API
erinleigh90 Mar 15, 2024
943fde6
test: add unit tests for path parameter
erinleigh90 Mar 15, 2024
265a494
nit: remove unnecessary colon
erinleigh90 Mar 15, 2024
78550ea
feat: add path to types
erinleigh90 Mar 18, 2024
4fdc01f
test: first pass to add path coverage
erinleigh90 Mar 18, 2024
f7df812
Merge branch 'gen2-getProperties' into gen2-getUrl
erinleigh90 Mar 18, 2024
0cad022
fix: adjust tests
erinleigh90 Mar 18, 2024
b2118b3
Merge branch 'gen2-storage' into gen2-getUrl-getProperties
erinleigh90 Mar 19, 2024
23213af
chore: remove links from tsdocs
erinleigh90 Mar 19, 2024
ceaa7a4
test: fix expected Key
erinleigh90 Mar 19, 2024
7b1137d
chore: prettier fix
erinleigh90 Mar 19, 2024
972b655
chore: increase getUrl bundle size
erinleigh90 Mar 19, 2024
2fda5b5
chore: clean up tsdocs in getProperties
erinleigh90 Mar 20, 2024
c3eda69
chore: consolidate constants imports
erinleigh90 Mar 20, 2024
59d541d
test: remove leading slash from path
erinleigh90 Mar 21, 2024
19d6dd8
chore: address feedback
erinleigh90 Mar 21, 2024
a6a4fd0
Merge branch 'gen2-storage' into gen2-getUrl-getProperties
erinleigh90 Mar 22, 2024
d8b4b12
chore: fix test and bundle size check
erinleigh90 Mar 22, 2024
ee54ff3
chore: add back-tics to tsdocs
erinleigh90 Mar 22, 2024
f138c31
chore: use test.each instead of forEach
erinleigh90 Mar 22, 2024
b069077
chore: fix deprecation and tsdocs
erinleigh90 Mar 22, 2024
f3305c8
chore: add contextSpec to copy tsdocs
erinleigh90 Mar 22, 2024
23a5076
Update packages/storage/src/providers/s3/apis/copy.ts
erinleigh90 Mar 22, 2024
b2c4cce
chore: another tsdoc fix
erinleigh90 Mar 22, 2024
644aee1
chore: add tsdocs to server getUrl
erinleigh90 Mar 22, 2024
ec73fc8
address feedback
erinleigh90 Mar 25, 2024
7b530cf
address feedback
erinleigh90 Mar 25, 2024
3b5a402
address feedback
erinleigh90 Mar 25, 2024
30d5b86
chore: add overload for deprecation messaging to getUrl
erinleigh90 Mar 25, 2024
b604e0a
address feedback
erinleigh90 Mar 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 119 additions & 8 deletions packages/storage/__tests__/providers/s3/apis/getProperties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { headObject } from '../../../../src/providers/s3/utils/client';
import { getProperties } from '../../../../src/providers/s3';
import { AWSCredentials } from '@aws-amplify/core/internals/utils';
import { Amplify } from '@aws-amplify/core';
import { GetPropertiesOptions } from '../../../../src/providers/s3/types';
import {
GetPropertiesOptionsKey,
GetPropertiesOptionsPath,
} from '../../../../src/providers/s3/types';

jest.mock('../../../../src/providers/s3/utils/client');
jest.mock('@aws-amplify/core', () => ({
Expand All @@ -30,10 +33,12 @@ const credentials: AWSCredentials = {
sessionToken: 'sessionToken',
secretAccessKey: 'secretAccessKey',
};
const key = 'key';
const path = '/path';
const targetIdentityId = 'targetIdentityId';
const defaultIdentityId = 'defaultIdentityId';

describe('getProperties api', () => {
describe('getProperties with key', () => {
beforeAll(() => {
mockFetchAuthSession.mockResolvedValue({
credentials,
Expand All @@ -48,9 +53,9 @@ describe('getProperties api', () => {
},
});
});
describe('getProperties happy path ', () => {
describe('getProperties happy path with key', () => {
erinleigh90 marked this conversation as resolved.
Show resolved Hide resolved
const expected = {
key: 'key',
key,
size: '100',
contentType: 'text/plain',
eTag: 'etag',
Expand All @@ -63,7 +68,6 @@ describe('getProperties api', () => {
region: 'region',
userAgentValue: expect.any(String),
};
const key = 'key';
beforeEach(() => {
mockHeadObject.mockReturnValueOnce({
ContentLength: '100',
Expand Down Expand Up @@ -111,7 +115,7 @@ describe('getProperties api', () => {
expect(
await getProperties({
key,
options: options as GetPropertiesOptions,
options: options as GetPropertiesOptionsKey,
}),
).toEqual(expected);
expect(headObject).toHaveBeenCalledTimes(1);
Expand All @@ -133,7 +137,7 @@ describe('getProperties api', () => {
);
expect.assertions(3);
try {
await getProperties({ key: 'keyed' });
await getProperties({ key });
} catch (error: any) {
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(
Expand All @@ -144,7 +148,114 @@ describe('getProperties api', () => {
},
{
Bucket: 'bucket',
Key: 'public/keyed',
Key: `public/${key}`,
},
);
expect(error.$metadata.httpStatusCode).toBe(404);
}
});
});
});

describe('getProperties with path', () => {
beforeAll(() => {
mockFetchAuthSession.mockResolvedValue({
credentials,
identityId: defaultIdentityId,
});
mockGetConfig.mockReturnValue({
Storage: {
S3: {
bucket,
region,
},
},
});
});
describe('getProperties with path', () => {
const expected = {
path,
size: '100',
contentType: 'text/plain',
eTag: 'etag',
metadata: { key: 'value' },
lastModified: 'last-modified',
versionId: 'version-id',
};
const config = {
credentials,
region: 'region',
useAccelerateEndpoint: true,
userAgentValue: expect.any(String),
};
beforeEach(() => {
mockHeadObject.mockReturnValueOnce({
ContentLength: '100',
ContentType: 'text/plain',
ETag: 'etag',
LastModified: 'last-modified',
Metadata: { key: 'value' },
VersionId: 'version-id',
});
});
afterEach(() => {
jest.clearAllMocks();
});
[
{
path: '/path',
expectedKey: '/path',
},
{
path: () => '/path',
expectedKey: '/path',
},
].forEach(({ path, expectedKey }) => {
it(`should getProperties with path ${path} and expectedKey ${expectedKey}`, async () => {
const headObjectOptions = {
Bucket: 'bucket',
Key: expectedKey,
};
expect.assertions(3);
erinleigh90 marked this conversation as resolved.
Show resolved Hide resolved
expect(
await getProperties({
path,
options: {
useAccelerateEndpoint: true,
} as GetPropertiesOptionsPath,
}),
).toEqual(expected);
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(config, headObjectOptions);
});
});
});

describe('getProperties error path with path', () => {
erinleigh90 marked this conversation as resolved.
Show resolved Hide resolved
afterEach(() => {
jest.clearAllMocks();
});
it('getProperties should return a not found error', async () => {
mockHeadObject.mockRejectedValueOnce(
Object.assign(new Error(), {
$metadata: { httpStatusCode: 404 },
name: 'NotFound',
}),
);
expect.assertions(3);
try {
await getProperties({ path });
} catch (error: any) {
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(
{
credentials,
region: 'region',
userAgentValue: expect.any(String),
},
{
Bucket: 'bucket',
Key: path,
},
);
expect(error.$metadata.httpStatusCode).toBe(404);
Expand Down
106 changes: 103 additions & 3 deletions packages/storage/__tests__/providers/s3/apis/getUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
getPresignedGetObjectUrl,
headObject,
} from '../../../../src/providers/s3/utils/client';
import { GetUrlOptions } from '../../../../src/providers/s3/types';
import {
GetUrlOptionsKey,
GetUrlOptionsPath,
} from '../../../../src/providers/s3/types';

jest.mock('../../../../src/providers/s3/utils/client');
jest.mock('@aws-amplify/core', () => ({
Expand All @@ -35,7 +38,7 @@ const credentials: AWSCredentials = {
const targetIdentityId = 'targetIdentityId';
const defaultIdentityId = 'defaultIdentityId';

describe('getUrl test', () => {
describe('getUrl test with key', () => {
beforeAll(() => {
mockFetchAuthSession.mockResolvedValue({
credentials,
Expand Down Expand Up @@ -112,7 +115,7 @@ describe('getUrl test', () => {
options: {
...options,
validateObjectExistence: true,
} as GetUrlOptions,
} as GetUrlOptionsKey,
});
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -147,3 +150,100 @@ describe('getUrl test', () => {
});
});
});

describe('getUrl test with path', () => {
beforeAll(() => {
mockFetchAuthSession.mockResolvedValue({
credentials,
identityId: defaultIdentityId,
});
mockGetConfig.mockReturnValue({
Storage: {
S3: {
bucket,
region,
},
},
});
});

describe('getUrl happy path', () => {
const config = {
credentials,
region,
userAgentValue: expect.any(String),
};
const path = '/path';
beforeEach(() => {
(headObject as jest.Mock).mockImplementation(() => {
return {
Key: path,
ContentLength: '100',
ContentType: 'text/plain',
ETag: 'etag',
LastModified: 'last-modified',
Metadata: { key: 'value' },
};
});
(getPresignedGetObjectUrl as jest.Mock).mockReturnValueOnce({
url: new URL('https://google.com'),
});
});
afterEach(() => {
jest.clearAllMocks();
});
[
{
path: '/path',
expectedKey: '/path',
},
{
path: () => '/path',
expectedKey: '/path',
},
].forEach(({ path, expectedKey }) => {
it(`should getUrl with path ${path} and expectedKey ${expectedKey}`, async () => {
const headObjectOptions = {
Bucket: bucket,
Key: expectedKey,
};
expect.assertions(4);
erinleigh90 marked this conversation as resolved.
Show resolved Hide resolved
const result = await getUrl({
path,
options: {
validateObjectExistence: true,
} as GetUrlOptionsPath,
});
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledWith(config, headObjectOptions);
expect(result.url).toEqual({
url: new URL('https://google.com'),
});
});
});
});
describe('getUrl error path', () => {
afterAll(() => {
jest.clearAllMocks();
});
it('should return not found error when the object is not found', async () => {
(headObject as jest.Mock).mockImplementation(() => {
throw Object.assign(new Error(), {
$metadata: { httpStatusCode: 404 },
name: 'NotFound',
});
});
expect.assertions(2);
try {
await getUrl({
path: '/invalid_key',
options: { validateObjectExistence: true },
});
} catch (error: any) {
expect(headObject).toHaveBeenCalledTimes(1);
expect(error.$metadata?.httpStatusCode).toBe(404);
}
});
});
});
45 changes: 41 additions & 4 deletions packages/storage/src/providers/s3/apis/getProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@

import { Amplify } from '@aws-amplify/core';

import { GetPropertiesInput, GetPropertiesOutput, S3Exception } from '../types';
import {
GetPropertiesInput,
GetPropertiesInputKey,

Check warning on line 8 in packages/storage/src/providers/s3/apis/getProperties.ts

View workflow job for this annotation

GitHub Actions / unit-tests / Unit Test - @aws-amplify/storage

Deprecated: Use {@link GetPropertiesInputPath} instead
GetPropertiesInputPath,
GetPropertiesOutput,
GetPropertiesOutputKey,
GetPropertiesOutputPath,
S3Exception,
} from '../types';
import { StorageValidationErrorCode } from '../../../errors/types/validation';

import { getProperties as getPropertiesInternal } from './internal/getProperties';
Expand All @@ -17,8 +25,37 @@
* @throws A {@link S3Exception} when the underlying S3 service returned error.
* @throws A {@link StorageValidationErrorCode} when API call parameters are invalid.
*/
export const getProperties = (

interface GetProperties {
/**
* Gets the properties of a file. The properties include S3 system metadata and
* the user metadata that was provided when uploading the file.
*
* @param input - The GetPropertiesInput object.
* @returns Requested object properties.
* @throws An {@link S3Exception} when the underlying S3 service returned error.
erinleigh90 marked this conversation as resolved.
Show resolved Hide resolved
* @throws A {@link StorageValidationErrorCode} when API call parameters are invalid.
*/
(input: GetPropertiesInputPath): Promise<GetPropertiesOutputPath>;
/**
* @deprecated The `key` and `accessLevel` parameters are deprecated and may be removed in the next major version.
* Please use {@link https://docs.amplify.aws/react/build-a-backend/storage/download/#downloaddata | path} instead.
erinleigh90 marked this conversation as resolved.
Show resolved Hide resolved
*
* Gets the properties of a file. The properties include S3 system metadata and
* the user metadata that was provided when uploading the file.
*
* @param input - The GetPropertiesInput object.
* @returns Requested object properties.
* @throws An {@link S3Exception} when the underlying S3 service returned error.
* @throws A {@link StorageValidationErrorCode} when API call parameters are invalid.
*/
(input: GetPropertiesInputKey): Promise<GetPropertiesOutputKey>;

Check warning on line 52 in packages/storage/src/providers/s3/apis/getProperties.ts

View workflow job for this annotation

GitHub Actions / unit-tests / Unit Test - @aws-amplify/storage

Deprecated: Use {@link GetPropertiesInputPath} instead
}

export const getProperties: GetProperties = <
Output extends GetPropertiesOutput,
>(
input: GetPropertiesInput,
): Promise<GetPropertiesOutput> => {
return getPropertiesInternal(Amplify, input);
): Promise<Output> => {
return getPropertiesInternal(Amplify, input) as Promise<Output>;
};
erinleigh90 marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading