Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
israx committed Jul 22, 2024
1 parent b2fdbb4 commit da383b3
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe('resolveS3ConfigAndInput', () => {
it('should resolve prefix with given access level', async () => {
mockDefaultResolvePrefix.mockResolvedValueOnce('prefix');
const { keyPrefix } = await resolveS3ConfigAndInput(Amplify, {
accessLevel: 'someLevel' as any,
options: { accessLevel: 'someLevel' as any },
});
expect(mockDefaultResolvePrefix).toHaveBeenCalledWith({
accessLevel: 'someLevel',
Expand Down Expand Up @@ -232,7 +232,9 @@ describe('resolveS3ConfigAndInput', () => {
},
});
const { s3Config } = await resolveS3ConfigAndInput(Amplify, {
locationCredentialsProvider: mockLocationCredentialsProvider,
options: {
locationCredentialsProvider: mockLocationCredentialsProvider,
},
});

if (typeof s3Config.credentials === 'function') {
Expand Down Expand Up @@ -267,18 +269,16 @@ describe('resolveS3ConfigAndInput', () => {
const testCases = [...deprecatedInputs, ...callbackPathInputs];

it.each(testCases)('should throw when input is %s', async input => {
const { s3Config } = await resolveS3ConfigAndInput(
Amplify,
{ locationCredentialsProvider: mockLocationCredentialsProvider },
input,
);
const { s3Config } = await resolveS3ConfigAndInput(Amplify, {
...input,
options: {
locationCredentialsProvider: mockLocationCredentialsProvider,
},
});
if (typeof s3Config.credentials === 'function') {
await expect(s3Config.credentials()).rejects.toThrow(
expect.objectContaining({
name: INVALID_STORAGE_INPUT,
message: 'The input needs to have a path as a string value.',
recoverySuggestion:
'Please provide a valid path as a string value for the input.',
}),
);
} else {
Expand Down
6 changes: 1 addition & 5 deletions packages/storage/src/providers/s3/apis/downloadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ const downloadDataJob =
> => {
const { options: downloadDataOptions } = downloadDataInput;
const { bucket, keyPrefix, s3Config, identityId } =
await resolveS3ConfigAndInput(
Amplify,
downloadDataOptions,
downloadDataInput,
);
await resolveS3ConfigAndInput(Amplify, downloadDataInput);
const { inputType, objectKey } = validateStorageOperationInput(
downloadDataInput,
identityId,
Expand Down
9 changes: 5 additions & 4 deletions packages/storage/src/providers/s3/apis/internal/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const copyWithPath = async (
const { source, destination } = input;
const { s3Config, bucket, identityId } = await resolveS3ConfigAndInput(
amplify,
{},
input,
);

Expand Down Expand Up @@ -95,11 +94,13 @@ export const copyWithKey = async (
s3Config,
bucket,
keyPrefix: sourceKeyPrefix,
} = await resolveS3ConfigAndInput(amplify, input.source, input);
} = await resolveS3ConfigAndInput(amplify, {
...input,
options: input.source,
});
const { keyPrefix: destinationKeyPrefix } = await resolveS3ConfigAndInput(
amplify,
input.destination,
input,
{ ...input, options: input.destination },
); // resolveS3ConfigAndInput does not make extra API calls or storage access if called repeatedly.

// TODO(ashwinkumar6) V6-logger: warn `You may copy files from another user if the source level is "protected", currently it's ${srcLevel}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ export const getProperties = async (
input: GetPropertiesInput | GetPropertiesWithPathInput,
action?: StorageAction,
): Promise<GetPropertiesOutput | GetPropertiesWithPathOutput> => {
const { options: getPropertiesOptions } = input;
const { s3Config, bucket, keyPrefix, identityId } =
await resolveS3ConfigAndInput(amplify, getPropertiesOptions, input);
await resolveS3ConfigAndInput(amplify, input);
const { inputType, objectKey } = validateStorageOperationInput(
input,
identityId,
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/src/providers/s3/apis/internal/getUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const getUrl = async (
): Promise<GetUrlOutput | GetUrlWithPathOutput> => {
const { options: getUrlOptions } = input;
const { s3Config, keyPrefix, bucket, identityId } =
await resolveS3ConfigAndInput(amplify, getUrlOptions, input);
await resolveS3ConfigAndInput(amplify, input);
const { inputType, objectKey } = validateStorageOperationInput(
input,
identityId,
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/src/providers/s3/apis/internal/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const list = async (
bucket,
keyPrefix: generatedPrefix,
identityId,
} = await resolveS3ConfigAndInput(amplify, options, input);
} = await resolveS3ConfigAndInput(amplify, input);

const { inputType, objectKey } = validateStorageOperationInputWithPrefix(
input,
Expand Down
3 changes: 1 addition & 2 deletions packages/storage/src/providers/s3/apis/internal/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ export const remove = async (
amplify: AmplifyClassV6,
input: RemoveInput | RemoveWithPathInput,
): Promise<RemoveOutput | RemoveWithPathOutput> => {
const { options = {} } = input ?? {};
const { s3Config, keyPrefix, bucket, identityId } =
await resolveS3ConfigAndInput(amplify, options, input);
await resolveS3ConfigAndInput(amplify, input);

const { inputType, objectKey } = validateStorageOperationInput(
input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export const getMultipartUploadHandlers = (
const { options: uploadDataOptions, data } = uploadDataInput;
const resolvedS3Options = await resolveS3ConfigAndInput(
Amplify,
uploadDataOptions,
uploadDataInput,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ export const putObjectJob =
async (): Promise<ItemWithKey | ItemWithPath> => {
const { options: uploadDataOptions, data } = uploadDataInput;
const { bucket, keyPrefix, s3Config, isObjectLockEnabled, identityId } =
await resolveS3ConfigAndInput(
Amplify,
uploadDataOptions,
uploadDataInput,
);
await resolveS3ConfigAndInput(Amplify, uploadDataInput);
const { inputType, objectKey } = validateStorageOperationInput(
uploadDataInput,
identityId,
Expand Down
40 changes: 36 additions & 4 deletions packages/storage/src/providers/s3/utils/resolveS3ConfigAndInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ import {
StorageOperationInputWithPrefix,
} from '../../../types/inputs';
import { StorageError } from '../../../errors/StorageError';
import { CopyInput } from '../types';
import {
CopyInput,
CopyWithPathInput,
DownloadDataInput,
DownloadDataWithPathInput,
GetPropertiesInput,
GetPropertiesWithPathInput,
GetUrlInput,
GetUrlWithPathInput,
ListAllInput,
ListAllWithPathInput,
ListPaginateInput,
ListPaginateWithPathInput,
RemoveWithPathInput,
} from '../types';
import { INVALID_STORAGE_INPUT } from '../../../errors/constants';

import { DEFAULT_ACCESS_LEVEL, LOCAL_TESTING_S3_ENDPOINT } from './constants';
Expand All @@ -37,14 +51,30 @@ interface ResolvedS3ConfigAndInput {
identityId?: string;
}
export type DeprecatedStorageInput =
| DownloadDataInput
| GetUrlInput
| StorageOperationInputWithKey
| StorageOperationInputWithPrefix
| CopyInput
| ListAllInput
| ListPaginateInput
| GetPropertiesInput
| CopyInput;

export type CallbackPathStorageInput =
| StorageOperationInputWithPath
| StorageCopyInputWithPath;

type StorageInput =
| DeprecatedStorageInput
| DownloadDataWithPathInput
| RemoveWithPathInput
| ListAllWithPathInput
| ListPaginateWithPathInput
| GetPropertiesWithPathInput
| GetUrlWithPathInput
| CopyWithPathInput;

/**
* resolve the common input options for S3 API handlers from Amplify configuration and library options.
*
Expand All @@ -58,9 +88,9 @@ export type CallbackPathStorageInput =
*/
export const resolveS3ConfigAndInput = async (
amplify: AmplifyClassV6,
apiOptions?: S3ApiOptions,
input?: DeprecatedStorageInput | CallbackPathStorageInput,
apiInput?: StorageInput & { options?: S3ApiOptions },
): Promise<ResolvedS3ConfigAndInput> => {
const { options: apiOptions } = apiInput ?? {};
/**
* IdentityId is always cached in memory so we can safely make calls here. It
* should be stable even for unauthenticated users, regardless of credentials.
Expand All @@ -75,7 +105,9 @@ export const resolveS3ConfigAndInput = async (
*/
const credentialsProvider = async () => {
if (isLocationCredentialsProvider(apiOptions)) {
assertStorageInput(input);
assertStorageInput(
apiInput as DeprecatedStorageInput | CallbackPathStorageInput,
);
}

const { credentials } = isLocationCredentialsProvider(apiOptions)
Expand Down

0 comments on commit da383b3

Please sign in to comment.