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

chore: rollback aws sdk js to v2 #74

Merged
merged 5 commits into from
Aug 1, 2023
Merged
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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@
},
"homepage": "https://github.com/decentraland/catalyst-storage#readme",
"dependencies": {
"@aws-sdk/client-s3": "^3.369.0",
"@aws-sdk/lib-storage": "^3.369.0",
"@well-known-components/interfaces": "^1.1.1",
"aws-sdk": "^2.1426.0",
"aws-sdk-client-mock": "^3.0.0",
"destroy": "^1.2.0"
},
"devDependencies": {
"@aws-sdk/util-stream-node": "^3.369.0",
"@dcl/eslint-config": "^1.1.6",
"@types/destroy": "^1.0.0",
"@types/mock-aws-s3": "^2.6.3",
"@types/node": "^18.11.18",
"@well-known-components/env-config-provider": "^1.2.0",
"@well-known-components/logger": "^3.1.2",
"@well-known-components/test-helpers": "^1.5.2",
"aws-sdk-client-mock-jest": "^3.0.0",
"typescript": "^4.7.3"
"mock-aws-s3": "^4.0.2",
"typescript": "^4.9.5"
},
"prettier": {
"printWidth": 120,
Expand Down
4 changes: 3 additions & 1 deletion src/extras/compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export async function compressContentFile(contentFilePath: string): Promise<bool
}

