Skip to content
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
2 changes: 1 addition & 1 deletion .bit/.progress
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25
29
19 changes: 19 additions & 0 deletions bunnimage-upload/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
53 changes: 53 additions & 0 deletions bunnimage-upload/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const multipart = require('parse-multipart')
const fetch = require('node-fetch')
const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING;
const { BlobServiceClient } = require("@azure/storage-blob");

module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');

var the_header_value = req.headers['codename'];

var responseMessage = ""
if (body == null) {
responseMessage = "Sorry! No image attached."
} else {
var password = the_header_value
var boundary = multipart.getBoundary(req.headers['content-type']);
var body = req.body;
var parsedBody = multipart.Parse(body, boundary);

var filetype = parsedBody[0].type;
if (filetype == "image/png") {
ext = "png";
} else if (filetype == "image/jpeg") {
ext = "jpeg";
} else if (filetype == "image/jpg") {
ext = "jpg"
} else {
username = "invalidimage"
ext = "";
}
responseMessage = await uploadFile(parsedBody, ext, password);
}

context.res = {
body: responseMessage
};

}

async function uploadFile(parsedBody, ext) {

const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
const containerName = "1bitcamp";
const containerClient = blobServiceClient.getContainerClient(containerName); // Get a reference to a container

const blobName = password + "." + ext; // Create the container
const blockBlobClient = containerClient.getBlockBlobClient(blobName); // Get a block blob client

const uploadBlobResponse = await blockBlobClient.upload(parsedBody[0].data, parsedBody[0].data.length);

return ("Your blob is saved");

}
3 changes: 3 additions & 0 deletions bunnimage-upload/sample.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Azure"
}
Loading