/
S3Bucket.ts
65 lines (58 loc) · 1.76 KB
/
S3Bucket.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { S3 } from 'aws-sdk';
import * as mime from 'mime';
import { bindNodeCallback, Observable, Observer } from 'rxjs';
import { Readable } from 'stream';
/**
* @beta
*/
export class S3Bucket {
private bucketName: string;
private s3Client: S3;
public static toMimeType(key: string): string {
return mime.getType(key);
}
public constructor(bucketName: string, client?: S3) {
this.bucketName = bucketName;
this.s3Client = client ? client : new S3();
}
public setBucketName(name: string): void {
if (!name) {
throw new Error('Bucket name is required.');
}
this.bucketName = name;
}
public exists(fileName: string): Observable<boolean> {
return Observable.create((observer: Observer<boolean>) => {
this.s3Client.headObject({ Bucket: this.bucketName, Key: fileName }, (err, data) => {
if (err && err.code === 'NotFound') {
observer.next(false);
} else if (err) {
observer.error(err);
} else {
observer.next(true);
}
observer.complete();
});
});
}
public store(
fileName: string,
content: string | Buffer | Readable,
mimeType?: string
): Observable<S3.Types.PutObjectOutput> {
const rval: Function = bindNodeCallback<S3.Types.PutObjectRequest, S3.Types.PutObjectOutput>(
this.s3Client.putObject
);
return rval(this.toPutObjectRequest(fileName, content, mimeType));
}
// tslint:disable-next-line:no-any
private toPutObjectRequest(key: string, content: any, mimeType?: string): S3.Types.PutObjectRequest {
const rval: S3.Types.PutObjectRequest = {
Body: content,
Bucket: this.bucketName,
ContentType: mimeType ? mimeType : S3Bucket.toMimeType(key),
Key: key
};
return rval;
}
}