すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:単一接続の帯域幅制限

最終更新日:Nov 11, 2024

このトピックでは、オブジェクトのアップロードまたはダウンロード要求にパラメーターを追加して、アップロードまたはダウンロード帯域幅の制限を設定する方法について説明します。 これにより、他のアプリケーションに十分な帯域幅が確保されます。

単純なアップロードとダウンロードのための帯域幅調整の設定

次のコードでは、オブジェクトの単純なアップロードとダウンロードの帯域幅調整を設定する方法の例を示します。

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,
  authorizationV4: true,
  // Specify the name of your bucket. 
  bucket: 'yourbucketname'
});

// Configure the speed limit by using the request header. 
const headers = {  
 // Set the minimum bandwidth to 100 KB/s. 
 'x-oss-traffic-limit': 8 * 1024 * 100 
}

// Configure bandwidth throttling for the object to be uploaded. 
async function put() {
  // Specify the path of the object. 
  const filePath = 'D:\\localpath\\examplefile.txt'; 
  // Create a file stream for the object. 
  const fileStream = fs.createReadStream(filePath); 
  const result = await client.putStream('file-name', fileStream, {
    // Set the request header properly. 
    headers, 
    // Set the default timeout period to 60000. Unit: milliseconds. If the duration of an object upload exceeds the timeout period, an exception is thrown. When you configure bandwidth throttling for object uploads, modify the timeout period. 
    timeout: 60000 
  });
  console.log(result);
}

put()

// Configure bandwidth throttling for the object to be downloaded. 
async function get() {
  const result = await client.get('file name', {
    headers,
    // Set the default timeout period to 60000. Unit: milliseconds. If the duration of an object download exceeds the timeout period, an exception is thrown. When you configure bandwidth throttling for object downloads, modify the timeout period. 
    timeout: 60000 
  })
  console.log(result)
}

get()

署名付きURLを使用してオブジェクトをアップロードまたはダウンロードする場合の帯域幅調整の設定

次のコードでは、署名付きURLを使用してオブジェクトをアップロードまたはダウンロードする場合に、単一接続帯域幅スロットリングを構成する方法の例を示します。

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,
  authorizationV4: true,
  // Specify the name of your bucket. 
  bucket: 'yourbucketname',
});

// Configure bandwidth throttling for the object to be uploaded by using URL Query. 
async function putByQuery() {
  const url = client.signatureUrl('file name', {
    // Set the minimum bandwidth to 100 KB/s. 
    trafficLimit: 8 * 1024 * 100, 
    // Configure the PUT request. 
    method: 'PUT' 
  })
  // Specify the path of the object. 
  const filePath = 'D:\\localpath\\examplefile.txt'; 
  // Create a file stream for the object. 
  const fileStream = fs.createReadStream(filePath); 
  const result = await client.urllib.request(url, {
    method: 'PUT',
    // Specify the file stream as a parameter. 
    stream: fileStream, 
    // Set the default timeout period to 60000. Unit: milliseconds. We recommend that you modify the timeout period after you configure bandwidth throttling. Otherwise, the request fails. 
    timeout: 60000, 
  });

  console.log(result)
}

putByQuery()

// Configure bandwidth throttling for the object to be downloaded by using URL Query. 
async function getByQuery() {
  const url = client.signatureUrl('file name', {
    // Set the minimum bandwidth to 100 KB/s. 
    trafficLimit: 8 * 1024 * 100, 
  })

  const result = await client.urllib.request(url, {
    // Set the default timeout period to 60000. Unit: milliseconds. We recommend that you modify the timeout period after you configure bandwidth throttling. Otherwise, the request fails. 
    timeout: 60000, 
  });

  console.log(result)
}

getByQuery()

関連ドキュメント

シングル接続帯域幅調整の完全なサンプルコードについては、『GitHub』をご参照ください。