Skip to content

Commit

Permalink
feat: add style for get object url
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 committed May 30, 2024
1 parent d711c14 commit 7954516
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
8 changes: 7 additions & 1 deletion adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export abstract class Adapter {
abstract getObjectInfo(region: string, object: StorageObject): Promise<ObjectInfo>;
abstract getObjectHeader(region: string, object: StorageObject, domain?: Domain): Promise<ObjectHeader>;
abstract getObject(region: string, object: StorageObject, domain?: Domain): Promise<ObjectGetResult>;
abstract getObjectURL(region: string, object: StorageObject, domain?: Domain, deadline?: Date): Promise<URL>;
abstract getObjectURL(
region: string,
object: StorageObject,
domain?: Domain,
deadline?: Date,
style?: 'path' | 'virtualHost' | 'bucketEndpoint'
): Promise<URL>;
abstract getObjectStream(s3RegionId: string, object: StorageObject, domain?: Domain, option?: GetObjectStreamOption): Promise<Readable>;
abstract putObject(
region: string,
Expand Down
5 changes: 5 additions & 0 deletions kodo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ export class Kodo implements Adapter {
object: StorageObject,
domain?: Domain,
deadline?: Date,
style: 'path' | 'virtualHost' | 'bucketEndpoint' = 'bucketEndpoint',
): Promise<URL> {
if (!domain) {
let domains = await this._listDomains(s3RegionId, object.bucket);
Expand All @@ -613,6 +614,10 @@ export class Kodo implements Adapter {
domain = domains[0];
}

if (style !== 'bucketEndpoint') {
throw new Error('Only support "bucketEndpoint" style for now');
}

let url = new URL(`${domain.protocol}://${domain.name}`);
url.pathname = encodeURI(object.key);
if (domain.private || domain.protected) {
Expand Down
34 changes: 25 additions & 9 deletions s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export class S3 extends Kodo {
protected clientsLock = new AsyncLock();
protected listKodoBucketsPromise?: Promise<Bucket[]>;

private async getClient(s3RegionId?: string): Promise<AWS.S3> {
const cacheKey = s3RegionId ?? '';
private async getClient(s3RegionId?: string, s3ForcePathStyle = true): Promise<AWS.S3> {
const cacheKey = [s3RegionId ?? '', s3ForcePathStyle ? 's3ForcePathStyle' : ''].join(':');
if (this.clients[cacheKey]) {
return this.clients[cacheKey];
}
Expand Down Expand Up @@ -98,7 +98,8 @@ export class S3 extends Kodo {
agent: s3IdEndpoint.s3Endpoint.startsWith('https://')
? HttpClient.httpsKeepaliveAgent
: HttpClient.httpKeepaliveAgent,
}
},
s3ForcePathStyle
});
});
this.clients[cacheKey] = client;
Expand Down Expand Up @@ -624,10 +625,20 @@ export class S3 extends Kodo {
);
}

async getObjectURL(s3RegionId: string, object: StorageObject, domain?: Domain, deadline?: Date): Promise<URL> {
async getObjectURL(
s3RegionId: string,
object: StorageObject,
domain?: Domain,
deadline?: Date,
style: 'path' | 'virtualHost' | 'bucketEndpoint' = 'path',
): Promise<URL> {
let s3Promise: Promise<AWS.S3>;
// if domain is not undefined, use the domain, else use the default s3 endpoint
const s3Promise: Promise<AWS.S3> = domain
? Promise.resolve(new AWS.S3({
if (domain) {
if (style !== 'bucketEndpoint') {
throw new Error('Custom S3 endpoint only support "bucketEndpoint" style');
}
s3Promise = Promise.resolve(new AWS.S3({
apiVersion: '2006-03-01',
region: s3RegionId,
endpoint: `${domain.protocol}://${domain.name}`,
Expand All @@ -636,9 +647,14 @@ export class S3 extends Kodo {
secretAccessKey: this.adapterOption.secretKey,
},
signatureVersion: 'v4',
s3BucketEndpoint: true,
}))
: this.getClient(s3RegionId);
s3BucketEndpoint: true, // use bucketEndpoint style
}));
} else {
if (style === 'bucketEndpoint') {
throw new Error('Default S3 endpoint not support "bucketEndpoint" style');
}
s3Promise = this.getClient(s3RegionId, style === 'path');
}
const [s3, bucketId] = await Promise.all([
s3Promise,
this.fromKodoBucketNameToS3BucketId(object.bucket),
Expand Down

0 comments on commit 7954516

Please sign in to comment.