Este tópico descreve como adicionar parâmetros a uma solicitação de upload ou download de objeto para definir o limite de largura de banda. Essa configuração garante largura de banda suficiente para outras aplicações.
Configure a limitação de largura de banda para uploads e downloads simples
O código a seguir exemplifica como configurar a limitação de largura de banda para uploads e downloads simples de objetos:
const OSS = require('ali-oss')
const fs = require('fs')
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()
Configure a limitação de largura de banda ao usar URLs assinadas para upload ou download de objetos
O exemplo a seguir demonstra como aplicar a limitação de largura de banda por conexão única durante o upload ou download de objetos com URLs assinadas:
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()
Referências
Para obter o código de exemplo completo sobre limitação de largura de banda por conexão única, visite o GitHub.