|
1
|
import { S3 } from 'aws-sdk';
|
|
2
|
import { Logger, Injectable } from '@nestjs/common';
|
|
3
|
import { ConfigService } from '@nestjs/config';
|
|
4
|
//https://github.com/dimer47/node-ovh-objectstorage#readme
|
|
5
|
// based on https://docs.openstack.org/api-ref/object-store
|
|
6
|
import OVHStorage from 'node-ovh-objectstorage';
|
|
7
|
//second solution : https://github.com/stefanodefaria/swift-client
|
|
8
|
//import SwiftClient from 'openstack-swift-client';
|
|
9
|
//therd solution use directly the api of
|
|
10
|
|
|
11
|
|
|
12
|
/** TODO update with multer s3 : after check the lib stability */
|
|
13
|
@Injectable()
|
|
14
|
export class FileUploadService {
|
|
15
|
|
|
16
|
|
|
17
|
bucketName: string;
|
|
18
|
s3Instance: S3 = null;
|
|
19
|
swiftInstance: OVHStorage = null;
|
|
20
|
|
|
21
|
|
|
22
|
constructor(
|
|
23
|
private configService: ConfigService
|
|
24
|
) {
|
|
25
|
this.bucketName = this.configService.get('AWS_S3_BUCKET');
|
|
26
|
this.s3Instance = this.getS3();
|
|
27
|
this.swiftInstance = this.getSwift();
|
|
28
|
//this.swiftconnet();
|
|
29
|
|
|
30
|
|
|
31
|
}
|
|
32
|
|
|
33
|
|
|
34
|
async swiftconnet() {
|
|
35
|
try {
|
|
36
|
//let storage = new OVHStorage(config);
|
|
37
|
await this.getSwift().connection();
|
|
38
|
} catch (e) {
|
|
39
|
// throw e
|
|
40
|
console.log('error swift connection', e);
|
|
41
|
}
|
|
42
|
}
|
|
43
|
|
|
44
|
|
|
45
|
/** Singleton istance for the s3 object */
|
|
46
|
getSwift(): OVHStorage {
|
|
47
|
//console.log('getSwift ', this.swiftInstance);
|
|
48
|
if (this.swiftInstance) return this.swiftInstance;
|
|
49
|
const storageConfig = {
|
|
50
|
username: this.configService.get('SWIFT_USER_NAME'),
|
|
51
|
password: this.configService.get('SWIFT_PASSWORD'),
|
|
52
|
authURL: this.configService.get('SWIFT_AUTH_END_POINT'),
|
|
53
|
tenantId: this.configService.get('SWIFT_TENENT_ID'),
|
|
54
|
region: this.configService.get('SWIFT_REGION')
|
|
55
|
// key is optional
|
|
56
|
// key: "Temporary key (optional), for generate temporary download link",
|
|
57
|
// // options is optional
|
|
58
|
// options: {
|
|
59
|
// slugify : true, // true, by default
|
|
60
|
// check_exists: true, // true, by default
|
|
61
|
// }
|
|
62
|
}
|
|
63
|
//console.log('swift storageConfig', storageConfig);
|
|
64
|
return new OVHStorage(storageConfig);
|
|
65
|
}
|
|
66
|
|
|
67
|
|
|
68
|
async upload(file: Express.Multer.File, isS3: boolean = false) {
|
|
69
|
if (isS3) {
|
|
70
|
return await this.uploadS3(file.buffer, this.bucketName, file.originalname);
|
|
71
|
} else {
|
|
72
|
await this.swiftconnet();
|
|
73
|
return await this.uploadSwift(file);
|
|
74
|
}
|
|
75
|
}
|
|
76
|
|
|
77
|
|
|
78
|
async uploadSwift(file: Express.Multer.File) {
|
|
79
|
try {
|
|
80
|
const path: string = this.configService.get('SWIFT_CONTAINER')+ file.originalname;
|
|
81
|
const publicPath : string = this.configService.get('SWIFT_PUBLIC_URL')+ file.originalname;
|
|
82
|
//console.log('uploadSwift path', path);
|
|
83
|
const res = await this.getSwift().objects().saveData(file.buffer, path);
|
|
84
|
return { ...res, publicPath : publicPath};
|
|
85
|
} catch (e) {
|
|
86
|
console.log('upload swift error', e);
|
|
87
|
}
|
|
88
|
}
|
|
89
|
|
|
90
|
|
|
91
|
/** Singleton istance for the s3 object */
|
|
92
|
getS3() {
|
|
93
|
if (this.s3Instance) return this.s3Instance;
|
|
94
|
return new S3({
|
|
95
|
endpoint: this.configService.get('AWS_S3_END_POINT'),
|
|
96
|
accessKeyId: this.configService.get('AWS_ACCESS_KEY_ID'),
|
|
97
|
secretAccessKey: this.configService.get('AWS_SECRET_ACCESS_KEY')
|
|
98
|
});
|
|
99
|
}
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
async uploadS3(file, bucket, name) {
|
|
105
|
const params = {
|
|
106
|
Bucket: bucket,
|
|
107
|
Key: String(name),
|
|
108
|
Body: file,
|
|
109
|
};
|
|
110
|
return new Promise((resolve, reject) => {
|
|
111
|
this.getS3().upload(params, (err, data) => {
|
|
112
|
if (err) {
|
|
113
|
Logger.error(err);
|
|
114
|
reject(err.message);
|
|
115
|
}
|
|
116
|
console.log('data upload s3', data);
|
|
117
|
resolve(data);
|
|
118
|
});
|
|
119
|
});
|
|
120
|
}
|
|
121
|
|
|
122
|
|
|
123
|
/** get file from s3 bucket */
|
|
124
|
async getObjectS3(fileKey: string) {
|
|
125
|
console.log('fileKey', fileKey);
|
|
126
|
|
|
127
|
|
|
128
|
return new Promise((resolve, reject) => {
|
|
129
|
this.getS3().getObject(
|
|
130
|
{ Bucket: this.bucketName, Key: fileKey },
|
|
131
|
function (err, data) {
|
|
132
|
if (err) {
|
|
133
|
console.error('s3 get file (object) error ', err);
|
|
134
|
Logger.error(err);
|
|
135
|
reject(err.message);
|
|
136
|
} else {
|
|
137
|
console.log('s3 get file (object) data sucess', data);
|
|
138
|
resolve(data);
|
|
139
|
}
|
|
140
|
}
|
|
141
|
);
|
|
142
|
});
|
|
143
|
}
|
|
144
|
|
|
145
|
|
|
146
|
/** get stream file from s3 bucket */
|
|
147
|
async getstreamObjectS3(fileKey: string) {
|
|
148
|
//https://stackoverflow.com/questions/69718485/how-to-download-file-from-aws-s3-bucket-in-nestjs
|
|
149
|
// res.attachment(fileKey);
|
|
150
|
// var fileStream = s3.getObject(options).createReadStream();
|
|
151
|
// fileStream.pipe(res);
|
|
152
|
// // Download to specific location with following code:
|
|
153
|
// var file = require('fs').createWriteStream('your/download/path');
|
|
154
|
// s3.getObject(params).createReadStream().pipe(file);
|
|
155
|
|
|
156
|
|
|
157
|
// return new Promise((resolve, reject) => {
|
|
158
|
// this.getS3().getObject(
|
|
159
|
// { Bucket: this.bucketName, Key: fileKey },
|
|
160
|
// function (err, data) {
|
|
161
|
// if (err) {
|
|
162
|
// console.error('s3 get file (object) error ', err);
|
|
163
|
// Logger.error(err);
|
|
164
|
// reject(err.message);
|
|
165
|
// } else {
|
|
166
|
// console.log('s3 get file (object) data sucess', data);
|
|
167
|
// resolve(data);
|
|
168
|
// }
|
|
169
|
// }
|
|
170
|
// );
|
|
171
|
// });
|
|
172
|
}
|
|
173
|
}
|