依据ImageKit的官方文档,我们需要在后台SDK传入API公钥,私钥和URL端点,前面已经配置好了。
但是,想要将图片上传到imagekit的媒体库,还需要后端服务器用私有密钥对请求进行验证
怎么验证?
使用imagekit-nestjs封装好的方法
我这里新建了user模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import { Controller, Get } from '@nestjs/common'; import { UserService } from './user.service';
@Controller('user') export class UserController { constructor( private readonly userService: UserService) {}
@Get('keys') async getImageKitKeys() { const res = await this.userService.getAuthParameters(); return res; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import { Injectable } from '@nestjs/common'; import { ImageKitService } from "imagekit-nestjs";
@Injectable() export class UserService { constructor(private readonly imageKitService: ImageKitService) {}
async getAuthParameters(){ const keys = await this.imageKitService.getAuthenticationParameters(); return keys; } }
|
开启跨域
1 2 3 4 5 6 7 8 9 10
| import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module';
async function bootstrap() { const app = await NestFactory.create(AppModule); app.enableCors(); await app.listen(3005); } bootstrap();
|
启动服务
根据官方文档所说,如果成功的话会返回三个参数
用postman试一下
完成!
寥寥数笔,花了我三个多小时🫤
参考
CodeVisionEvgen/imagekit-nestjs: Imagekit nestjs module (github.com)
React (imagekit.io)
API keys (imagekit.io)
Nest 通关秘籍 - zxg_神说要有光 - 掘金小册 (juejin.cn)