Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure blob adapter #1366

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions lib/azure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
var Gun = require('../gun');
var azureBlobStore;
let containerClient;

const { Readable } = require('stream');
const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to be moved to L17 also


Gun.on('create', function(root) {
console.log("Please note this adapter for azure is not fully working yet");
this.to.next(root);
var opt = root.opt;
if (!opt.azureBlob && !process.env.AZURE_BLOB_CONTAINER) {
return;
}

try {
azureBlobStore = require('@azure/storage-blob');
} catch (e) {
console.log("Please `npm install @azure/storage-blob` or add it to your package.json !");
}

var opts = opt.azureBlob || (opt.azureBlob = {});
opts.accountName = opts.accountName || process.env.AZURE_BLOB_ACCOUNT_NAME;
opts.accountKey = opts.accountKey || process.env.AZURE_BLOB_ACCOUNT_KEY;
opts.containerName = opts.containerName || process.env.AZURE_BLOB_CONTIAINER_NAME;

const storageAccountBaseUrl = `https://${opts.accountName}.blob.core.windows.net`,
sharedKeyCredential = new StorageSharedKeyCredential(opts.accountName, opts.accountKey);

const blobServiceClient = new BlobServiceClient(
storageAccountBaseUrl,
sharedKeyCredential
);

containerClient = blobServiceClient.getContainerClient(opts.containerName);

// Check if the container exists, if not, create it
containerClient.exists().then((exists) => {
if (!exists) {
containerClient.create();
console.log(`Blob container ${opts.containerName} created successfully.`);
}
}).catch((error) => {
console.error("Error checking container existence:", error);
});

opt.store = Store(opt);
});

function Store(opt) {
opt = opt || {};
opt.file = String(opt.file || 'radata');
var opts = opt.azureBlob,
azureBlob = opts.azureBlob;
var c = { p: {}, g: {}, l: {} };

var store = function Store() {};
if (Store[opt.file]) {
console.log("Warning: reusing same Azure Blob store and options as 1st.");
return Store[opt.file];
}
Store[opt.file] = store;

store.put = async function(file, data, cb) {
try {
const blobClient = containerClient.getBlockBlobClient(file);
const stream = Readable.from(data);
const uploadResponse = await blobClient.uploadStream(stream, data.length, undefined, undefined);
console.log("File uploaded successfully", uploadResponse);
cb(null, 'azure');
} catch (error) {
console.error("Error uploading file:", error);
cb(error, null);
}
};

store.get = async function(file, cb) {
try {
const blobClient = containerClient.getBlockBlobClient(file);
const downloadResponse = await blobClient.download(0);
const downloadedData = [];
downloadResponse.readableStreamBody.on("data", (data) => {
downloadedData.push(data.toString());
});
downloadResponse.readableStreamBody.on("end", () => {
const data = downloadedData.join('');
cb(null, data);
});
downloadResponse.readableStreamBody.on("error", (err) => {
console.error("Error reading stream:", err);
cb(err, null);
});
} catch (error) {
// console.error("Error downloading file:", error);
cb(error, null);
}
};

store.list = async function(cb) {
try {
const response = await containerClient.listBlobsFlat();
const keys = [];
response.segment.blobItems.forEach((blob) => {
keys.push(blob.name);
});
cb(keys);
} catch (error) {
//console.error("Error listing blobs:", error);
cb([]);
}
};

return store;
}

module.exports = Store;
2 changes: 2 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
require('./rfs');
require('./rs3');
require('./wire');
require('./azure')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct! Tho, until you have it 100% working let's keep it commented out in PRs/prod. If a dev doesn't have Azure optionally NPM installed we want to make sure nothing crashes/breaks.



try{require('../sea');}catch(e){}
try{require('../axe');}catch(e){}
Expand Down