-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add S3 integration for modules Add new function `allS3` to upload module images to S3 bucket if not already uploaded. Update the Nuxt config to include S3 credentials. Update module gallery component to display S3 image if available. Update scheduler to run `allS3` every 3 hours. # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch main # Your branch is ahead of 'origin/main' by 1 commit. # (use "git push" to publish your local commits) # Changes to be committed: # modified: server/flowing.ts # modified: nuxt.config.ts # modified: components/module/Gallery.vue # modified: server/plugins/scheduler.ts # modified: prisma/schema.prisma # modified: .gitignore # modified: pnpm-lock.yaml * feat: Upload images to S3 and log success/error messages Uploaded images to S3 in 'allS3' function and added success and error messages using 'consola'. Added conditional check in 'startScheduler' to only run 'allS3' if S3 access key is provided.
- Loading branch information
Showing
10 changed files
with
1,381 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,3 +81,5 @@ sw.* | |
# Auto generate on postinstall step | ||
auto-imports.d.ts | ||
components.d.ts | ||
|
||
server/api/test.ts |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
import { useScheduler } from '#scheduler' | ||
import { flowing, allSize } from '~/server/flowing' | ||
import { flowing, allSize, allS3 } from '~/server/flowing' | ||
|
||
export default defineNitroPlugin(() => { | ||
startScheduler() | ||
}) | ||
|
||
function startScheduler() { | ||
const config = useRuntimeConfig() | ||
|
||
const scheduler = useScheduler() | ||
|
||
// fetch every 3 hour | ||
scheduler.run(async () => { | ||
await flowing() | ||
}).everyHours(3) | ||
|
||
scheduler.run(async () => { | ||
await allSize() | ||
}).everyDays(14) | ||
if (config.s3.accessKeyId) { | ||
await allS3() | ||
} | ||
}).everyHours(3) | ||
} |
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,49 @@ | ||
import { | ||
S3Client, | ||
PutObjectCommand, | ||
|
||
} from '@aws-sdk/client-s3' | ||
|
||
const config = useRuntimeConfig() | ||
|
||
const S3 = new S3Client({ | ||
region: 'auto', | ||
endpoint: config.s3.endpoint, | ||
credentials: { | ||
accessKeyId: config.s3.accessKeyId, | ||
secretAccessKey: config.s3.secretAccessKey, | ||
}, | ||
}) | ||
|
||
export async function uploadRemoteImageToS3(imageUrl: string, s3Key: string) { | ||
try { | ||
// 下载图片 | ||
const response = await fetch(imageUrl) | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch image: ${response.statusText}`) | ||
} | ||
|
||
// 获取图片的二进制数据 | ||
const blob = await response.blob() | ||
|
||
// 将Blob转换为ArrayBuffer,因为PutObjectCommand需要Body为Buffer类型的参数 | ||
const arrayBuffer = await new Response(blob).arrayBuffer() | ||
const buffer = Buffer.from(arrayBuffer) | ||
|
||
// 上传到S3 | ||
const command = new PutObjectCommand( | ||
{ | ||
Bucket: config.s3.bucket, | ||
Key: s3Key, // 图片在S3中的路径和名称 | ||
Body: buffer, | ||
ContentType: response.headers.get('content-type') || undefined, // 保持原始的MIME类型 | ||
}, | ||
) | ||
const result = await S3.send(command) | ||
return result | ||
} | ||
catch (error) { | ||
console.error('Error uploading image:', error) | ||
throw error | ||
} | ||
} |