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

Bump the aws-sdk to v3 #1615

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion src/features/device-types/build-info-facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ export const getDeviceTypeJson = multiCacheMemoizee(
);
const deviceType =
response && response.Body
? (JSON.parse(response.Body.toString()) as DeviceTypeJson)
? (JSON.parse(
await response.Body.transformToString(),
) as DeviceTypeJson)
: undefined;
if (deviceType) {
deviceType.buildId = buildId;
Expand Down
80 changes: 27 additions & 53 deletions src/features/device-types/storage/s3.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import AWSWrapper from './aws-sdk-wrapper.cjs';
import { S3 } from '@aws-sdk/client-s3';
import _ from 'lodash';
import path from 'path';

const { AWS } = AWSWrapper;

import {
IMAGE_STORAGE_ACCESS_KEY,
IMAGE_STORAGE_BUCKET as S3_BUCKET,
Expand All @@ -14,75 +12,53 @@ import {

export const getKey = (...parts: string[]): string => parts.join('/');

class UnauthenticatedS3Facade {
constructor(private s3: AWS.S3) {}

public headObject(
params: AWS.S3.Types.HeadObjectRequest,
): ReturnType<AWS.S3['headObject']> {
return this.s3.makeUnauthenticatedRequest('headObject', params);
}

public getObject(
params: AWS.S3.Types.GetObjectRequest,
): ReturnType<AWS.S3['getObject']> {
return this.s3.makeUnauthenticatedRequest('getObject', params);
}

public listObjectsV2(
params: AWS.S3.Types.ListObjectsV2Request,
): ReturnType<AWS.S3['listObjectsV2']> {
return this.s3.makeUnauthenticatedRequest('listObjectsV2', params);
}
}

function createS3Client() {
if (!IMAGE_STORAGE_ACCESS_KEY || !IMAGE_STORAGE_SECRET_KEY) {
return new UnauthenticatedS3Facade(
new AWS.S3({
endpoint: IMAGE_STORAGE_ENDPOINT,
s3ForcePathStyle: IMAGE_STORAGE_FORCE_PATH_STYLE,
signatureVersion: 'v4',
const s3Client = new S3({
// The transformation for endpoint is not implemented.
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed.
// Please create/upvote feature request on aws-sdk-js-codemod for endpoint.
endpoint: IMAGE_STORAGE_ENDPOINT,

region: 'us-east-1',

// The key s3ForcePathStyle is renamed to forcePathStyle.
forcePathStyle: IMAGE_STORAGE_FORCE_PATH_STYLE,

...(!IMAGE_STORAGE_ACCESS_KEY || !IMAGE_STORAGE_SECRET_KEY
? {
// makes the requests being unauthenticated
signer: { sign: async (request) => request },
}
: {
credentials: {
accessKeyId: IMAGE_STORAGE_ACCESS_KEY,
secretAccessKey: IMAGE_STORAGE_SECRET_KEY,
},
}),
);
}
return new AWS.S3({
endpoint: IMAGE_STORAGE_ENDPOINT,
s3ForcePathStyle: IMAGE_STORAGE_FORCE_PATH_STYLE,
signatureVersion: 'v4',
accessKeyId: IMAGE_STORAGE_ACCESS_KEY,
secretAccessKey: IMAGE_STORAGE_SECRET_KEY,
});
}

const s3Client = createS3Client();
});

async function getFileInfo(s3Path: string) {
const req = s3Client.headObject({
return await s3Client.headObject({
Bucket: S3_BUCKET,
Key: s3Path,
});
return await req.promise();
}

export async function getFile(s3Path: string) {
const req = s3Client.getObject({
return await s3Client.getObject({
Bucket: S3_BUCKET,
Key: s3Path,
});
return await req.promise();
}

export async function getFolderSize(
folder: string,
marker?: string,
): Promise<number> {
const req = s3Client.listObjectsV2({
const res = await s3Client.listObjectsV2({
Bucket: S3_BUCKET,
Prefix: `${folder}/`,
ContinuationToken: marker,
});
const res = await req.promise();

const size = _.sumBy(res.Contents, 'Size');
const nextMarker = res.NextContinuationToken;
Expand All @@ -97,15 +73,13 @@ export async function listFolders(
folder: string,
marker?: string,
): Promise<string[]> {
const req = s3Client.listObjectsV2({
const res = await s3Client.listObjectsV2({
Bucket: S3_BUCKET,
Prefix: `${folder}/`,
Delimiter: '/',
ContinuationToken: marker,
});

const res = await req.promise();

const objects = _(res.CommonPrefixes)
.map(({ Prefix }) => Prefix)
// only keep the folder paths (which are ending with `/`)
Expand Down
84 changes: 48 additions & 36 deletions test/test-lib/aws-mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type AWS from 'aws-sdk';
import type {
GetObjectCommandInput,
GetObjectCommandOutput,
HeadObjectCommandInput,
ListObjectsV2CommandInput,
S3,
S3ClientConfig,
} from '@aws-sdk/client-s3';
import Bluebird from 'bluebird';
import { assert } from 'chai';
import _ from 'lodash';
Expand All @@ -14,22 +21,27 @@ import listObjectsV2Mocks from '../fixtures/s3/listObjectsV2.json' assert { type
// AWS S3 Client getObject results have a Buffer on their Body prop
// and a Date on their LastModified prop so we have to reconstruct
// them from the strings that the mock object holds
const getObjectMocks: Dictionary<AWS.S3.Types.GetObjectOutput> = _.mapValues(
const getObjectMocks: Dictionary<GetObjectCommandOutput> = _.mapValues(
$getObjectMocks,
(
getObjectMock: (typeof $getObjectMocks)[keyof typeof $getObjectMocks],
): AWS.S3.Types.GetObjectOutput => {
): GetObjectCommandOutput => {
return {
...getObjectMock,

Body:
'Body' in getObjectMock && getObjectMock.Body
? Buffer.from(getObjectMock.Body)
? ({
async transformToString() {
return Buffer.from(getObjectMock.Body).toString();
},
} as GetObjectCommandOutput['Body'])
: undefined,
LastModified:
'LastModified' in getObjectMock && getObjectMock.LastModified
? new Date(getObjectMock.LastModified)
: undefined,
$metadata: {},
};
},
);
Expand Down Expand Up @@ -58,50 +70,52 @@ const toReturnType = <T extends (...args: any[]) => any>(result: {
} as unknown as ReturnType<T>;
};

interface UnauthenticatedRequestParams {
[key: string]: any;
}
// interface UnauthenticatedRequestParams {
// [key: string]: any;
// }

class S3Mock {
constructor(params: AWS.S3.Types.ClientConfiguration) {
constructor(params: S3ClientConfig) {
assert(
params.accessKeyId === IMAGE_STORAGE_ACCESS_KEY,
'accessKeyId' in params &&
params.accessKeyId === IMAGE_STORAGE_ACCESS_KEY,
'S3 access key not matching',
);
assert(
params.secretAccessKey === IMAGE_STORAGE_SECRET_KEY,
'secretAccessKey' in params &&
params.secretAccessKey === IMAGE_STORAGE_SECRET_KEY,
'S3 secret key not matching',
);
}

public makeUnauthenticatedRequest(
operation: string,
params?: UnauthenticatedRequestParams,
): AWS.Request<any, AWS.AWSError> {
if (operation === 'headObject') {
return this.headObject(params as AWS.S3.Types.HeadObjectRequest);
}
if (operation === 'getObject') {
return this.getObject(params as AWS.S3.Types.GetObjectRequest);
}
if (operation === 'listObjectsV2') {
return this.listObjectsV2(params as AWS.S3.Types.ListObjectsV2Request);
}
throw new Error(`AWS Mock: Operation ${operation} isn't implemented`);
}
// public makeUnauthenticatedRequest(
// operation: string,
// params?: UnauthenticatedRequestParams,
// ): AWS.Request<any, AWS.AWSError> {
// if (operation === 'headObject') {
// return this.headObject(params as HeadObjectCommandInput);
// }
// if (operation === 'getObject') {
// return this.getObject(params as GetObjectCommandInput);
// }
// if (operation === 'listObjectsV2') {
// return this.listObjectsV2(params as ListObjectsV2CommandInput);
// }
// throw new Error(`AWS Mock: Operation ${operation} isn't implemented`);
// }

public headObject(
params: AWS.S3.Types.HeadObjectRequest,
): ReturnType<AWS.S3['headObject']> {
params: HeadObjectCommandInput,
): ReturnType<S3['headObject']> {
const mock = getObjectMocks[params.Key as keyof typeof getObjectMocks];
if (mock) {
const trimmedMock = _.omit(mock, 'Body', 'ContentRange', 'TagCount');
return toReturnType<AWS.S3['headObject']>(trimmedMock);
return toReturnType<S3['headObject']>(trimmedMock);
}

// treat not found IGNORE file mocks as 404
if (_.endsWith(params.Key, '/IGNORE')) {
return toReturnType<AWS.S3['headObject']>(
return toReturnType<S3['headObject']>(
Bluebird.reject(new NotFoundError()),
);
}
Expand All @@ -111,29 +125,27 @@ class S3Mock {
);
}

public getObject(
params: AWS.S3.Types.GetObjectRequest,
): ReturnType<AWS.S3['getObject']> {
public getObject(params: GetObjectCommandInput): ReturnType<S3['getObject']> {
const mock = getObjectMocks[params.Key as keyof typeof getObjectMocks];
if (!mock) {
throw new Error(
`aws mock: getObject could not find a mock for ${params.Key}`,
);
}
return toReturnType<AWS.S3['getObject']>(mock);
return toReturnType<S3['getObject']>(mock);
}

public listObjectsV2(
params: AWS.S3.Types.ListObjectsV2Request,
): ReturnType<AWS.S3['listObjectsV2']> {
params: ListObjectsV2CommandInput,
): ReturnType<S3['listObjectsV2']> {
const mock =
listObjectsV2Mocks[params.Prefix as keyof typeof listObjectsV2Mocks];
if (!mock) {
throw new Error(
`aws mock: listObjectsV2 could not find a mock for ${params.Prefix}`,
);
}
return toReturnType<AWS.S3['listObjectsV2']>(mock);
return toReturnType<S3['listObjectsV2']>(mock);
}
}

Expand Down
Loading