如何更改现有 azure cosmosdb 集合的吞吐量值?
How to change throughput value of existing azure cosmosdb collection?
我想使用剩余 API 减少现有 azure cosmos db 集合的吞吐量值,它给出了 bad/unauthorized 错误。请帮助我用正确的输入调用API。
https://docs.microsoft.com/en-us/rest/api/cosmos-db/replace-an-offer
您需要提供authorization header
type={typeoftoken}&ver={tokenversion}&sig={hashsignature}
从replaceThroughput方法开始
const replaceThroughput = async(req, res, next) => {
let collectionName = _.get(req, 'headers.collectionname');
let offerThroughput = _.get(req, 'headers.offerthroughput');
const container = await cosmosClient.database(process.env.COSMOS_DB_NAME).container(collectionName);
let containerResponse = await container.read().catch((error)=>{
let err = new Error();
err.statusCode = 404;
err.message = 'Collection not found';
next(err);
return;
});
let resourceId = _.get(containerResponse, 'body._rid', '');
console.log(resourceId)
let offerPayload = await getOfferPayload(resourceId);
_.set(offerPayload, 'content.offerThroughput', offerThroughput);
let offerResponse = await replaceOfferThroughput(offerPayload.id, offerPayload);
if (offerResponse.error) {
let err = new Error();
err.statusCode = 500;
err.message = 'Replace resource offer failed';
err.data = offerResponse.error;
next(err);
return;
} else {
res.end(JSON.stringify({'result': 'success', 'message': 'throughput updated', 'data': offerResponse}));
}
next();
};
const getOfferPayload = async (resourceId) => {
var dateString = new Date().toUTCString();
let authString = getAuthorizationTokenUsingMasterKey('GET','offers', '', dateString, process.env.COSMOS_DB_KEY);
const options = {
method: 'GET',
rejectUnauthorized: false,
uri: 'https://' + process.env.COSMOS_DB_NAME + '.documents.azure.com/offers',
headers: {
'x-ms-version': '2018-06-18',
'Authorization': authString,
'x-ms-date': dateString
}
};
try{
let response = await request(options);
response = JSON.parse(response);
response = _.filter(response.Offers, {offerResourceId: resourceId});
return response[0];
}catch(err){
return err;
}
};
const replaceOfferThroughput = async (offerResourceId, offerPayload) => {
var dateString = new Date().toUTCString();
let authString = getAuthorizationTokenUsingMasterKey('PUT','offers', offerResourceId, dateString, process.env.COSMOS_DB_KEY);
const options = {
method: 'PUT',
body: offerPayload,
json: true,
rejectUnauthorized: false,
uri: 'https://' + process.env.COSMOS_DB_NAME + '.documents.azure.com/offers/'+ offerResourceId,
headers: {
'x-ms-version': '2018-06-18',
'Authorization': authString,
'x-ms-date': dateString
}
};
try{
return await request(options);
}catch(err){
return err;
}
};
const getAuthorizationTokenUsingMasterKey = (verb, resourceType, resourceId, date, masterKey) => {
var key = Buffer.from(masterKey, "base64");
var text = (verb || "").toLowerCase() + "\n" +
(resourceType || "").toLowerCase() + "\n" +
(resourceId || "").toLowerCase() + "\n" +
date.toLowerCase() + "\n" +
"" + "\n";
var body = Buffer.from(text, "utf8");
var signature = crypto.createHmac("sha256", key).update(body).digest("base64");
var MasterToken = "master";
var TokenVersion = "1.0";
return encodeURIComponent("type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + signature);
}
创建连接
const CosmosClient = require("@azure/cosmos").CosmosClient;
cosmosClient = new CosmosClient({
endpoint: process.env.COSMOS_DB_URL,
auth: {
masterKey: process.env.COSMOS_DB_KEY
}
});
我想使用剩余 API 减少现有 azure cosmos db 集合的吞吐量值,它给出了 bad/unauthorized 错误。请帮助我用正确的输入调用API。
https://docs.microsoft.com/en-us/rest/api/cosmos-db/replace-an-offer
您需要提供authorization header
type={typeoftoken}&ver={tokenversion}&sig={hashsignature}
从replaceThroughput方法开始
const replaceThroughput = async(req, res, next) => {
let collectionName = _.get(req, 'headers.collectionname');
let offerThroughput = _.get(req, 'headers.offerthroughput');
const container = await cosmosClient.database(process.env.COSMOS_DB_NAME).container(collectionName);
let containerResponse = await container.read().catch((error)=>{
let err = new Error();
err.statusCode = 404;
err.message = 'Collection not found';
next(err);
return;
});
let resourceId = _.get(containerResponse, 'body._rid', '');
console.log(resourceId)
let offerPayload = await getOfferPayload(resourceId);
_.set(offerPayload, 'content.offerThroughput', offerThroughput);
let offerResponse = await replaceOfferThroughput(offerPayload.id, offerPayload);
if (offerResponse.error) {
let err = new Error();
err.statusCode = 500;
err.message = 'Replace resource offer failed';
err.data = offerResponse.error;
next(err);
return;
} else {
res.end(JSON.stringify({'result': 'success', 'message': 'throughput updated', 'data': offerResponse}));
}
next();
};
const getOfferPayload = async (resourceId) => {
var dateString = new Date().toUTCString();
let authString = getAuthorizationTokenUsingMasterKey('GET','offers', '', dateString, process.env.COSMOS_DB_KEY);
const options = {
method: 'GET',
rejectUnauthorized: false,
uri: 'https://' + process.env.COSMOS_DB_NAME + '.documents.azure.com/offers',
headers: {
'x-ms-version': '2018-06-18',
'Authorization': authString,
'x-ms-date': dateString
}
};
try{
let response = await request(options);
response = JSON.parse(response);
response = _.filter(response.Offers, {offerResourceId: resourceId});
return response[0];
}catch(err){
return err;
}
};
const replaceOfferThroughput = async (offerResourceId, offerPayload) => {
var dateString = new Date().toUTCString();
let authString = getAuthorizationTokenUsingMasterKey('PUT','offers', offerResourceId, dateString, process.env.COSMOS_DB_KEY);
const options = {
method: 'PUT',
body: offerPayload,
json: true,
rejectUnauthorized: false,
uri: 'https://' + process.env.COSMOS_DB_NAME + '.documents.azure.com/offers/'+ offerResourceId,
headers: {
'x-ms-version': '2018-06-18',
'Authorization': authString,
'x-ms-date': dateString
}
};
try{
return await request(options);
}catch(err){
return err;
}
};
const getAuthorizationTokenUsingMasterKey = (verb, resourceType, resourceId, date, masterKey) => {
var key = Buffer.from(masterKey, "base64");
var text = (verb || "").toLowerCase() + "\n" +
(resourceType || "").toLowerCase() + "\n" +
(resourceId || "").toLowerCase() + "\n" +
date.toLowerCase() + "\n" +
"" + "\n";
var body = Buffer.from(text, "utf8");
var signature = crypto.createHmac("sha256", key).update(body).digest("base64");
var MasterToken = "master";
var TokenVersion = "1.0";
return encodeURIComponent("type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + signature);
}
创建连接
const CosmosClient = require("@azure/cosmos").CosmosClient;
cosmosClient = new CosmosClient({
endpoint: process.env.COSMOS_DB_URL,
auth: {
masterKey: process.env.COSMOS_DB_KEY
}
});