The OSS client is the main entry point for all interactions with Alibaba Cloud Object Storage Service. It provides authentication, connection management, and serves as the base for all other operations.
function OSS(options: ClientOptions, ctx?: ClientContext): OSSClient;
interface ClientOptions {
region?: string;
accessKeyId: string;
accessKeySecret: string;
stsToken?: string;
bucket?: string;
endpoint?: string;
internal?: boolean;
secure?: boolean;
timeout?: number | number[];
cname?: boolean;
isRequestPay?: boolean;
agent?: Agent;
httpsAgent?: Agent;
maxSockets?: number;
retryMax?: number;
retryDelay?: number;
urllib?: any;
headers?: Record<string, string>;
headerEncoding?: string;
cloudBoxId?: string;
refreshSTSToken?: () => Promise<STSCredentials>;
requestErrorRetryHandle?: (error: Error, retryCount: number) => boolean;
}
interface STSCredentials {
accessKeyId: string;
accessKeySecret: string;
stsToken: string;
}
interface ClientContext {
[key: string]: any;
}const OSS = require('ali-oss');
// Basic configuration
const client = new OSS({
region: 'oss-cn-hangzhou',
accessKeyId: 'your-access-key-id',
accessKeySecret: 'your-access-key-secret',
bucket: 'your-bucket-name'
});
// Configuration with custom endpoint
const client = new OSS({
accessKeyId: 'your-access-key-id',
accessKeySecret: 'your-access-key-secret',
endpoint: 'https://oss-cn-beijing.aliyuncs.com',
bucket: 'your-bucket-name'
});
// Configuration with STS token
const client = new OSS({
region: 'oss-cn-hangzhou',
accessKeyId: 'your-sts-access-key',
accessKeySecret: 'your-sts-access-secret',
stsToken: 'your-sts-token',
bucket: 'your-bucket-name'
});interface AccessKeyConfig {
region: string;
accessKeyId: string;
accessKeySecret: string;
bucket?: string;
}interface STSTokenConfig {
region: string;
accessKeyId: string;
accessKeySecret: string;
stsToken: string;
bucket?: string;
}interface AgentConfig {
agent?: Agent;
httpsAgent?: Agent;
maxSockets?: number;
timeout?: number | number[];
}const http = require('http');
const https = require('https');
const client = new OSS({
region: 'oss-cn-hangzhou',
accessKeyId: 'your-access-key-id',
accessKeySecret: 'your-access-key-secret',
agent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
maxSockets: 20,
timeout: [60000, 120000] // [connection timeout, response timeout]
});interface RetryConfig {
retryMax?: number;
retryDelay?: number;
}const client = new OSS({
region: 'oss-cn-hangzhou',
accessKeyId: 'your-access-key-id',
accessKeySecret: 'your-access-key-secret',
retryMax: 3,
retryDelay: 1000
});signature(stringToSign: string): string;
authorization(method: string, resource: string, subres?: string, headers?: Record<string, string>): string;
authorizationV4(method: string, requestParams: V4RequestParams, bucketName?: string, objectName?: string, additionalHeaders?: Record<string, string>): string;
interface V4RequestParams {
headers: Record<string, string>;
queries: Record<string, string>;
date: Date;
}async request(params: RequestParams): Promise<RequestResult>;
interface RequestParams {
method: string;
uri: string;
headers?: Record<string, string>;
content?: any;
timeout?: number | number[];
ctx?: any;
}
interface RequestResult {
status: number;
headers: Record<string, string>;
data?: any;
res: any;
}parseXML(str: string): Promise<any>;
requestError(result: RequestResult): Error;
setSLDEnabled(enable: boolean): void;
checkBrowserAndVersion(name: string, version: string): boolean;// Browser global
const client = new OSS({
region: 'oss-cn-hangzhou',
accessKeyId: 'your-access-key-id',
accessKeySecret: 'your-access-key-secret',
bucket: 'your-bucket-name'
});
// Additional browser properties
interface BrowserClientOptions extends ClientOptions {
// Browser-specific options are handled automatically
}For browser usage, ensure your OSS bucket has appropriate CORS configuration:
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>static initOptions(options: ClientOptions): ClientOptions;
static ImageClient: typeof ImageClient;
static ClusterClient: typeof ClusterClient;
static STS: typeof STS;interface OSSError extends Error {
name: string;
message: string;
code?: string;
status?: number;
headers?: Record<string, string>;
requestId?: string;
hostId?: string;
}Common error scenarios: