npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nest-public/nest-oss

v2.2.0

Published

nest.js框架阿里云OSS上传插件

Downloads

8

Readme

npm version npm

nest-oss

Nest框架 阿里云OSS上传插件

安装

$ npm install @nest-public/nest-oss --save

用法

config.js 配置

export const config = {
	client: {
		endpoint: 'oss-cn-shenzhen.aliyuncs.com', // endpoint域名
		accessKeyId: 'xxxxxxxxxxxx', // 账号
		accessKeySecret: 'xxxxxxx', // 密码
		bucket: 'xxxxxx', // 存储桶
		internal: false, // 是否使用阿里云内部网访问
		secure: true, // 使用 HTTPS
		cname: false, // 自定义endpoint
		timeout: '90s'
	},
	domain: '', // 自定义域名
};

module.ts

import { AppController } from './test.controller';
import { OSSModule } from '@nest-public/nest-oss';
import { config } from '../config/config.js';

@Module({
	imports: [ 
		OSSModule.forRoot(config)
	],
	controllers: [AppController],
})

export class AppModule{}

controller.ts

import { Controller, Req, Post, Get, UseInterceptors, UploadedFiles } from '@nestjs/common';
import { FilesInterceptor } from '@nestjs/platform-express';
import { OSSService } from '@nest-public/nest-oss';

/**
 * AppController
 * @export
 * @class AppControllerr
 */
@Controller()
export class AppController {
    constructor(private readonly oSSService: OSSService) {}

   /**
    * 多文件上传oss
    */
    @Post('uploadOSS')
    @UseInterceptors(FilesInterceptor('files'))
	public async uploadOSS(@UploadedFiles() file) {
		const result = await this.oSSService.upload(file);

		return result;
		// result [
		// 	{
		// 		uploaded: true,
		// 		path: 'images/20191115/16420962.png',
		// 		src: 'http://xxxx.oss-cn-shenzhen.aliyuncs.com/images/20191115/16420962.png',
		// 		srcSign: 'https://xxx.oss-cn-shenzhen.aliyuncs.com/images/20191115/16420962.png?OSSAccessKeyId=LTAI6lgwcBcCbiKX&Expires=1573814530&Signature=brYN7qbDdyxGARc%2BdoRsnblJx2w%3D',
		// 		message: '上传成功'
		// 	}
		// ]
    }
    
    /**
     * 对oss文件签名
     */
    @Get('getUrl')
    public async uploadOSS(@Req() request) {
        const url = req.query.url; // 原始oss url
        const width = req.query.width; // 设置返回图片宽度
        const height = req.query.height; // 设置返回图片高度
        const result = await this.oSSService.getOssSign(file, width, height);

        return result;
	}
	
	/**
	 * 前端获取签名
	 */
	@Get('uploadSgin')
	public getUploadSgin() {
		const result = this.oSSService.getUploadSgin();

		return result;

        // result {
		// 	policy: 'xxxxxxxxxxxxxx';
		// 	OSSAccessKeyId: 'xxxxxxxxxxxxxx'
		// 	signature: 'xxxxxxxxxxxxxx'
		// }
	}

    /**
     * 批量删除图片
     */
    @Get('delete')
    public async deleteOSS(@Req() request) {
        const uploadUrl = ['images/20191115/16420962.png'];
		const result = await this.oSSService.deleteMulti([uploadUrl]);

		return result;
		// result {
		// 	res: {
		// 		status: 200,
		// 		statusCode: 200,
		// 		statusMessage: 'OK',
		// 		// ....
		// 	},
		//  deleted: [ 'images/20191115/16420962.png' ]
		// }
    }
}

获取签名参数在浏览器端上传说明

可以参考 https://help.aliyun.com/document_detail/31925.html?spm=a2c4g.11186623.2.11.10836e28CWrMt0#concept-frd-4gy-5db

  1. 通过接口获取签名参数
  2. 以fromdata的形式准备好上传数据(一次只能上传一个文件)
// 上传参数
const params = new FromData();
formData.append('key', '${filename}');
formData.append('name', '${filename}'); // 文件名
formData.append('policy', 'xxxxxx');
formData.append('OSSAccessKeyId', 'xxxxxx');
formData.append('success_action_status', 200); // 让服务端返回200,不设置则默认返回204
formData.append('signature', 'xxxxxx');
formData.append('file', file); // 上传的文件 
  1. 以 Post multipart 形式上传

测试

进行单元测试前请先配置好 test.module.ts 里面的oss配置

# jest 测试
$ npm run test