Code Monkey home page Code Monkey logo

nest-qiniu-oss's Introduction

安装

npm i nest-qiniu-oss

使用

注册

// upload.module.ts
import { Module } from '@nestjs/common'
import { UploadController } from './upload.controller'
import { conf } from 'qiniu'
import { QiniuOSSModule, QiniuOSSService } from 'nest-qiniu-oss'

@Module({
	imports: [
		QiniuOSSModule.register({
            // 七牛云 AccessKey
			accessKey: 'your qiniu accessKey',
            // 七牛云 SecretKey
			secretKey: 'your qiniu secretKey',
            // 指定上传空间
			bucket: 'bucket',
            // 域名配置
            domain: 'http://bucket.test',
            // 存储区域编码 官方不推荐使用(已弃用)
            // zone: conf.Zone,
			// 存储区域 'z0' | 'cn-east-2' | 'z1' | 'z2' | 'na0' | 'as0' | 'ap-southeast-2'
			// 查看: https://developer.qiniu.com/kodo/1671/region-endpoint-fq
			region: 'z0'
            // 注册为 nest 应用的全局模块
			global: true
		})
	],
	controllers: [UploadController],
	providers: [QiniuOSSService],
	exports: []
})
export class UploadModule {}

使用异步注册

// upload.module.ts
import { Module } from '@nestjs/common'
import { UploadController } from './upload.controller'
import { conf } from 'qiniu'
import { QiniuOSSModule, QiniuOSSService } from 'nest-qiniu-oss'

@Module({
	imports: [
		QiniuOSSModule.registerAsync({
			imports: [],
			useFactory: () => {

			// do some thing ...
			// 一般在此之前通过注入 configService 加载配置

			return {
			    accessKey: 'your qiniu accessKey',
			    secretKey: 'your qiniu secretKey',
			    bucket: 'bucket',
			    domain: 'http://bucket.test',
				region: 'z0'
			    global: true
			}
			}
		})
	],
	controllers: [UploadController],
	providers: [QiniuOSSService],
	exports: []
})
export class UploadModule {}

service

import { Injectable } from '@nestjs/common'
import { QiniuOSSService } from 'nest-qiniu-oss'

@Injectable()
export class UploadService {
	constructor(private readonly qiniuOSSService: QiniuOSSService) {}

	getUploadToken() {
		// 创建鉴权对象
		const mac = this.qiniuOSSService.mac()

		// 创建配置
		const conf = this.qiniuOSSService.createConfig()

		// 创建上传策略
		const putPolicy = this.qiniuOSSService.createPutPolicy({
			scope: this.qiniuOSSService.options.bucket
		})

		// 创建上传凭证
		return putPolicy.uploadToken(mac)
	}
}

nest-qiniu-oss's People

Contributors

liangjuen avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.