-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use aws-sdk to increase aws support in eks pods
When running in EKS pod with a ServeAccount trusted by an IAM role, the application can get credentials directly from the SA to call AWS API.
- Loading branch information
1 parent
84c4adb
commit 141ccb6
Showing
14 changed files
with
1,552 additions
and
627 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createReadStream } from 'fs'; | ||
import { readFile } from 'fs/promises'; | ||
import { join } from 'path'; | ||
import { ObjectId } from 'mongodb'; | ||
import { uploadFile, getUploadedFile } from '../../server/core/storage'; | ||
import streamToBuffer from '../../server/utils/streamToBuffer'; | ||
import { cleanDatabase, composeHandlers, setupDatabase, createBucket, dropBucket } from '../helpers'; | ||
|
||
beforeEach(composeHandlers(setupDatabase, createBucket)); | ||
|
||
afterEach(composeHandlers(cleanDatabase, dropBucket)); | ||
|
||
const testFile = join(__dirname, 'img.png'); | ||
|
||
test('Test function uploadFile() followed by getUploadedFile()', async () => { | ||
const stream = createReadStream(testFile); | ||
const response = await uploadFile('jest', 'test-01.png', stream); | ||
expect(ObjectId.isValid(response._id)).toBe(true); | ||
expect(response.filename).toEqual('test-01.png'); | ||
expect(response.displayName).toEqual('test-01.png'); | ||
expect(response.uploadedAt instanceof Date).toEqual(true); | ||
expect(response.etag).toEqual('"d6869304047d8495f2465a7b963e10ec"'); | ||
expect(response.objectName).toEqual(`jest/${response._id.toHexString()}.png`); | ||
const uploadedFileStream = await getUploadedFile(response); | ||
const uploadedFile = await streamToBuffer(uploadedFileStream); | ||
const originalFile = await readFile(testFile); | ||
expect(uploadedFile).toEqual(originalFile); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,35 @@ | ||
import { | ||
CreateBucketCommand, | ||
DeleteBucketCommand, | ||
DeleteObjectsCommand, | ||
ListObjectsV2Command, | ||
} from '@aws-sdk/client-s3'; | ||
import config from '../../server/core/config'; | ||
import { minioClient } from '../../server/core/storage'; | ||
import { client } from '../../server/core/storage'; | ||
|
||
const { bucket } = config.storage; | ||
|
||
const emptyBucket = async (): Promise<void> => { | ||
const stream = await minioClient.listObjectsV2(bucket, '', true); | ||
|
||
return new Promise((resolve, reject) => { | ||
const objectNames = []; | ||
|
||
stream.on('data', object => { | ||
objectNames.push(object.name); | ||
}); | ||
|
||
stream.on('error', reject); | ||
|
||
stream.on('end', async () => { | ||
if (objectNames.length) { | ||
await minioClient.removeObjects(bucket, objectNames); | ||
} | ||
|
||
resolve(); | ||
}); | ||
}); | ||
const listObjectsResponse = await client.send(new ListObjectsV2Command({ Bucket: bucket })); | ||
const objectNames = listObjectsResponse.Contents.map(object => object.Key).filter(Boolean); | ||
|
||
if (objectNames.length) { | ||
await client.send( | ||
new DeleteObjectsCommand({ | ||
Bucket: bucket, | ||
Delete: { | ||
Objects: objectNames.map(objectName => ({ Key: objectName })), | ||
}, | ||
}) | ||
); | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const setupEmptyBucket = async (): Promise<void> => { | ||
const alreadyExist = await minioClient.bucketExists(bucket); | ||
|
||
if (alreadyExist) { | ||
await emptyBucket(); | ||
await minioClient.removeBucket(bucket); | ||
} | ||
export const createBucket = async (): Promise<void> => { | ||
await client.send(new CreateBucketCommand({ Bucket: bucket })); | ||
}; | ||
|
||
await minioClient.makeBucket(bucket, config.storage.provider.region); | ||
export const dropBucket = async (): Promise<void> => { | ||
await emptyBucket(); | ||
await client.send(new DeleteBucketCommand({ Bucket: bucket })); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.