Simple download (Harmony SDK)

更新时间:
复制 MD 格式

Download an object from an OSS bucket directly to memory or to a local file using the OSS SDK for HarmonyOS.

Note This guide covers direct programmatic downloads. To generate a download link for browser or end-user access, use a pre-signed URL instead. For more information, see Authorized access.

Prerequisites

Before you begin, ensure that you have:

Download to memory

Use getObject without a writeStream to receive the object content as an ArrayBuffer in res.data.

import Client, { RequestError } from '@aliyun/oss';

const client = new Client({
  accessKeyId: 'yourAccessKeyId',         // AccessKey ID obtained from Security Token Service (STS)
  accessKeySecret: 'yourAccessKeySecret', // AccessKey secret obtained from STS
  securityToken: 'yourSecurityToken',     // STS token obtained from STS
  region: 'oss-cn-hangzhou',             // Region where the bucket is located
});

const bucket = 'yourBucketName';
const key = 'yourObjectName';

const getObject = async () => {
  try {
    const res = await client.getObject({ bucket, key });

    // res.data contains the object content as an ArrayBuffer
    const buf: ArrayBuffer = res.data!;
    console.log(JSON.stringify(res));
  } catch (err) {
    if (err instanceof RequestError) {
      console.log('High-level error code: ', err.code);
      console.log('message: ', err.message);
      console.log('Request ID: ', err.requestId);
      console.log('HTTP status code: ', err.status);
      console.log('Fine-grained error code: ', err.ec);
    } else {
      console.log('unknown error: ', err);
    }
  }
};

getObject();

Replace the placeholders before running:

PlaceholderDescriptionExample
yourAccessKeyIdAccessKey ID obtained from STSLTAI5tXxx
yourAccessKeySecretAccessKey secret obtained from STSxXxXxXxx
yourSecurityTokenSTS token obtained from STS
oss-cn-hangzhouRegion where the bucket is locatedoss-cn-hangzhou
yourBucketNameName of the bucketexamplebucket
yourObjectNameKey of the object to downloadimages/photo.jpg

Download to a local file

Pass a writeStream to getObject to stream the object content directly into a file. When using this mode, res.data is undefined—the content goes to the stream instead.

import Client, { RequestError } from '@aliyun/oss';
import { fileIo as fs } from '@kit.CoreFileKit';

const client = new Client({
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  securityToken: 'yourSecurityToken',
  region: 'oss-cn-hangzhou',
});

const bucket = 'yourBucketName';
const key = 'yourObjectName';

const downloadToFile = async () => {
  const writeStream = await fs.createStream('yourFilePath', 'w'); // Local file path to write to

  try {
    const res = await client.getObject({
      bucket,
      key,
      writeStream, // Object content is written to this stream; res.data is undefined
    });
    console.log(JSON.stringify(res));
  } catch (err) {
    if (err instanceof RequestError) {
      console.log('High-level error code: ', err.code);
      console.log('message: ', err.message);
      console.log('Request ID: ', err.requestId);
      console.log('HTTP status code: ', err.status);
      console.log('Fine-grained error code: ', err.ec);
    } else {
      console.log('unknown error: ', err);
    }
  } finally {
    await writeStream.close(); // Always close the write stream
  }
};

downloadToFile();

Replace the placeholders before running:

PlaceholderDescriptionExample
yourAccessKeyIdAccessKey ID obtained from STSLTAI5tXxx
yourAccessKeySecretAccessKey secret obtained from STSxXxXxXxx
yourSecurityTokenSTS token obtained from STS
oss-cn-hangzhouRegion where the bucket is locatedoss-cn-hangzhou
yourBucketNameName of the bucketexamplebucket
yourObjectNameKey of the object to downloadimages/photo.jpg
yourFilePathAbsolute path of the local file to create/data/storage/el2/base/files/photo.jpg