it is working with the new AWS S3 SDK

This commit is contained in:
Luke Berndt
2023-04-09 13:31:13 -04:00
parent 6e4cbb0ec8
commit ef9eb0a89a
5 changed files with 1435 additions and 34 deletions

View File

@@ -8,17 +8,18 @@ var Talkgroup = require("../models/talkgroup");
var { callModel: Call } = require("../models/call");
var secrets = require("../config/secrets");
var util = require("util")
const { PutObjectCommand, S3Client } = require("@aws-sdk/client-s3");
const {fromIni} = require("@aws-sdk/credential-providers");
var s3_endpoint = process.env['S3_ENDPOINT'] != null ? process.env['S3_ENDPOINT'] : 'https://s3.us-west-1.wasabisys.com';
var s3_region = process.env['S3_REGION'] != null ? process.env['S3_REGION'] : 'us-west-1';
var s3_bucket = process.env['S3_BUCKET'] != null ? process.env['S3_BUCKET'] : 'openmhz-west';
var s3_profile = process.env['S3_PROFILE'] != null ? process.env['S3_PROFILE'] : 'wasabi-account';
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
const client = new S3Client({
credentials: fromIni({ profile: s3_profile }),
bucketEndpoint: s3_endpoint,
endpoint: s3_endpoint,
region: s3_region,
maxAttempts: 1
});
@@ -33,10 +34,10 @@ exports.upload = async function (req, res, next) {
}
var shortName = req.params.shortName.toLowerCase();
var apiKey = req.body.api_key;
let item = null;
try {
const item = System.findOne({ 'shortName': shortName }, ["key", "ignoreUnknownTalkgroup"]);
item = await System.findOne({ 'shortName': shortName }, ["key", "ignoreUnknownTalkgroup"]);
} catch (err) {
console.warn("[" + req.params.shortName + "] Error /:shortName/upload - Error: " + err);
res.status(500);
@@ -157,8 +158,7 @@ exports.upload = async function (req, res, next) {
});
try {
const response = await client.send(command);
console.log(response);
await client.send(command);
wasabiSrc.destroy();
fs.unlink(req.file.path, async (err) => {
@@ -174,11 +174,8 @@ exports.upload = async function (req, res, next) {
next();
}
});
if (err) {
console.error("[" + call.shortName + "] " + call.name + " - content-length: " + req.headers['content-length'] + " Wasabi Error", err);
}
} catch (err) {
console.warn("[" + call.shortName + "] Upload Error: " + err + " Filename: " + call.name + " content-length: " + req.headers['content-length']);
console.warn("[" + call.shortName + "] Upload Error: " + err + " Filename: " + call.name + " content-length: " + req.headers['content-length'] + " Key: " + object_key + " Bucket: " + s3_bucket);
res.status(500);
sysStats.addError(call.toObject());
res.contentType('json');

1411
backend/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -3,6 +3,9 @@
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@aws-sdk/client-s3": "^3.309.0",
"@aws-sdk/credential-provider-process": "^3.306.0",
"@aws-sdk/credential-providers": "^3.309.0",
"adm-zip": "^0.5.10",
"aws-sdk": "^2.1353",
"axios": "1.3.5",

View File

@@ -73,15 +73,15 @@ exports.initStats = async function() {
exports.addError = function(call) {
// if there is no Array associated with the system.
if (errorTotals[call.shortName] == undefined) {
errorTotals[call.shortName] = new Array();
if (uploadErrors[call.shortName] == undefined) {
uploadErrors[call.shortName] = new Array();
for (var j = 0; j < spots; j++) {
errorTotals[call.shortName][j] = 0;
uploadErrors[call.shortName][j] = 0;
}
}
// Add an error to the count for the current period.
errorTotals[call.shortName][0]++;
uploadErrors[call.shortName][0]++;
}
// Keeps track of the number of calls for each talkgroup for a system
@@ -144,25 +144,14 @@ exports.shiftStats = async function() {
// for all the systems in Error Stats
for (var shortName in uploadErrors) {
if (upload.hasOwnProperty(shortName)) {
if (uploadErrors.hasOwnProperty(shortName)) {
// Update the DB with Error Stats and Error Totals
statsCollection.update({
shortName: shortName
}, {
$set: {
"uploadErrors": uploadErrors[shortName]
}
}, {
upsert: true
}, function(err, objects) {
if (err) {
console.log("Shortname: " + shortName);
console.log("uploadErrors: " + util.inspect(uploadErrors[shortName]));
console.warn(err.message);
}
});
const query = { shortName: shortName};
const update = { $set: { "uploadErrors": uploadErrors[shortName]}};
const options = {upsert: true};
statsCollection.updateOne(query, update, options);
// move everything back one after updating
for (var j = spots - 1; j > 0; j--) {
uploadErrors[shortName][j] = uploadErrors[shortName][j - 1];
@@ -205,11 +194,11 @@ exports.shiftStats = async function() {
}
}
const query = { shortName: item.shortName};
const query = { shortName: shortName};
const update = { $set: { "decodeErrorsFreq": decodeErrorsFreq[shortName]}};
const options = {upsert: true};
sysCollection.updateOne(query, update, options);
statsCollection.updateOne(query, update, options);
}
}
@@ -277,7 +266,7 @@ exports.shiftStats = async function() {
const update = { $set: { "talkgroupStats": talkgroupStats[shortName], "callTotals": callTotals[shortName] }};
const options = {upsert: true};
sysCollection.updateOne(query, update, options);
statsCollection.updateOne(query, update, options);
}
}
updateActiveSystems();

View File

@@ -5,6 +5,7 @@ export REACT_APP_ADMIN_EMAIL="admin@email.com"
export DOMAIN_NAME="openmhz.test"
export S3_PROFILE='default'
export S3_REGION='us-west-001'
export S3_ENDPOINT='https://s3.us-west-1.wasabisys.com'
export S3_BUCKET='openmhz-test'