All Products
Search
Document Center

Object Storage Service:Conditional download (Node.js SDK)

Last Updated:Nov 29, 2025

You can specify conditions when you download objects. If the specified conditions are met, the objects can be downloaded. If the specified conditions are not met, an error is returned and the objects cannot be downloaded.

Download objects that are modified earlier than the specified time

The following sample code provides an example on how to download objects that are modified earlier than the specified time:

const OSS = require('ali-oss');

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket'
});

async function main() {
  try {
    // Upload a file named exampleobject.txt to the destination bucket. You can customize the file content.
    await client.put("exampleobject.txt", Buffer.from("contenttest"));
    // Specify a time in the If-Modified-Since request header. The object is downloaded if the specified time is earlier than the actual modification time of the object.
    let result = await client.get("exampleobject.txt", {
      headers: {
        "If-Modified-Since": new Date("1970-01-01").toGMTString(),
      },
    });
    console.log(result.content.toString() === "contenttest");
    console.log(result.res.status === 200);

    // If the specified time is the same as or later than the actual modification time of the object, a 304 Not Modified error is returned.
    result = await client.get("exampleobject.txt", {
      headers: {
        "If-Modified-Since": new Date().toGMTString(),
      },
    });
    console.log(result.content.toString() === "");
    console.log(result.res.status === 304);
  } catch (e) {
    console.log(e.code === "Not Modified");
  }
}

main();

For more information about the naming conventions for buckets, see Bucket. For more information about the naming conventions for objects, see Object.

Download objects that are modified no earlier than the specified time

The following sample code provides an example on how to download objects that are modified no earlier than the specified time:

const OSS = require('ali-oss');

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket'
});

async function main() {
  // Upload a file named exampleobject.txt to the destination bucket and obtain the ETag of the file.
  let {res: {headers: {etag}}}  = await client.put('exampleobject.txt', Buffer.from('contenttest'));

  // Pass the ETag in the If-Match request header. The object is downloaded if the specified ETag matches the ETag of the object.
  let result = await client.get("exampleobject.txt", {
    headers: {
      "If-Match": etag,
    },
  });
  console.log(result.content.toString() === "contenttest");
  console.log(result.res.status === 200);

  // If the specified ETag does not match the ETag of the object, a 412 Precondition Failed error is returned.
  try {
    await client.get("exampleobject.txt", {
      headers: {
        "If-Match": etag,
      },
    });
  } catch (e) {
    console.log(e.status === 412);
    console.log(e.code === "PreconditionFailed");
  }
}

main();

Download objects whose ETags match the specified ETag values

The ETag of an object can be used to check whether the object content changes. The following sample code provides an example on how to download objects whose ETags match the specified ETag values:

const OSS = require('ali-oss');

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket'
});

async function main() {
  // Upload a file named exampleobject.txt to the destination bucket and obtain the ETag of the file.
  let {res: {headers: {etag}}}  = await client.put('exampleobject.txt', Buffer.from('contenttest'));

  // Pass the ETag in the If-Match request header. The object is downloaded if the specified ETag matches the ETag of the object.
  let result = await client.get("exampleobject.txt", {
    headers: {
      "If-Match": etag,
    },
  });
  console.log(result.content.toString() === "contenttest");
  console.log(result.res.status === 200);

  // If the specified ETag does not match the ETag of the object, a 412 Precondition Failed error is returned.
  try {
    await client.get("exampleobject.txt", {
      headers: {
        "If-Match": etag,
      },
    });
  } catch (e) {
    console.log(e.status === 412);
    console.log(e.code === "PreconditionFailed");
  }
}

main();

Download objects whose ETags do not match the specified ETag values

The following sample code provides an example on how to download objects whose ETags do not match the specified ETag values:

const OSS = require('ali-oss');

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket'
});

async function main() {
  try {
    // Upload a file named exampleobject.txt to the destination bucket and obtain the ETag of the file.
    let {
      res: {
        headers: { etag },
      },
    } = await client.put("exampleobject.txt", Buffer.from("contenttest"));

    // Pass the ETag in the If-None-Match request header. The object is downloaded if the specified ETag does not match the ETag of the object.
    let result = await client.get("exampleobject.txt", {
      headers: {
        "If-None-Match": etag,
      },
    });
    console.log(result.content.toString() === "contenttest");
    console.log(result.res.status === 200);

    // If the specified ETag matches the ETag of the object, a 304 Not Modified error is returned.
    result = await client.get("exampleobject.txt", {
      headers: {
        "If-None-Match": etag,
      },
    });
    console.log(result.content.toString() === "");
    console.log(result.res.status === 304);
  } catch (e) {
    console.log(e.code === "Not Modified");
  }
}

main();

References

For more information about the API operation that you can call to perform conditional download, see GetObject.