Skip to content

Blob Functions #3

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

Merged
merged 4 commits into from
Mar 15, 2021
Merged
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
28 changes: 28 additions & 0 deletions SyncGetBlobAsBytesReturnHttpResponse/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"scriptFile": "main.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "blob",
"direction": "in",
"name": "file",
"dataType": "binary",
"connection": "AzureWebJobsStorage",
"path": "python-worker-perf-tests/{infile}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
30 changes: 30 additions & 0 deletions SyncGetBlobAsBytesReturnHttpResponse/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import hashlib
import azure.functions as azf


def main(req: azf.HttpRequest, file: bytes) -> azf.HttpResponse:
"""
Read a blob (bytes) and respond back (in HTTP response) with the number of
bytes read and the MD5 digest of the content.
"""
assert isinstance(file, bytes)

content_size = len(file)
content_md5 = hashlib.md5(file).hexdigest()

response_dict = {
'content_size': content_size,
'content_md5': content_md5
}

response_body = json.dumps(response_dict, indent=2)

return azf.HttpResponse(
body=response_body,
mimetype="application/json",
status_code=200
)
28 changes: 28 additions & 0 deletions SyncGetBlobAsBytesStreamReturnHttpResponse/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"scriptFile": "main.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "blob",
"direction": "in",
"name": "file",
"dataType": "binary",
"connection": "AzureWebJobsStorage",
"path": "python-worker-perf-tests/{infile}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
30 changes: 30 additions & 0 deletions SyncGetBlobAsBytesStreamReturnHttpResponse/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import hashlib
import azure.functions as azf


def main(req: azf.HttpRequest, file: azf.InputStream) -> azf.HttpResponse:
"""
Read a blob (as azf.InputStream) and respond back (in HTTP response) with
the number of bytes read and the MD5 digest of the content.
"""
file_bytes = file.read()

content_size = len(file_bytes)
content_md5 = hashlib.md5(file_bytes).hexdigest()

response_dict = {
'content_size': content_size,
'content_md5': content_md5
}

response_body = json.dumps(response_dict, indent=2)

return azf.HttpResponse(
body=response_body,
mimetype="application/json",
status_code=200
)
28 changes: 28 additions & 0 deletions SyncGetBlobAsStrReturnHttpResponse/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"scriptFile": "main.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "blob",
"direction": "in",
"name": "file",
"dataType": "string",
"connection": "AzureWebJobsStorage",
"path": "python-worker-perf-tests/{infile}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
31 changes: 31 additions & 0 deletions SyncGetBlobAsStrReturnHttpResponse/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import hashlib
import azure.functions as azf


def main(req: azf.HttpRequest, file: str) -> azf.HttpResponse:
"""
Read a blob (string) and respond back (in HTTP response) with the number of
characters read and the MD5 digest of the utf-8 encoded content.
"""
assert isinstance(file, str)

num_chars = len(file)
content_bytes = file.encode('utf-8')
content_md5 = hashlib.md5(content_bytes).hexdigest()

response_dict = {
'num_chars': num_chars,
'content_md5': content_md5
}

response_body = json.dumps(response_dict, indent=2)

return azf.HttpResponse(
body=response_body,
mimetype="application/json",
status_code=200
)
28 changes: 28 additions & 0 deletions SyncPutBlobAsBytesReturnHttpResponse/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"scriptFile": "main.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "blob",
"direction": "out",
"name": "file",
"dataType": "binary",
"connection": "AzureWebJobsStorage",
"path": "python-worker-perf-tests/{outfile}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
41 changes: 41 additions & 0 deletions SyncPutBlobAsBytesReturnHttpResponse/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import random
import json
import hashlib
import azure.functions as azf


def main(req: azf.HttpRequest, file: azf.Out[bytes]) -> azf.HttpResponse:
"""
Write a blob (bytes) and respond back (in HTTP response) with the number of
bytes written and the MD5 digest of the content.
The number of bytes to write are specified in the input HTTP request.
"""
content_size = int(req.params['content_size'])

# When this is set, then 0x01 byte is repeated content_size number of
# times to use as input.
# This is to avoid generating random input for large size which can be
# slow.
if 'no_random_input' in req.params:
content = b'\x01' * content_size
else:
content = bytearray(random.getrandbits(8) for _ in range(content_size))
content_md5 = hashlib.md5(content).hexdigest()

file.set(content)

response_dict = {
'content_size': content_size,
'content_md5': content_md5
}

response_body = json.dumps(response_dict, indent=2)

return azf.HttpResponse(
body=response_body,
mimetype="application/json",
status_code=200
)
28 changes: 28 additions & 0 deletions SyncPutBlobAsStrReturnHttpResponse/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"scriptFile": "main.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "blob",
"direction": "out",
"name": "file",
"dataType": "string",
"connection": "AzureWebJobsStorage",
"path": "python-worker-perf-tests/{outfile}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
39 changes: 39 additions & 0 deletions SyncPutBlobAsStrReturnHttpResponse/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import string
import random
import json
import hashlib
import azure.functions as azf


def main(req: azf.HttpRequest, file: azf.Out[str]) -> azf.HttpResponse:
"""
Write a blob (string) and respond back (in HTTP response) with the number of
characters written and the MD5 digest of the utf-8 encoded content.
The number of characters to write are specified in the input HTTP request.
"""
num_chars = int(req.params['num_chars'])

content = ''.join(random.choices(string.ascii_uppercase + string.digits,
k=num_chars))
content_bytes = content.encode('utf-8')
content_size = len(content_bytes)
content_md5 = hashlib.md5(content_bytes).hexdigest()

file.set(content)

response_dict = {
'num_chars': num_chars,
'content_size': content_size,
'content_md5': content_md5
}

response_body = json.dumps(response_dict, indent=2)

return azf.HttpResponse(
body=response_body,
mimetype="application/json",
status_code=200
)
52 changes: 52 additions & 0 deletions SyncPutGetMultipleBlobsAsBytesReturnHttpResponse/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"scriptFile": "main.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "blob",
"direction": "in",
"name": "inputfile1",
"dataType": "binary",
"connection": "AzureWebJobsStorage",
"path": "python-worker-perf-tests/{infile1}"
},
{
"type": "blob",
"direction": "in",
"name": "inputfile2",
"dataType": "binary",
"connection": "AzureWebJobsStorage",
"path": "python-worker-perf-tests/{infile2}"
},
{
"type": "blob",
"direction": "out",
"name": "outputfile1",
"dataType": "binary",
"connection": "AzureWebJobsStorage",
"path": "python-worker-perf-tests/{outfile1}"
},
{
"type": "blob",
"direction": "out",
"name": "outputfile2",
"dataType": "binary",
"connection": "AzureWebJobsStorage",
"path": "python-worker-perf-tests/{outfile2}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
Loading