Reputation: 59
We have 11 JSON files in our azure blob storage container in a folder called jsonfiles, Let's assume the file names as (a.json, b.json, c.json, d.json, e.json, f.json, g.json, h.json, i.json, j.json, k.json).
Similarly, we have 11 Cosmo Mongo DB Collection, assume the collection name as below [ a, b, c, d, e, f, g, h, I, j, k].
I need to copy/upload (a.json file to 'a' collection), (b.json to 'b' collection), (c.json to 'c' collection), (d.json to 'd' collection), (e.json to 'e' collection), (f.json to 'f' collection), (g.json to 'g' collection), (h.json to 'h' collection), (i.json to 'i' collection), (j.json to 'j' collection), (k.json to 'k') collection.
I need to Achieve the above requirement through Azure Function App. If Someone is well and good with Azure Function, Please Explain in detailed how to achieve this.
Detailed Explanation is Much Appreciated. Thank You in Advance.
Upvotes: 0
Views: 309
Reputation: 2069
to download the blob, you will need a blob service client
,container client
and a blobclient
. Then we can use downloadtofile
function to download the file.
the downloadtofile
function will take a file location as input and will write data from the blob to a file with the same name is blob.
There must exist a file with same name is blob.
Similarly, now you would need a MongodbClient
to connect to database and then we can read file which are downloaded and add them in documents which are Json objects and then use insertOne
as to upload the documents with our Json file to azure cosmos db mongo db.
Here I am downloading three files and uploading them to azure
complete code:
// Dependencies
const { BlobServiceClient } = require("@azure/storage-blob");
const { MongoClient } = require('mongodb');
const fs = require('fs');
const { ObjectID } = require("bson");
const container_name = "test";
const connblob = "<Connection String of Storage Account>";
module.exports = async function (context, req) {
/// Hepls in connecting to Database
const client = new MongoClient("< Connectiom String of azure cosmosdb mongodb>");
// connecting to blob storage
var blob_service_client = BlobServiceClient.fromConnectionString(connblob);
// refence of container
var container_client = blob_service_client.getContainerClient(container_name);
// Reference of perticular blobs
var blob_client1 = container_client.getBlobClient("a.json");
// download the blob
await blob_client1.downloadToFile("./blobs/a.json");
var blob_client2 = container_client.getBlobClient("b.json");
await blob_client2.downloadToFile("./blobs/b.json");
var blob_client3 = container_client.getBlobClient("c.json");
await blob_client3.downloadToFile("./blobs/c.json");
// connection to databse
await client.connect();
// creating the database
const databse = client.db("test");
// creating collenctions
const collection1 = databse.collection("a");
const collection2 = databse.collection("b");
const collection3 = databse.collection("c");
// Reading files
const f1 = fs.readFileSync('./blobs/a.json', 'utf-8')
const f2 = fs.readFileSync('./blobs/b.json', 'utf-8')
const f3 = fs.readFileSync('./blobs/c.json', 'utf-8')
// creating documents
var t1 = {
_id:ObjectID("123456789012")
}
var t2 = {
_id:ObjectID("212345678909")
}
var t3 = {
_id:ObjectID("312345678901")
}
// adding files to documents
t1["file"] = f1;
t2["file"] = f2;
t3["file"] = f3;
// adding documents to mongodb
collection1.insertOne(t1);
collection2.insertOne(t2);
collection3.insertOne(t3);
context.res = {
// status: 200, /* Defaults to 200 */
body: "responseMessage"
};
}
Upvotes: 1