|
@@ -9,6 +9,7 @@ import { SendSmsResponse } from '@alicloud/dysmsapi20170525'
|
|
|
import * as randomstring from 'randomstring'
|
|
import * as randomstring from 'randomstring'
|
|
|
import * as OSS from 'ali-oss'
|
|
import * as OSS from 'ali-oss'
|
|
|
import { buffer } from 'stream/consumers'
|
|
import { buffer } from 'stream/consumers'
|
|
|
|
|
+import * as fs from 'fs'
|
|
|
|
|
|
|
|
@Injectable()
|
|
@Injectable()
|
|
|
export class AliyunService {
|
|
export class AliyunService {
|
|
@@ -48,7 +49,7 @@ export class AliyunService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public async uploadFile(fileName: string, fileBuffer: Buffer) {
|
|
|
|
|
|
|
+ public async uploadFile(path: string, data: Buffer) {
|
|
|
let client = new OSS({
|
|
let client = new OSS({
|
|
|
endpoint: this.aliyunConfiguration.ossEndPoint,
|
|
endpoint: this.aliyunConfiguration.ossEndPoint,
|
|
|
accessKeyId: this.aliyunConfiguration.accessKeyId,
|
|
accessKeyId: this.aliyunConfiguration.accessKeyId,
|
|
@@ -56,13 +57,15 @@ export class AliyunService {
|
|
|
bucket: this.aliyunConfiguration.ossBucket
|
|
bucket: this.aliyunConfiguration.ossBucket
|
|
|
})
|
|
})
|
|
|
try {
|
|
try {
|
|
|
- const result = await client.put(buffer, 'files/111.png')
|
|
|
|
|
- // 自定义headers
|
|
|
|
|
- //,{headers}
|
|
|
|
|
-
|
|
|
|
|
- console.log(result);
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- console.log(e);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const result = await client.put(path, data)
|
|
|
|
|
+ if (result.res.status === 200) {
|
|
|
|
|
+ return { url: result.url }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new InternalServerErrorException(result.res.statusMessage)
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ Logger.error(e)
|
|
|
|
|
+ throw new InternalServerErrorException(e.message)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|