English | 简体中文
Alicloud OSS module for Nest framework (node.js)
- Create Alicloud account
- Purchase OSS. (read more)
- Create bucket, go to Dashboard > Object Storage Service > Bucket List > Add Bucket
$ npm install --save nestjs-alicloud-oss
#or
$ yarn add nestjs-alicloud-oss
- Import the
AlicloudOssModule
with the following configuration:
import { Module } from '@nestjs/common';
import { AlicloudOssModule } from 'nestjs-alicloud-oss';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
controllers: [AppController],
providers: [AppService],
imports: [
AlicloudOssModule.withConfig({
options: {
accessKeyId: '******',
accessKeySecret: '******',
region: '******', // the bucket data region location, doc demo used 'oss-cn-beijing'.
bucket: '******', // the default bucket you want to access, doc demo used 'nest-alicloud-oss-demo'.
},
}),
],
})
export class AppModule {}
You may provide a default
bucket
name for the whole module, this will apply to all controllers withing this module. You can also provide (override) thebucket
in the controller, for each route.
import { Controller, Logger, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { AlicloudOssFileInterceptor, UploadedFileMetadata } from 'nestjs-alicloud-oss';
@Controller()
export class AppController {
@Post('upload')
@UseInterceptors(AlicloudOssFileInterceptor('file'))
UploadedFileUsingInterceptor(
@UploadedFile()
file: UploadedFileMetadata,
) {
console.log(file.objectUrl);
// http://nest-alicloud-oss-demo.oss-cn-beijing.aliyuncs.com/filename
}
}
import { Controller, Logger, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { AlicloudOssFileInterceptor, UploadedFileMetadata } from 'nestjs-alicloud-oss';
@Controller()
export class AppController {
@Post('upload2')
@UseInterceptors(
AlicloudOssFileInterceptor('file', null, {
{ folder: 'x/y/z', bucket: 'nest-alicloud-oss-demo2' },
}),
)
UploadedFilesUsingInterceptor(
@UploadedFile()
file: UploadedFileMetadata,
) {
console.log(file.objectUrl);
// http://nest-alicloud-oss-demo2.oss-cn-beijing.aliyuncs.com/x/y/z/filename
}
}
import { Controller, Logger, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { AlicloudOssService, UploadedFileMetadata } from 'nestjs-alicloud-oss';
@Controller()
export class AppController {
constructor(private readonly ossService: AlicloudOssService) {}
@Post('upload3')
@UseInterceptors(FileInterceptor('file'))
async UploadedFilesUsingService(
@UploadedFile()
file: UploadedFileMetadata,
) {
file = {
...file,
customName: 'customFilename.txt',
folder: 'a/b/c',
bucket: 'nest-alicloud-oss-demo3'
};
const url = await this.ossService.upload(file);
console.log(url)
// http://nest-alicloud-oss-demo3.oss-cn-beijing.aliyuncs.com/a/b/c/customFilename.txt
console.log(file.objectUrl);
// http://nest-alicloud-oss-demo3.oss-cn-beijing.aliyuncs.com/a/b/c/customFilename.txt
}
}
The MIT License.