@@ -14,6 +14,8 @@ import {
14
14
ListObjectsV2Command
15
15
} from '@aws-sdk/client-s3'
16
16
import { CID } from 'multiformats/cid'
17
+ import { base32upper } from 'multiformats/bases/base32'
18
+ import type { MultibaseCodec } from 'multiformats/bases/interface'
17
19
18
20
export interface S3DatastoreInit {
19
21
/**
@@ -31,6 +33,12 @@ export interface S3DatastoreInit {
31
33
* Whether to try to create the bucket if it is missing when `.open` is called
32
34
*/
33
35
createIfMissing ?: boolean
36
+
37
+ /**
38
+ * The multibase codec to use - nb. should be case insensitive.
39
+ * default: base32upper
40
+ */
41
+ base ?: MultibaseCodec < string >
34
42
}
35
43
36
44
/**
@@ -41,6 +49,7 @@ export class S3Blockstore extends BaseBlockstore {
41
49
public createIfMissing : boolean
42
50
private readonly s3 : S3
43
51
private readonly bucket : string
52
+ private readonly base : MultibaseCodec < string >
44
53
45
54
constructor ( s3 : S3 , bucket : string , init ?: S3DatastoreInit ) {
46
55
super ( )
@@ -57,14 +66,17 @@ export class S3Blockstore extends BaseBlockstore {
57
66
this . s3 = s3
58
67
this . bucket = bucket
59
68
this . createIfMissing = init ?. createIfMissing ?? false
69
+ this . base = init ?. base ?? base32upper
60
70
}
61
71
62
72
/**
63
73
* Returns the full key which includes the path to the ipfs store
64
74
*/
65
- _getFullKey ( key : CID ) : string {
75
+ _getFullKey ( cid : CID ) : string {
66
76
// Avoid absolute paths with s3
67
- return [ this . path , key . toString ( ) ] . filter ( Boolean ) . join ( '/' ) . replace ( / \/ \/ + / g, '/' )
77
+ const str = this . base . encoder . encode ( cid . multihash . bytes )
78
+
79
+ return [ this . path , str ] . filter ( Boolean ) . join ( '/' ) . replace ( / \/ \/ + / g, '/' )
68
80
}
69
81
70
82
/**
@@ -212,7 +224,7 @@ export class S3Blockstore extends BaseBlockstore {
212
224
}
213
225
214
226
// Remove the path from the key
215
- const cid = CID . parse ( d . Key . slice ( ( this . path ?? '' ) . length ) )
227
+ const cid = CID . decode ( this . base . decoder . decode ( d . Key . slice ( ( this . path ?? '' ) . length ) ) )
216
228
217
229
yield {
218
230
cid,
0 commit comments