async function gzipCompressFile(input: string, output: string): Promise<CompressionResult | null> {
if (path.resolve(input) === path.resolve(output)) throw new Error("Can't compress a file using src==dst")
if (path.resolve(input) === path.resolve(output)) {
throw new Error("Can't compress a file using src==dst")
}
const gzip = createGzip()
const source = fs.createReadStream(input)
const destination = fs.createWriteStream(output)
Expand Down
84 changes: 35 additions & 49 deletions src/s3-based-storage-component.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { Upload } from '@aws-sdk/lib-storage'
import {
DeleteObjectsCommand,
GetObjectCommand,
HeadObjectCommand,
ListObjectsV2Command,
ListObjectsV2CommandOutput,
ListObjectsV2Request,
NoSuchKey,
S3Client
} from '@aws-sdk/client-s3'
import { S3 } from 'aws-sdk'
import { Readable } from 'stream'
import { AppComponents, ContentItem, IContentStorageComponent } from './types'
import { SimpleContentItem } from './content-item'
import { ListObjectsV2Output } from 'aws-sdk/clients/s3'

/**
* @public
Expand All @@ -22,7 +13,7 @@ export async function createAwsS3BasedFileSystemContentStorage(
): Promise<IContentStorageComponent> {
const { config, logs } = components

const s3 = new S3Client({
const s3 = new S3({
region: await config.requireString('AWS_REGION')
})

Expand All @@ -36,7 +27,7 @@ export async function createAwsS3BasedFileSystemContentStorage(
*/
export async function createS3BasedFileSystemContentStorage(
components: Pick<AppComponents, 'logs'>,
s3: S3Client,
s3: Pick<S3, 'headObject' | 'upload' | 'getObject' | 'deleteObjects' | 'listObjectsV2'>,
options: { Bucket: string; getKey?: (hash: string) => string }
): Promise<IContentStorageComponent> {
const logger = components.logs.getLogger('s3-based-content-storage')
Expand All @@ -45,45 +36,40 @@ export async function createS3BasedFileSystemContentStorage(

async function exist(id: string): Promise<boolean> {
try {
const command = new HeadObjectCommand({ Bucket, Key: getKey(id) })
const output = await s3.send(command)
return !!output.ETag
const obj = await s3.headObject({ Bucket, Key: getKey(id) }).promise()
return !!obj.ETag
} catch {
return false
}
}

async function storeStream(id: string, stream: Readable): Promise<void> {
await new Upload({
client: s3,
params: {
Bucket,
Key: getKey(id),
Body: stream
},

// Forcing chunks of 5Mb to improve upload of large files
partSize: 5 * 1024 * 1024
}).done()
await s3
.upload(
{
Bucket,
Key: getKey(id),
Body: stream
},
{
// Forcing chunks of 5Mb to improve upload of large files
partSize: 5 * 1024 * 1024
}
)
.promise()
}

async function retrieve(id: string): Promise<ContentItem | undefined> {
try {
const command = new GetObjectCommand({ Bucket, Key: getKey(id) })
const output = await s3.send(command)

const body = output?.Body
if (!body) {
return undefined
}
const obj = await s3.headObject({ Bucket, Key: getKey(id) }).promise()

return new SimpleContentItem(
() => Readable.fromWeb(body.transformToWebStream() as any) as any,
output.ContentLength || null,
output.ContentEncoding || null
async () => s3.getObject({ Bucket, Key: getKey(id) }).createReadStream(),
obj.ContentLength || null,
obj.ContentEncoding || null
)
} catch (error: any) {
if (!(error instanceof NoSuchKey)) {
if (error.code !== 'NotFound') {
logger.error(error)
}
}
Expand All @@ -96,21 +82,22 @@ export async function createS3BasedFileSystemContentStorage(
}

async function deleteFn(ids: string[]): Promise<void> {
const command = new DeleteObjectsCommand({
Bucket,
Delete: {
Objects: ids.map(($) => ({ Key: getKey($) }))
}
})
await s3.send(command)
await s3
.deleteObjects({
Bucket,
Delete: {
Objects: ids.map(($) => ({ Key: getKey($) }))
}
})
.promise()
}

async function existMultiple(cids: string[]): Promise<Map<string, boolean>> {
return new Map(await Promise.all(cids.map(async (cid): Promise<[string, boolean]> => [cid, await exist(cid)])))
}

async function* allFileIds(prefix?: string): AsyncIterable<string> {
const params: ListObjectsV2Request = {
const params: S3.Types.ListObjectsV2Request = {
Bucket,
ContinuationToken: undefined
}
Expand All @@ -119,10 +106,9 @@ export async function createS3BasedFileSystemContentStorage(
params.Prefix = prefix
}

let output: ListObjectsV2CommandOutput
let output: ListObjectsV2Output
do {
const command = new ListObjectsV2Command(params)
output = await s3.send(command)
output = await s3.listObjectsV2(params).promise()
if (output.Contents) {
for (const content of output.Contents) {
yield content.Key!
Expand Down
2 changes: 1 addition & 1 deletion test/content-storage-with-streams.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { createFolderBasedFileSystemContentStorage, createFsComponent, IContentStorageComponent } from '../src'
import { bufferToStream, streamToBuffer } from '../src/content-item'
import { bufferToStream, streamToBuffer } from '../src'
import { FileSystemUtils as fsu } from './file-system-utils'
import { createLogComponent } from '@well-known-components/logger'

Expand Down
2 changes: 1 addition & 1 deletion test/content-storage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { createFolderBasedFileSystemContentStorage, createFsComponent, IContentStorageComponent } from '../src'
import { bufferToStream, streamToBuffer } from '../src/content-item'
import { bufferToStream, streamToBuffer } from '../src'
import { FileSystemUtils as fsu } from './file-system-utils'
import { createLogComponent } from '@well-known-components/logger'

Expand Down
2 changes: 1 addition & 1 deletion test/file-system-content-storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mkdtempSync, rmSync } from 'fs'
import os from 'os'
import path from 'path'
import { createFolderBasedFileSystemContentStorage, createFsComponent, IContentStorageComponent } from '../src'
import { bufferToStream, streamToBuffer } from '../src/content-item'
import { bufferToStream, streamToBuffer } from '../src'
import { createLogComponent } from '@well-known-components/logger'

describe('fileSystemContentStorage', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/in-memory-storage-component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createInMemoryStorage, IContentStorageComponent } from '../src'
import { bufferToStream, streamToBuffer } from '../src/content-item'
import { bufferToStream, streamToBuffer } from '../src'

describe('storage mock', () => {
let storage: IContentStorageComponent
Expand Down
Loading
Loading