白板的微信
我找到了一种使用 aws-sdk 执行此操作的方法,此代码并非 100% 我的代码:uploadToS3Bucket功能:import * as AWS from "aws-sdk";import { v4 as uuid } from "uuid";const s3 = new AWS.S3({ region: process.env.AWS_REGION, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, accessKeyId: process.env.AWS_ACCESS_KEY_ID,});export const uploadToS3Bucket = async ( file: any, bucket: string): Promise<{ key: string; url: string }> => { try { const { type, subtype, extname } = file; let mimeType = type + "/" + subtype; let fileType = "image/jpg"; const name = uuid() + "." + extname; let buffer = Buffer.from(JSON.stringify(file), "utf-8"); await s3 .putObject({ Key: name, Bucket: bucket, ContentType: fileType, Body: buffer.toString("base64"), ACL: "public-read", }) .promise(); let url = `https://${bucket}.s3.amazonaws.com/${name}`; console.log(url); return { key: name, url, }; } catch (err) { console.log(err); return err; }};在控制器中:public async store({ request }: HttpContextContract) { let file = getFileFromRequest(request, "defined_file_prop_name_here"); if (file) { await uploadToS3Bucket(file, BUCKET_NAME); }}