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(storage): integrate locationCredentialsProvider #13590

Draft
wants to merge 15 commits into
base: storage-browser/main
Choose a base branch
from
14 changes: 7 additions & 7 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -461,43 +461,43 @@
"name": "[Storage] copy (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ copy }",
"limit": "14.9 kB"
"limit": "15.28 kB"
},
{
"name": "[Storage] downloadData (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ downloadData }",
"limit": "15.49 kB"
"limit": "15.89 kB"
},
{
"name": "[Storage] getProperties (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ getProperties }",
"limit": "14.77 kB"
"limit": "15.14 kB"
},
{
"name": "[Storage] getUrl (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ getUrl }",
"limit": "15.87 kB"
"limit": "16.27 kB"
},
{
"name": "[Storage] list (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ list }",
"limit": "15.36 kB"
"limit": "15.74 kB"
},
{
"name": "[Storage] remove (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ remove }",
"limit": "14.63 kB"
"limit": "15.2 kB"
},
{
"name": "[Storage] uploadData (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ uploadData }",
"limit": "19.94 kB"
"limit": "20.30 kB"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ import {
import './testUtils';

jest.mock('../../../../src/providers/s3/utils/client/s3data');
jest.mock('../../../../src/providers/s3/utils');
jest.mock('../../../../src/providers/s3/utils', () => {
const utils = jest.requireActual('../../../../src/providers/s3/utils');

return {
...utils,
createDownloadTask: jest.fn(),
validateStorageOperationInput: jest.fn(),
};
});
jest.mock('@aws-amplify/core', () => ({
ConsoleLogger: jest.fn().mockImplementation(function ConsoleLogger() {
return { debug: jest.fn() };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

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

import { uploadData } from '../../../../../src/providers/s3/apis';
import { MAX_OBJECT_SIZE } from '../../../../../src/providers/s3/utils/constants';
import { createUploadTask } from '../../../../../src/providers/s3/utils';
Expand All @@ -12,7 +14,25 @@ import { putObjectJob } from '../../../../../src/providers/s3/apis/uploadData/pu
import { getMultipartUploadHandlers } from '../../../../../src/providers/s3/apis/uploadData/multipart';
import { UploadDataInput, UploadDataWithPathInput } from '../../../../../src';

jest.mock('../../../../../src/providers/s3/utils/');
jest.mock('@aws-amplify/core', () => ({
ConsoleLogger: jest.fn().mockImplementation(function ConsoleLogger() {
return { debug: jest.fn() };
}),
Amplify: {
getConfig: jest.fn(),
Auth: {
fetchAuthSession: jest.fn(),
},
},
}));
jest.mock('../../../../../src/providers/s3/utils', () => {
const utils = jest.requireActual('../../../../../src/providers/s3/utils');

return {
...utils,
createUploadTask: jest.fn(),
};
});
jest.mock('../../../../../src/providers/s3/apis/uploadData/putObjectJob');
jest.mock('../../../../../src/providers/s3/apis/uploadData/multipart');

Expand All @@ -27,9 +47,21 @@ const mockGetMultipartUploadHandlers = (
onResume: jest.fn(),
onCancel: jest.fn(),
});

const mockGetConfig = Amplify.getConfig as jest.Mock;
const bucket = 'bucket';
const region = 'region';
/* TODO Remove suite when `key` parameter is removed */
describe('uploadData with key', () => {
beforeAll(() => {
mockGetConfig.mockReturnValue({
Storage: {
S3: {
bucket,
region,
},
},
});
});
afterEach(() => {
jest.clearAllMocks();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ describe('getMultipartUploadHandlers with key', () => {
const mockS3Config: S3InternalConfig = {
credentialsProvider: mockCredentialsProvider,
identityIdProvider: mockIdentityIdProvider,
serviceOptions: mockServiceOptions,
libraryOptions: mockLibraryOptions,
...mockServiceOptions,
...mockLibraryOptions,
};
beforeAll(() => {
mockCredentialsProvider.mockImplementation(async () => credentials);
Expand Down Expand Up @@ -692,8 +692,8 @@ describe('getMultipartUploadHandlers with path', () => {
const mockS3Config: S3InternalConfig = {
credentialsProvider: mockCredentialsProvider,
identityIdProvider: mockIdentityIdProvider,
serviceOptions: mockServiceOptions,
libraryOptions: mockLibraryOptions,
...mockServiceOptions,
...mockLibraryOptions,
};
beforeAll(() => {
mockCredentialsProvider.mockImplementation(async () => credentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ mockPutObject.mockResolvedValue({
const config: S3InternalConfig = {
credentialsProvider: mockCredentialsProvider,
identityIdProvider: mockIdentityIdProvider,
serviceOptions: mockServiceOptions,
libraryOptions: mockLibraryOptions,
...mockServiceOptions,
...mockLibraryOptions,
};

/* TODO Remove suite when `key` parameter is removed */
Expand Down Expand Up @@ -124,9 +124,7 @@ describe('putObjectJob with key', () => {
const job = putObjectJob({
config: {
...config,
libraryOptions: {
isObjectLockEnabled: true,
},
isObjectLockEnabled: true,
},
input: {
key: 'key',
Expand Down Expand Up @@ -220,9 +218,7 @@ describe('putObjectJob with path', () => {
const job = putObjectJob({
config: {
...config,
libraryOptions: {
isObjectLockEnabled: true,
},
isObjectLockEnabled: true,
},
input: {
path: testPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe('resolveS3ConfigAndInput', () => {
const config: S3InternalConfig = {
credentialsProvider: mockCredentialsProvider,
identityIdProvider: mockIdentityIdProvider,
serviceOptions: mockServiceOptions,
libraryOptions: mockLibraryOptions,
...mockServiceOptions,
...mockLibraryOptions,
};
beforeEach(() => {
mockCredentialsProvider.mockImplementation(async () => credentials);
Expand Down Expand Up @@ -88,9 +88,7 @@ describe('resolveS3ConfigAndInput', () => {
resolveS3ConfigAndInput({
config: {
...config,
serviceOptions: {
bucket: undefined,
},
bucket: undefined,
},
}),
).rejects.toMatchObject(
Expand All @@ -108,9 +106,7 @@ describe('resolveS3ConfigAndInput', () => {
resolveS3ConfigAndInput({
config: {
...config,
serviceOptions: {
bucket,
},
region: undefined,
},
}),
).rejects.toMatchObject(
Expand All @@ -126,7 +122,7 @@ describe('resolveS3ConfigAndInput', () => {
};

const { s3Config } = await resolveS3ConfigAndInput({
config: { ...config, serviceOptions },
config: { ...config, ...serviceOptions },
});
expect(s3Config.customEndpoint).toEqual('http://localhost:20005');
expect(s3Config.forcePathStyle).toEqual(true);
Expand All @@ -136,7 +132,7 @@ describe('resolveS3ConfigAndInput', () => {
const { isObjectLockEnabled } = await resolveS3ConfigAndInput({
config: {
...config,
libraryOptions: { isObjectLockEnabled: true },
isObjectLockEnabled: true,
},
});
expect(isObjectLockEnabled).toEqual(true);
Expand All @@ -154,9 +150,7 @@ describe('resolveS3ConfigAndInput', () => {
const { keyPrefix } = await resolveS3ConfigAndInput({
config: {
...config,
libraryOptions: {
prefixResolver: customResolvePrefix,
},
prefixResolver: customResolvePrefix,
},
});
expect(customResolvePrefix).toHaveBeenCalled();
Expand Down Expand Up @@ -184,9 +178,7 @@ describe('resolveS3ConfigAndInput', () => {
const { keyPrefix } = await resolveS3ConfigAndInput({
config: {
...config,
libraryOptions: {
defaultAccessLevel: 'someLevel' as any,
},
defaultAccessLevel: 'someLevel' as any,
},
});
expect(mockDefaultResolvePrefix).toHaveBeenCalledWith({
Expand Down
5 changes: 5 additions & 0 deletions packages/storage/src/errors/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export const NO_STORAGE_CONFIG = 'NoStorageConfig';
export const INVALID_STORAGE_PATH = 'InvalidStoragePath';
5 changes: 4 additions & 1 deletion packages/storage/src/providers/s3/apis/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CopyWithPathInput,
CopyWithPathOutput,
} from '../types';
import { createStorageConfiguration } from '../utils';

import { copy as copyInternal } from './internal/copy';

Expand Down Expand Up @@ -38,5 +39,7 @@ export function copy(input: CopyWithPathInput): Promise<CopyWithPathOutput>;
export function copy(input: CopyInput): Promise<CopyOutput>;

export function copy(input: CopyInput | CopyWithPathInput) {
return copyInternal(Amplify, input);
const config = createStorageConfiguration(Amplify, input, 'READWRITE');

return copyInternal(config, input);
}
91 changes: 5 additions & 86 deletions packages/storage/src/providers/s3/apis/downloadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@
// SPDX-License-Identifier: Apache-2.0

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

import {
DownloadDataInput,
DownloadDataOutput,
DownloadDataWithPathInput,
DownloadDataWithPathOutput,
} from '../types';
import { resolveS3ConfigAndInput } from '../utils/resolveS3ConfigAndInput';
import { createDownloadTask, validateStorageOperationInput } from '../utils';
import { getObject } from '../utils/client/s3data';
import { createStorageConfiguration } from '../utils/config';
import { getStorageUserAgentValue } from '../utils/userAgent';
import { logger } from '../../../utils';
import {
StorageDownloadDataOutput,
StorageItemWithKey,
StorageItemWithPath,
} from '../../../types';
import { STORAGE_INPUT_KEY } from '../utils/constants';
import { createStorageConfiguration } from '../utils';

import { internalDownloadData } from './internal/downloadData';

/**
* Download S3 object data to memory
Expand Down Expand Up @@ -94,78 +84,7 @@ export function downloadData(input: DownloadDataInput): DownloadDataOutput;
export function downloadData(
input: DownloadDataInput | DownloadDataWithPathInput,
) {
const abortController = new AbortController();

const downloadTask = createDownloadTask({
job: downloadDataJob(input, abortController.signal),
onCancel: (message?: string) => {
abortController.abort(message);
},
});
const config = createStorageConfiguration(Amplify, input, 'READ');

return downloadTask;
return internalDownloadData(input, config);
}

const downloadDataJob =
(
downloadDataInput: DownloadDataInput | DownloadDataWithPathInput,
abortSignal: AbortSignal,
) =>
async (): Promise<
StorageDownloadDataOutput<StorageItemWithKey | StorageItemWithPath>
> => {
const { options: downloadDataOptions } = downloadDataInput;
const config = createStorageConfiguration(Amplify);

const { bucket, keyPrefix, s3Config, identityId } =
await resolveS3ConfigAndInput({
config,
apiOptions: downloadDataOptions,
});
const { inputType, objectKey } = validateStorageOperationInput(
downloadDataInput,
identityId,
);
const finalKey =
inputType === STORAGE_INPUT_KEY ? keyPrefix + objectKey : objectKey;

logger.debug(`download ${objectKey} from ${finalKey}.`);

const {
Body: body,
LastModified: lastModified,
ContentLength: size,
ETag: eTag,
Metadata: metadata,
VersionId: versionId,
ContentType: contentType,
} = await getObject(
{
...s3Config,
abortSignal,
onDownloadProgress: downloadDataOptions?.onProgress,
userAgentValue: getStorageUserAgentValue(StorageAction.DownloadData),
},
{
Bucket: bucket,
Key: finalKey,
...(downloadDataOptions?.bytesRange && {
Range: `bytes=${downloadDataOptions.bytesRange.start}-${downloadDataOptions.bytesRange.end}`,
}),
},
);

const result = {
body,
lastModified,
size,
contentType,
eTag,
metadata,
versionId,
};

return inputType === STORAGE_INPUT_KEY
? { key: objectKey, ...result }
: { path: objectKey, ...result };
};
Loading
Loading