oss bucket创建
    创建规则:
    image.png
    由于上传文件,设置为公共读

    然后配置access_key
    获取位置为:
    image.png
    新建后只能查看一次,然后在后端oss.service.ts配置对应config.service.ts即可

    前端上传文件

    1. import { HttpClient } from '@angular/common/http';
    2. import { Injectable } from '@angular/core';
    3. import { BE_URL } from '../constant';
    4. @Injectable()
    5. export class UploadService {
    6. constructor(private httpClient: HttpClient) {}
    7. // 上传文件
    8. uploadFile(file: any) {
    9. const formData = new FormData();
    10. formData.append('file', file);
    11. return this.httpClient.post<any>(`${BE_URL}/project/api/upload`, formData, {
    12. params: {
    13. _allow_anonymous: 'true'
    14. }
    15. });
    16. }
    17. }

    调用服务

    1. // 上传文件
    2. this.uploadService.uploadFile(files[0]).subscribe(result => {
    3. console.log({ result });
    4. });

    后端上传文件

    1. import {
    2. Controller,
    3. Post,
    4. Req,
    5. UploadedFile,
    6. UseInterceptors,
    7. } from '@nestjs/common';
    8. import { FileInterceptor } from '@nestjs/platform-express';
    9. import { format } from 'date-fns';
    10. import { nonceStr } from '../../shared/functions/utils';
    11. import { MLogger } from '../../shared/modules/logger/logger.service';
    12. import { OssService } from '../../shared/services/oss.service';
    13. // 用于提供各种api的controller
    14. @Controller('project/api')
    15. export class ApiController {
    16. private logger = new MLogger(this.constructor.name);
    17. constructor(public ossService: OssService) {}
    18. @Post('/upload')
    19. @UseInterceptors(FileInterceptor('file'))
    20. async uploadFile(@UploadedFile() file, @Req() req) {
    21. const result = await this.ossService.uploadFile(
    22. file.buffer,
    23. `upload-files/${format(new Date(), 'yyyyMMdd')}/${nonceStr(5)}-${
    24. file.originalname
    25. }`,
    26. );
    27. return { ...result, filename: file.originalname };
    28. }
    29. }