-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add support s3 custom domain #2014
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e1d55cd
feat: add support s3 custom domain
boris-w 28c8694
chore: remove log code and env example
boris-w 5290713
refactor: use AWS official method for S3 custom domain
boris-w 396765d
feat: support aliyun oss
boris-w 9af7ffb
feat: debug s3 sockets connection count
boris-w 339c542
chore: safe isNaN
boris-w File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
68 changes: 68 additions & 0 deletions
68
apps/nestjs-backend/src/features/attachments/plugins/aliyun.ts
This file contains hidden or 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,68 @@ | ||
| /* eslint-disable sonarjs/no-duplicate-string */ | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3'; | ||
| import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { NodeHttpHandler } from '@smithy/node-http-handler'; | ||
| import { IStorageConfig, StorageConfig } from '../../../configs/storage'; | ||
| import { second } from '../../../utils/second'; | ||
| import type StorageAdapter from './adapter'; | ||
| import { S3Storage } from './s3'; | ||
| import type { IRespHeaders } from './types'; | ||
|
|
||
| @Injectable() | ||
| export class AliyunStorage extends S3Storage implements StorageAdapter { | ||
| private aliyunClient: S3Client; | ||
|
|
||
| constructor(@StorageConfig() readonly config: IStorageConfig) { | ||
| super(config); | ||
| const { endpoint, region, accessKey, secretKey, maxSockets } = this.config.s3; | ||
| const requestHandler = maxSockets | ||
| ? new NodeHttpHandler({ | ||
| httpsAgent: { | ||
| maxSockets: maxSockets, | ||
| }, | ||
boris-w marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| : undefined; | ||
| this.aliyunClient = new S3Client({ | ||
| region, | ||
| endpoint, | ||
| requestHandler, | ||
| credentials: { | ||
| accessKeyId: accessKey, | ||
| secretAccessKey: secretKey, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| private replacePrivateBucketEndpoint(url: string, bucket: string) { | ||
| const { privateBucketEndpoint, privateBucket } = this.config; | ||
| if (privateBucketEndpoint && bucket === privateBucket) { | ||
| const resUrl = new URL(url); | ||
| const newUrl = new URL(privateBucketEndpoint); | ||
| resUrl.protocol = newUrl.protocol; | ||
| resUrl.hostname = newUrl.hostname; | ||
| resUrl.port = newUrl.port; | ||
| return resUrl.toString(); | ||
| } | ||
| return url; | ||
| } | ||
|
|
||
| async getPreviewUrl( | ||
| bucket: string, | ||
| path: string, | ||
| expiresIn: number = second(this.config.urlExpireIn), | ||
| respHeaders?: IRespHeaders | ||
| ): Promise<string> { | ||
| const command = new GetObjectCommand({ | ||
| Bucket: bucket, | ||
| Key: path, | ||
| ResponseContentDisposition: respHeaders?.['Content-Disposition'], | ||
| }); | ||
|
|
||
| const res = await getSignedUrl(this.aliyunClient, command, { | ||
| expiresIn: expiresIn ?? second(this.config.tokenExpireIn), | ||
| }); | ||
| return this.replacePrivateBucketEndpoint(res, bucket); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add example and doc for this .env