Skip to content

Commit

Permalink
feat(bos): 实现BOS获取单个对象 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
otakustay committed Sep 3, 2022
1 parent 2550e46 commit 594ae59
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
"lint": "eslint src",
"build": "tsc -p tsconfig.build.json",
"ci": "yarn install --immutable && npm run lint && npm run build",
"release": "standard-version"
"release": "standard-version",
"deploy": "npm publish --access public"
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/otakustay/bce-sdk.git"
Expand Down
27 changes: 27 additions & 0 deletions src/bos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,31 @@ export class BosClient {
);
return response;
}

async getObject(bucketName: string, key: string) {
const response = await this.http.text(
'GET',
`/${key}`,
{headers: {host: `${bucketName}.${this.hostBase}`}}
);
return response;
}

async getObjectAsBlob(bucketName: string, key: string) {
const response = await this.http.blob(
'GET',
`/${key}`,
{headers: {host: `${bucketName}.${this.hostBase}`}}
);
return response;
}

async getObjectAsStream(bucketName: string, key: string) {
const response = await this.http.stream(
'GET',
`/${key}`,
{headers: {host: `${bucketName}.${this.hostBase}`}}
);
return response;
}
}
17 changes: 17 additions & 0 deletions src/http.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Blob} from 'node:buffer';
import {ReadableStream} from 'node:stream/web';
import {fetch} from 'undici';
import {fromPairs} from 'ramda';
import {Authorization, BceCredential} from './authorization.js';
Expand Down Expand Up @@ -51,6 +53,21 @@ export class Http {
return {headers, body: await response.text()};
}

async blob(method: string, url: string, options: RequestOptions): Promise<ClientResponse<Blob>> {
const {headers, response} = await this.request(method, url, options);
return {headers, body: await response.blob()};
}

async stream(method: string, url: string, options: RequestOptions): Promise<ClientResponse<ReadableStream>> {
const {headers, response} = await this.request(method, url, options);

if (response.body) {
return {headers, body: response.body};
}

throw new Error(`No stream body in response of ${method} ${url}`);
}

private async request(method: string, url: string, options: RequestOptions) {
const {headers, params} = options;
const timestamp = stringifyDate(new Date());
Expand Down

0 comments on commit 594ae59

Please sign in to comment.