Aliyun OSS Node.js Client
The `oss-client` package provides a robust Node.js client for Alibaba Cloud's Object Storage Service (OSS), functionally similar to Amazon S3. The current stable version is 2.5.1, released in August 2025. This library maintains an active release cadence, with minor feature updates every few months and bug fixes as needed. It leverages modern JavaScript features, exclusively using `async/await` for all operations, making asynchronous programming straightforward. A key differentiator is its strong TypeScript support and ESM-first approach since version 2.0.0, catering to contemporary Node.js development practices. It is specifically designed for Aliyun OSS, offering comprehensive object manipulation capabilities including put, get, delete, stream handling, URL generation, and ACL management, along with support for bucket and object tagging.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module ... oss-client/index.js from ... not supported.
cause Attempting to use `require()` to import `oss-client` in a CommonJS context when the package is primarily designed for ESM in v2.x.fixEnsure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import { OSSObject } from 'oss-client';`. Alternatively, for older Node.js or strict CommonJS environments, consider `oss-client` v1.x. -
TypeError: ossObject.put is not a function
cause Incorrectly instantiating the `OSSObject` client or trying to call a method that doesn't exist or is not exposed.fixVerify that `new OSSObject({...})` is correctly called and that `OSSObject` is properly imported. Check the method signature and available methods against the documentation for your `oss-client` version. -
Error: signature is invalid
cause Authentication failure due to incorrect `accessKeyId`, `accessKeySecret`, or an expired `stsToken` (if using v1.x or a custom STS solution). This can also happen if the region or bucket name is wrong.fixDouble-check your `accessKeyId`, `accessKeySecret`, `region`, and `bucket` configurations for typos. Ensure the keys have the necessary permissions for the operations being performed. If using temporary credentials, verify their validity.
Warnings
- breaking Version 2.0.0 introduced significant breaking changes, dropping support for Node.js versions below 16.0.0. Projects on older Node.js versions must either upgrade Node.js or remain on `oss-client` v1.x.
- breaking With v2.0.0, support for `stsToken`, `headerEncoding`, and dedicated `Bucket` and `Image Client` APIs was removed. These functionalities require alternative implementations or SDKs.
- breaking The `urllib` dependency was upgraded to version 4.x in `oss-client` v2.4.0. This might introduce breaking changes in `urllib` itself if your project directly or indirectly relied on older `urllib` versions.
- gotcha The `getObjectUrl` method was deprecated in favor of `generateObjectUrl` in v2.0.1. While `getObjectUrl` still exists as an alias, using the new method is recommended for future compatibility.
- gotcha When using `putStream`, ensure the stream is correctly handled and closed. Improper stream management can lead to resource leaks or incomplete uploads, especially with large files.
Install
-
npm install oss-client -
yarn add oss-client -
pnpm add oss-client
Imports
- OSSObject
const { OSSObject } = require('oss-client');import { OSSObject } from 'oss-client'; - ClientOptions
import type { ClientOptions } from 'oss-client'; - PutObjectOptions
import type { PutObjectOptions } from 'oss-client';
Quickstart
import { OSSObject } from 'oss-client';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
const ossObject = new OSSObject({
region: process.env.OSS_REGION ?? 'oss-cn-hangzhou',
endpoint: process.env.OSS_ENDPOINT ?? 'https://oss-cn-hangzhou.aliyuncs.com',
accessKeyId: process.env.OSS_ACCESS_KEY_ID ?? 'YOUR_ACCESS_KEY_ID',
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET ?? 'YOUR_ACCESS_KEY_SECRET',
bucket: process.env.OSS_BUCKET_NAME ?? 'your-bucket-name',
});
async function uploadFile() {
const localFilePath = join(__dirname, 'test-file.txt');
// For demonstration, create a dummy file if it doesn't exist
try {
readFileSync(localFilePath);
} catch (error) {
require('node:fs').writeFileSync(localFilePath, 'Hello, Aliyun OSS!');
}
const remoteObjectName = 'my-test-object.txt';
try {
const result = await ossObject.put(remoteObjectName, localFilePath);
console.log(`Successfully uploaded: ${result.name} to ${result.url}`);
const downloadResult = await ossObject.get(remoteObjectName);
console.log(`Downloaded content: ${downloadResult.content?.toString()}`);
await ossObject.delete(remoteObjectName);
console.log(`Successfully deleted: ${remoteObjectName}`);
} catch (error) {
console.error('OSS operation failed:', error);
}
}
uploadFile();