diff --git a/azure/functions/__init__.py b/azure/functions/__init__.py index 053d788a..fcd80316 100644 --- a/azure/functions/__init__.py +++ b/azure/functions/__init__.py @@ -102,4 +102,4 @@ 'BlobSource' ) -__version__ = '1.22.0b2' +__version__ = '1.22.0b4' diff --git a/azure/functions/blob.py b/azure/functions/blob.py index b75500ab..7c34f1a9 100644 --- a/azure/functions/blob.py +++ b/azure/functions/blob.py @@ -113,7 +113,8 @@ def decode(cls, data: meta.Datum, *, trigger_metadata) -> Any: trigger_metadata, 'Properties', python_type=dict) if properties: blob_properties = properties - length = properties.get('Length') + length = properties.get('ContentLength') or \ + properties.get('Length') length = int(length) if length else None else: blob_properties = None diff --git a/azure/functions/decorators/blob.py b/azure/functions/decorators/blob.py index bd2861fa..9ed4ce2f 100644 --- a/azure/functions/decorators/blob.py +++ b/azure/functions/decorators/blob.py @@ -17,7 +17,10 @@ def __init__(self, **kwargs): self.path = path self.connection = connection - self.source = source.value if source else None + if isinstance(source, BlobSource): + self.source = source.value + else: + self.source = source # type: ignore super().__init__(name=name, data_type=data_type) @staticmethod diff --git a/tests/decorators/test_blob.py b/tests/decorators/test_blob.py index 43926591..bad8f99b 100644 --- a/tests/decorators/test_blob.py +++ b/tests/decorators/test_blob.py @@ -50,7 +50,7 @@ def test_blob_trigger_creation_with_source_as_string(self): trigger = BlobTrigger(name="req", path="dummy_path", connection="dummy_connection", - source=BlobSource.EVENT_GRID, + source="EventGrid", data_type=DataType.UNDEFINED, dummy_field="dummy") diff --git a/tests/test_blob.py b/tests/test_blob.py index 3d9ed846..adb1f16a 100644 --- a/tests/test_blob.py +++ b/tests/test_blob.py @@ -84,7 +84,7 @@ def test_blob_input_with_metadata_no_blob_properties(self): self.assertEqual(result.metadata, None) def test_blob_input_with_metadata_no_trigger_metadata(self): - sample_blob_properties = '{"Length": "12"}' + sample_blob_properties = '{"ContentLength": "12"}' datum: Datum = Datum(value=b'blob_content', type='bytes') trigger_metadata: Dict[str, Any] = { 'Properties': Datum(sample_blob_properties, 'json'), @@ -97,7 +97,7 @@ def test_blob_input_with_metadata_no_trigger_metadata(self): # Verify result metadata self.assertIsInstance(result, InputStream) self.assertEqual(result.name, 'blob_trigger_name') - self.assertEqual(result.length, len(b'blob_content')) + self.assertEqual(result.length, 12) self.assertEqual(result.uri, 'https://test.io/blob_trigger') self.assertEqual(result.blob_properties, json.loads(sample_blob_properties)) @@ -115,7 +115,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self): "LeaseStatus": 2, "LeaseState": 1, "LeaseDuration": 0, - "Length": "12" + "ContentLength": "12" }''' datum: Datum = Datum(value=b'blob_content', type='bytes') trigger_metadata: Dict[str, Any] = { @@ -130,7 +130,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self): # Verify result metadata self.assertIsInstance(result, InputStream) self.assertEqual(result.name, 'blob_trigger_name') - self.assertEqual(result.length, len(b'blob_content')) + self.assertEqual(result.length, 12) self.assertEqual(result.uri, 'https://test.io/blob_trigger') self.assertEqual(result.blob_properties, json.loads(sample_blob_properties)) @@ -139,7 +139,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self): def test_blob_input_with_metadata_with_incorrect_trigger_metadata(self): sample_metadata = 'Hello World' - sample_blob_properties = '''{"Length": "12"}''' + sample_blob_properties = '''{"ContentLength": "12"}''' datum: Datum = Datum(value=b'blob_content', type='bytes') trigger_metadata: Dict[str, Any] = { 'Metadata': Datum(sample_metadata, 'string'), @@ -153,7 +153,7 @@ def test_blob_input_with_metadata_with_incorrect_trigger_metadata(self): # Verify result metadata self.assertIsInstance(result, InputStream) self.assertEqual(result.name, 'blob_trigger_name') - self.assertEqual(result.length, len(b'blob_content')) + self.assertEqual(result.length, 12) self.assertEqual(result.uri, 'https://test.io/blob_trigger') self.assertEqual(result.blob_properties, json.loads(sample_blob_properties)) @@ -228,3 +228,46 @@ def read(self) -> Datum: check_output_type = afb.BlobConverter.check_output_type_annotation self.assertTrue(check_output_type(CustomOutput)) + + def test_blob_input_with_metadata_with_length(self): + sample_blob_properties = '{"Length": "12"}' + datum: Datum = Datum(value=b'blob_content', type='bytes') + trigger_metadata: Dict[str, Any] = { + 'Properties': Datum(sample_blob_properties, 'json') + } + result: InputStream = afb. \ + BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata) + + # Verify result metadata + self.assertIsInstance(result, InputStream) + self.assertEqual(result.length, 12) + + def test_blob_input_with_metadata_with_both_length(self): + sample_blob_properties = '''{ + "ContentLength": "12", + "Length": "10" + }''' + datum: Datum = Datum(value=b'blob_content', type='bytes') + trigger_metadata: Dict[str, Any] = { + 'Properties': Datum(sample_blob_properties, 'json') + } + result: InputStream = afb. \ + BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata) + + # Verify result metadata. + # This should be 12, since we check for ContentLength first + self.assertIsInstance(result, InputStream) + self.assertEqual(result.length, 12) + + def test_blob_input_with_metadata_with_no_length(self): + sample_blob_properties = '''{}''' + datum: Datum = Datum(value=b'blob_content', type='bytes') + trigger_metadata: Dict[str, Any] = { + 'Properties': Datum(sample_blob_properties, 'json') + } + result: InputStream = afb. \ + BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata) + + # Verify result metadata. + self.assertIsInstance(result, InputStream) + self.assertEqual(result.length, None)