0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -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"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.315.0",
"@azure/storage-blob": "^12.13.0",
"@koa/cors": "^4.0.0",
"@logto/app-insights": "workspace:^1.1.0",
@ -40,7 +41,6 @@
"@logto/shared": "workspace:^2.0.0",
"@logto/ui": "workspace:*",
"@silverhand/essentials": "^2.5.0",
"aws-sdk": "^2.1329.0",
"chalk": "^5.0.0",
"clean-deep": "^3.4.0",
"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';
@ -8,10 +8,20 @@ export const buildS3Storage = (
accessKeyId: 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,
accessKeyId,
secretAccessKey,
credentials: {
accessKeyId,
secretAccessKey,
},
});
const uploadFile: UploadFile = async (
@ -19,21 +29,22 @@ export const buildS3Storage = (
objectKey: string,
{ contentType, publicUrl } = {}
) => {
const { Location, Key } = await s3
.upload({
Body: data,
Key: objectKey,
Bucket: bucket,
ContentType: contentType,
})
.promise();
const command = new PutObjectCommand({
Bucket: bucket,
Key: objectKey,
Body: data,
ContentType: contentType,
ACL: 'public-read',
});
await client.send(command);
if (publicUrl) {
return { url: `${publicUrl}/${objectKey}` };
}
return {
url: Location,
url: `https://${bucket}.s3.${region}.amazonaws.com/${objectKey}`,
};
};

File diff suppressed because it is too large Load diff