0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

fix(core): use client s3 sdk (#3715)

This commit is contained in:
wangsijie 2023-04-20 15:20:05 +08:00 committed by GitHub
parent 0acda89071
commit 2ad86d55b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 1036 additions and 214 deletions

View file

@ -25,6 +25,7 @@
"test:report": "codecov -F core" "test:report": "codecov -F core"
}, },
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "^3.315.0",
"@azure/storage-blob": "^12.13.0", "@azure/storage-blob": "^12.13.0",
"@koa/cors": "^4.0.0", "@koa/cors": "^4.0.0",
"@logto/app-insights": "workspace:^1.1.0", "@logto/app-insights": "workspace:^1.1.0",
@ -40,7 +41,6 @@
"@logto/shared": "workspace:^2.0.0", "@logto/shared": "workspace:^2.0.0",
"@logto/ui": "workspace:*", "@logto/ui": "workspace:*",
"@silverhand/essentials": "^2.5.0", "@silverhand/essentials": "^2.5.0",
"aws-sdk": "^2.1329.0",
"chalk": "^5.0.0", "chalk": "^5.0.0",
"clean-deep": "^3.4.0", "clean-deep": "^3.4.0",
"date-fns": "^2.29.3", "date-fns": "^2.29.3",

View file

@ -1,4 +1,4 @@
import AWS from 'aws-sdk'; import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import type { UploadFile } from './types.js'; import type { UploadFile } from './types.js';
@ -8,10 +8,20 @@ export const buildS3Storage = (
accessKeyId: string, accessKeyId: string,
secretAccessKey: string secretAccessKey: string
) => { ) => {
const s3 = new AWS.S3({ // Endpoint example: s3.us-west-2.amazonaws.com
const region = /s3\.([^.]*)\.amazonaws/.exec(endpoint)?.[1];
if (!region) {
throw new Error('Invalid S3 endpoint, can not find region');
}
const client = new S3Client({
region,
endpoint, endpoint,
accessKeyId, credentials: {
secretAccessKey, accessKeyId,
secretAccessKey,
},
}); });
const uploadFile: UploadFile = async ( const uploadFile: UploadFile = async (
@ -19,21 +29,22 @@ export const buildS3Storage = (
objectKey: string, objectKey: string,
{ contentType, publicUrl } = {} { contentType, publicUrl } = {}
) => { ) => {
const { Location, Key } = await s3 const command = new PutObjectCommand({
.upload({ Bucket: bucket,
Body: data, Key: objectKey,
Key: objectKey, Body: data,
Bucket: bucket, ContentType: contentType,
ContentType: contentType, ACL: 'public-read',
}) });
.promise();
await client.send(command);
if (publicUrl) { if (publicUrl) {
return { url: `${publicUrl}/${objectKey}` }; return { url: `${publicUrl}/${objectKey}` };
} }
return { return {
url: Location, url: `https://${bucket}.s3.${region}.amazonaws.com/${objectKey}`,
}; };
}; };

File diff suppressed because it is too large Load diff