-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: 放弃对浏览器的兼容
- Loading branch information
Showing
7 changed files
with
191 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {Http} from '../shared/index.js'; | ||
import {BosObjectClient} from './object.js'; | ||
|
||
export interface ListObjectOptions { | ||
delimiter?: string; | ||
marker?: string; | ||
maxKeys?: number; | ||
prefix?: string; | ||
} | ||
|
||
export interface ObjectOwner { | ||
id: string; | ||
displayName: string; | ||
} | ||
|
||
export interface ObjectContent { | ||
key: string; | ||
lastModified: string; | ||
eTag: string; | ||
size: number; | ||
storageClass: 'STANDARD' | 'COLD'; | ||
owner: ObjectOwner; | ||
} | ||
|
||
export interface CommonPrefix { | ||
prefix: string; | ||
} | ||
|
||
export interface ListObjectResponse { | ||
name: string; | ||
prefix: string; | ||
delimiter: string; | ||
commonPrefixes?: CommonPrefix[]; | ||
isTruncated: boolean; | ||
maxKeys: number; | ||
marker: string; | ||
nextMarker: string; | ||
contents: ObjectContent[]; | ||
} | ||
|
||
export class BosBucketClient { | ||
private readonly http: Http; | ||
|
||
constructor(http: Http, hostBase: string, bucketName: string) { | ||
this.http = http.withHeaders({host: `${bucketName}.${hostBase}`}); | ||
} | ||
|
||
withObject(objectKey: string) { | ||
return new BosObjectClient(this.http, objectKey); | ||
} | ||
|
||
async listObjects(options?: ListObjectOptions) { | ||
const response = await this.http.json<ListObjectResponse>( | ||
'GET', | ||
'/', | ||
{ | ||
params: { | ||
delimiter: options?.delimiter, | ||
marker: options?.marker, | ||
maxKeys: options?.maxKeys, | ||
prefix: options?.prefix, | ||
}, | ||
} | ||
); | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,56 @@ | ||
import type {ReadStream} from 'node:fs'; | ||
import {RegionClientOptions, Http} from '../shared/index.js'; | ||
import {BosBucketClient, ListObjectOptions} from './bucket.js'; | ||
import {ObjectBody, PutObjectOptions} from './object.js'; | ||
|
||
// https://cloud.baidu.com/doc/BOS/s/Rjwvysdnp | ||
|
||
export interface ListObjectOptions { | ||
delimiter?: string; | ||
marker?: string; | ||
maxKeys?: number; | ||
prefix?: string; | ||
} | ||
|
||
export interface ObjectOwner { | ||
id: string; | ||
displayName: string; | ||
} | ||
|
||
export interface ObjectContent { | ||
key: string; | ||
lastModified: string; | ||
eTag: string; | ||
size: number; | ||
storageClass: 'STANDARD' | 'COLD'; | ||
owner: ObjectOwner; | ||
} | ||
|
||
export interface CommonPrefix { | ||
prefix: string; | ||
} | ||
export type {ObjectBody, PutObjectOptions} from './object.js'; | ||
export type {CommonPrefix, ListObjectOptions, ListObjectResponse, ObjectContent, ObjectOwner} from './bucket.js'; | ||
|
||
export interface ListObjectResponse { | ||
name: string; | ||
prefix: string; | ||
delimiter: string; | ||
commonPrefixes?: CommonPrefix[]; | ||
isTruncated: boolean; | ||
maxKeys: number; | ||
marker: string; | ||
nextMarker: string; | ||
contents: ObjectContent[]; | ||
} | ||
|
||
export interface PutObjectOptions { | ||
headers?: Record<string, string>; | ||
} | ||
|
||
export type ObjectBody = string | Blob | ArrayBuffer | ReadStream; | ||
// https://cloud.baidu.com/doc/BOS/s/Rjwvysdnp | ||
|
||
export type BosOptions = RegionClientOptions; | ||
|
||
export class BosClient { | ||
private readonly hostBase: string; | ||
private readonly http: Http; | ||
protected readonly hostBase: string; | ||
protected readonly http: Http; | ||
|
||
constructor(options: BosOptions) { | ||
this.hostBase = `${options.region}.bcebos.com`; | ||
this.http = Http.fromEndpoint(this.hostBase, options); | ||
} | ||
|
||
withBucket(bucketName: string) { | ||
return new BosBucketClient(this.http, this.hostBase, bucketName); | ||
} | ||
|
||
withObject(bucketName: string, objectKey: string) { | ||
return this.withBucket(bucketName).withObject(objectKey); | ||
} | ||
|
||
async listObjects(bucketName: string, options?: ListObjectOptions) { | ||
const response = await this.http.json<ListObjectResponse>( | ||
'GET', | ||
'/', | ||
{ | ||
params: { | ||
delimiter: options?.delimiter, | ||
marker: options?.marker, | ||
maxKeys: options?.maxKeys, | ||
prefix: options?.prefix, | ||
}, | ||
headers: { | ||
'content-type': 'application/json', | ||
host: `${bucketName}.${this.hostBase}`, | ||
}, | ||
} | ||
); | ||
return response; | ||
return this.withBucket(bucketName).listObjects(options); | ||
} | ||
|
||
async getObject(bucketName: string, key: string) { | ||
const response = await this.http.text( | ||
'GET', | ||
`/${key}`, | ||
{headers: {host: `${bucketName}.${this.hostBase}`}} | ||
); | ||
return response; | ||
return this.withObject(bucketName, key).get(); | ||
} | ||
|
||
async getObjectAsBlob(bucketName: string, key: string) { | ||
const response = await this.http.blob( | ||
'GET', | ||
`/${key}`, | ||
{headers: {host: `${bucketName}.${this.hostBase}`}} | ||
); | ||
return response; | ||
return this.withObject(bucketName, key).getAsBlob(); | ||
} | ||
|
||
async getObjectAsStream(bucketName: string, key: string) { | ||
const response = await this.http.stream( | ||
'GET', | ||
`/${key}`, | ||
{headers: {host: `${bucketName}.${this.hostBase}`}} | ||
); | ||
return response; | ||
return this.withObject(bucketName, key).getAsStream(); | ||
} | ||
|
||
async putObject(bucketName: string, key: string, body: ObjectBody, options?: PutObjectOptions) { | ||
const response = await this.http.noContent( | ||
'PUT', | ||
`/${key}`, | ||
{ | ||
body, | ||
headers: { | ||
...options?.headers, | ||
host: `${bucketName}.${this.hostBase}`, | ||
}, | ||
} | ||
); | ||
return response; | ||
return this.withObject(bucketName, key).put(body, options); | ||
} | ||
|
||
async putObjectFromFile(bucketName: string, key: string, file: string, options?: PutObjectOptions) { | ||
const fs = await import('node:fs'); | ||
const stream = fs.createReadStream(file); | ||
const response = await this.putObject(bucketName, key, stream, options); | ||
return response; | ||
return this.withObject(bucketName, key).putFromFile(file, options); | ||
} | ||
|
||
async deleteObject(bucketName: string, key: string) { | ||
const response = await this.http.noContent( | ||
'DELETE', | ||
`/${key}`, | ||
{headers: {host: `${bucketName}.${this.hostBase}`}} | ||
); | ||
return response; | ||
return this.withObject(bucketName, key).delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import fs from 'node:fs'; | ||
import {Readable} from 'node:stream'; | ||
import {Http} from '../shared/index.js'; | ||
|
||
export interface PutObjectOptions { | ||
headers?: Record<string, string>; | ||
} | ||
|
||
export type ObjectBody = string | Blob | ArrayBuffer | Readable; | ||
|
||
export class BosObjectClient { | ||
private readonly http: Http; | ||
private readonly objectKey: string; | ||
|
||
constructor(http: Http, objectKey: string) { | ||
this.http = http; | ||
this.objectKey = objectKey; | ||
} | ||
|
||
async get() { | ||
const response = await this.http.text('GET', `/${this.objectKey}`); | ||
return response; | ||
} | ||
|
||
async getAsBlob() { | ||
const response = await this.http.blob('GET', `/${this.objectKey}`); | ||
return response; | ||
} | ||
|
||
async getAsStream() { | ||
const response = await this.http.stream('GET', `/${this.objectKey}`); | ||
return { | ||
headers: response.headers, | ||
// @ts-expect-error 都是`WebStream`,只是Node的类型不兼容,实际是能用的 | ||
body: Readable.fromWeb(response.body), | ||
}; | ||
} | ||
|
||
async put(body: ObjectBody, options?: PutObjectOptions) { | ||
const response = await this.http.noContent( | ||
'PUT', | ||
`/${this.objectKey}`, | ||
{ | ||
body, | ||
headers: { | ||
...options?.headers, | ||
}, | ||
} | ||
); | ||
return response; | ||
} | ||
|
||
async putFromFile(file: string, options?: PutObjectOptions) { | ||
const stream = fs.createReadStream(file); | ||
const response = await this.put(stream, options); | ||
return response; | ||
} | ||
|
||
async delete() { | ||
const response = await this.http.noContent('DELETE', `/${this.objectKey}`); | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
export * from './shared/index.js'; | ||
export { | ||
RegionClientOptions, | ||
BceCredential, | ||
BceCredentialContext, | ||
ClientResponse, | ||
ClientResponseNoContent, | ||
RequestOptions, | ||
RequestError, | ||
isRequestError, | ||
} from './shared/index.js'; | ||
export * from './bls/index.js'; | ||
export * from './bos/index.js'; | ||
export * from './sts/index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.