-
Notifications
You must be signed in to change notification settings - Fork 3
/
elastic-provider.js
57 lines (54 loc) · 1.48 KB
/
elastic-provider.js
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
import { S3Client, ListObjectsV2Command, HeadObjectCommand } from '@aws-sdk/client-s3'
export class ElasticProvider {
/**
* @param {string} addr Multiaddr of the elastic provider IPFS node.
* @param {{
* bucket: string
* region: string
* accessKeyId: string
* secretAccessKey: string
* }} s3Config S3 config for the bucket that elastic provider uses.
*/
constructor (addr, s3Config) {
this._addr = addr
this._s3 = new S3Client({
region: s3Config.region,
credentials: {
accessKeyId: s3Config.accessKeyId,
secretAccessKey: s3Config.secretAccessKey
}
})
this._bucketName = s3Config.bucket
}
get multiaddr () {
return this._addr
}
/**
* Determine if the elastic provider has the passed root CID.
* @param {import('multiformats').CID} cid
*/
async has (cid) {
const hasRaw = async pfx => {
const command = new ListObjectsV2Command({
Bucket: this._bucketName,
Prefix: pfx,
MaxKeys: 1
})
const response = await this._s3.send(command)
return !!(response.Contents && response.Contents.length)
}
const hasComplete = async key => {
const command = new HeadObjectCommand({
Bucket: this._bucketName,
Key: key
})
try {
await this._s3.send(command)
return true
} catch (err) {
return false
}
}
return await hasRaw(`raw/${cid}`) ? true : await hasComplete(`complete/${cid}.car`)
}
}