-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
OisinHick
wants to merge
4
commits into
amark:master
Choose a base branch
from
OisinHick:azure-blob-adapter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Azure blob adapter #1366
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
require('./rfs'); | ||
require('./rs3'); | ||
require('./wire'); | ||
require('./azure') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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){} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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