使用 AdonisJS 5 的文件存储 (S3)
File Storage (S3) with AdonisJS 5
在 AdonisJS v4 文档中,我们有 this section explaining how to stream files to a S3 bucket. I was looking for something similar in AdonisJS v5 docs but it have just an example 如何将文件上传到本地服务器。
如果它还没有准备好,因为 Adonis 5 不是它的最新版本,这是通过 Adonis v5(特别是打字稿)将文件上传到 S3 的另一种方式?
我找到了使用 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);
}
}
Adonis 4 使用 flydrive 实现 s3 存储,您也可以使用它来简化和节省时间。使用 flydrive 配置和实例化后非常简单
// Supported drivers: "local", "s3", "gcs"
await storage.disk('s3').put('testfile.txt', 'filecontents');
在 AdonisJS v4 文档中,我们有 this section explaining how to stream files to a S3 bucket. I was looking for something similar in AdonisJS v5 docs but it have just an example 如何将文件上传到本地服务器。
如果它还没有准备好,因为 Adonis 5 不是它的最新版本,这是通过 Adonis v5(特别是打字稿)将文件上传到 S3 的另一种方式?
我找到了使用 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);
}
}
Adonis 4 使用 flydrive 实现 s3 存储,您也可以使用它来简化和节省时间。使用 flydrive 配置和实例化后非常简单
// Supported drivers: "local", "s3", "gcs"
await storage.disk('s3').put('testfile.txt', 'filecontents');