Aliyun OSS Node.js Client

2.5.1 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates initializing the OSS client, uploading a file, retrieving its content, and then deleting the object using async/await.

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();

view raw JSON →