All Products
Search
Document Center

Object Storage Service:Conditional download

Last Updated:Oct 18, 2023

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({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket'
});

async function main() {
  try {
    // Upload an object named exampleobject.txt. 
    await client.put("exampleobject.txt", Buffer.from("contenttest"));
    // Set the If-Modified-Since header in the request. If the value of this header is earlier than the time when the uploaded object is last modified, the object is downloaded. 
    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 value of the If-Modified-Since header is equal to or later than the time when the uploaded object is last modified, OSS returns 304 Not Modified. 
    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({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket'
});

async function main() {
  // Upload an object named exampleobject.txt. 
  await client.put('exampleobject.txt', Buffer.from('contenttest'))

  // Set the If-Unmodified-Since header in the request. If the value of this header is equal to or later than the time when the uploaded object is last modified, the object is downloaded. 
  let result = await client.get("exampleobject.txt", {
    headers: {
      "If-Unmodified-Since": new Date().toGMTString(),
    },
  });
  console.log(result.content.toString() === "contenttest");
  console.log(result.res.status === 200);

  // If the value of the If-Unmodified-Since header is earlier than the time when the uploaded object is last modified, OSS returns 412 Precondition Failed. 
  try {
    await client.get("exampleobject.txt", {
      headers: {
        "If-Unmodified-Since": new Date().toGMTString(),
      },
    });
  } 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({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket'
});

async function main() {
  // Upload an object named exampleobject.txt and obtain the ETag of the object. 
  await client.put('exampleobject.txt', Buffer.from('contenttest'))

  // Set the If-Match header to the ETag value. If the specified ETag value matches the ETag of the object, the object is downloaded. 
  let result = await client.get("exampleobject.txt", {
    headers: {
      "If-Unmodified-Since": new Date().toGMTString(),
    },
  });
  console.log(result.content.toString() === "contenttest");
  console.log(result.res.status === 200);

  // If the specified ETag value does not match the ETag of the object, OSS returns 412 Precondition Failed. 
  try {
    await client.get("exampleobject.txt", {
      headers: {
        "If-Unmodified-Since": new Date().toGMTString(),
      },
    });
  } 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({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'examplebucket'
});

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

    // Set the If-Match header to the ETag value. If the specified ETag value does not match the ETag of the object, the object is downloaded. 
    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 value matches the ETag of the object, OSS returns 304 Not Modified. 
    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.