-
Notifications
You must be signed in to change notification settings - Fork 403
/
s3.ts
185 lines (159 loc) · 4.3 KB
/
s3.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as aws from '@aws-sdk/client-s3'
import { Upload } from '@aws-sdk/lib-storage'
import { BlobStore, BlobNotFoundError } from '@atproto/repo'
import { randomStr } from '@atproto/crypto'
import { CID } from 'multiformats/cid'
import stream from 'stream'
export type S3Config = { bucket: string } & Omit<
aws.S3ClientConfig,
'apiVersion'
>
// @NOTE we use Upload rather than client.putObject because stream
// length is not known in advance. See also aws/aws-sdk-js-v3#2348.
export class S3BlobStore implements BlobStore {
private client: aws.S3
private bucket: string
constructor(public did: string, cfg: S3Config) {
const { bucket, ...rest } = cfg
this.bucket = bucket
this.client = new aws.S3({
...rest,
apiVersion: '2006-03-01',
})
}
static creator(cfg: S3Config) {
return (did: string) => {
return new S3BlobStore(did, cfg)
}
}
private genKey() {
return randomStr(32, 'base32')
}
private getTmpPath(key: string): string {
return `tmp/${this.did}/${key}`
}
private getStoredPath(cid: CID): string {
return `blocks/${this.did}/${cid.toString()}`
}
private getQuarantinedPath(cid: CID): string {
return `quarantine/${this.did}/${cid.toString()}`
}
async putTemp(bytes: Uint8Array | stream.Readable): Promise<string> {
const key = this.genKey()
await new Upload({
client: this.client,
params: {
Bucket: this.bucket,
Body: bytes,
Key: this.getTmpPath(key),
},
}).done()
return key
}
async makePermanent(key: string, cid: CID): Promise<void> {
const alreadyHas = await this.hasStored(cid)
if (!alreadyHas) {
await this.move({
from: this.getTmpPath(key),
to: this.getStoredPath(cid),
})
} else {
// already saved, so we no-op & just delete the temp
await this.deleteKey(this.getTmpPath(key))
}
}
async putPermanent(
cid: CID,
bytes: Uint8Array | stream.Readable,
): Promise<void> {
await new Upload({
client: this.client,
params: {
Bucket: this.bucket,
Body: bytes,
Key: this.getStoredPath(cid),
},
}).done()
}
async quarantine(cid: CID): Promise<void> {
await this.move({
from: this.getStoredPath(cid),
to: this.getQuarantinedPath(cid),
})
}
async unquarantine(cid: CID): Promise<void> {
await this.move({
from: this.getQuarantinedPath(cid),
to: this.getStoredPath(cid),
})
}
private async getObject(cid: CID) {
const res = await this.client.getObject({
Bucket: this.bucket,
Key: this.getStoredPath(cid),
})
if (res.Body) {
return res.Body
} else {
throw new BlobNotFoundError()
}
}
async getBytes(cid: CID): Promise<Uint8Array> {
const res = await this.getObject(cid)
return res.transformToByteArray()
}
async getStream(cid: CID): Promise<stream.Readable> {
const res = await this.getObject(cid)
return res as stream.Readable
}
async delete(cid: CID): Promise<void> {
await this.deleteKey(this.getStoredPath(cid))
}
async deleteMany(cids: CID[]): Promise<void> {
const keys = cids.map((cid) => this.getStoredPath(cid))
await this.deleteManyKeys(keys)
}
async hasStored(cid: CID): Promise<boolean> {
return this.hasKey(this.getStoredPath(cid))
}
async hasTemp(key: string): Promise<boolean> {
return this.hasKey(this.getTmpPath(key))
}
private async hasKey(key: string) {
try {
const res = await this.client.headObject({
Bucket: this.bucket,
Key: key,
})
return res.$metadata.httpStatusCode === 200
} catch (err) {
return false
}
}
private async deleteKey(key: string) {
await this.client.deleteObject({
Bucket: this.bucket,
Key: key,
})
}
private async deleteManyKeys(keys: string[]) {
await this.client.deleteObjects({
Bucket: this.bucket,
Delete: {
Objects: keys.map((k) => ({ Key: k })),
},
})
}
private async move(keys: { from: string; to: string }) {
await this.client.copyObject({
Bucket: this.bucket,
CopySource: `${this.bucket}/${keys.from}`,
Key: keys.to,
})
await this.client.deleteObject({
Bucket: this.bucket,
Key: keys.from,
})
}
}
export default S3BlobStore