/
signed-url.service.ts
107 lines (93 loc) · 3.04 KB
/
signed-url.service.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {
DeleteObjectCommand,
DeleteObjectsCommand,
GetObjectCommand,
PutObjectCommand,
S3Client,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { Inject, Injectable } from '@nestjs/common';
import { DEFAULT_EXPIRES_IN, S3_SERVICE } from '../constants';
import { DeleteObjectOptions, DeleteObjectsOptions, GetObjectOptions, PutObjectOptions, PutSignedUrl } from '../types';
import { PrefixService } from './prefix.service';
import { prepareOptions } from '../helpers';
@Injectable()
export class SignedUrlService {
public constructor(
@Inject(S3_SERVICE) private readonly client: S3Client,
private readonly prefixService: PrefixService,
) {}
async getPutSignedUrl(
bucket: string,
remote: string,
expiresIn: number = DEFAULT_EXPIRES_IN,
options?: PutObjectOptions,
): Promise<PutSignedUrl> {
const { disableAutoPrefix, prefixContext, options: preparedOptions } = prepareOptions(options);
const key = disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket, prefixContext);
const command = new PutObjectCommand({
Bucket: bucket,
Key: key,
...preparedOptions,
});
const preSignedUrl = await getSignedUrl(this.client, command, {
expiresIn,
});
return {
url: preSignedUrl,
remote: key,
};
}
async getSignedUrl(
bucket: string,
remote: string,
expiresIn: number = DEFAULT_EXPIRES_IN,
options?: GetObjectOptions,
): Promise<string> {
const { disableAutoPrefix, prefixContext, options: preparedOptions } = prepareOptions(options);
const command = new GetObjectCommand({
Bucket: bucket,
Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket, prefixContext),
...preparedOptions,
});
return await getSignedUrl(this.client, command, {
expiresIn,
});
}
async getDeleteSignedUrl(
bucket: string,
remote: string,
expiresIn: number = DEFAULT_EXPIRES_IN,
options?: DeleteObjectOptions,
): Promise<string> {
const { disableAutoPrefix, prefixContext, options: preparedOptions } = prepareOptions(options);
const command = new DeleteObjectCommand({
Bucket: bucket,
Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket, prefixContext),
...preparedOptions,
});
return await getSignedUrl(this.client, command, {
expiresIn,
});
}
async getDeleteObjectsSignedUrl(
bucket: string,
remotes: string[],
expiresIn: number = DEFAULT_EXPIRES_IN,
options?: DeleteObjectsOptions,
): Promise<string> {
const { disableAutoPrefix, prefixContext, options: preparedOptions } = prepareOptions(options);
const command = new DeleteObjectsCommand({
Bucket: bucket,
Delete: {
Objects: remotes.map((r) => ({
Key: disableAutoPrefix ? r : this.prefixService.prefix(r, bucket, prefixContext),
})),
},
...preparedOptions,
});
return await getSignedUrl(this.client, command, {
expiresIn,
});
}
}