def test_blob_service_properties(self):

        # Instantiate a BlobServiceClient using a connection string
        from azure.storage.blob import BlobServiceClient
        blob_service_client = BlobServiceClient.from_connection_string(self.connection_string)

        # [START set_blob_service_properties]
        # Create service properties
        from azure.storage.blob import Logging, Metrics, CorsRule, RetentionPolicy

        # Create logging settings
        logging = Logging(read=True, write=True, delete=True, retention_policy=RetentionPolicy(enabled=True, days=5))

        # Create metrics for requests statistics
        hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5))
        minute_metrics = Metrics(enabled=True, include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True, days=5))

        # Create CORS rules
        cors_rule = CorsRule(['www.xyz.com'], ['GET'])
        cors = [cors_rule]

        # Set the service properties
        blob_service_client.set_service_properties(logging, hour_metrics, minute_metrics, cors)
        # [END set_blob_service_properties]

        # [START get_blob_service_properties]
        properties = blob_service_client.get_service_properties()
        # [END get_blob_service_properties]
        assert properties is not None
Exemple #2
0
    async def test_set_delete_retention_policy_edge_cases(
            self, resource_group, location, storage_account,
            storage_account_key):
        # Should work with minimum settings
        bsc = BlobServiceClient(self.account_url(storage_account, "blob"),
                                credential=storage_account_key,
                                transport=AiohttpTestTransport())

        delete_retention_policy = RetentionPolicy(enabled=True, days=1)
        await bsc.set_service_properties(
            delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = await bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(
            received_props['delete_retention_policy'], delete_retention_policy)

        # Should work with maximum settings
        delete_retention_policy = RetentionPolicy(enabled=True, days=365)
        await bsc.set_service_properties(
            delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = await bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(
            received_props['delete_retention_policy'], delete_retention_policy)

        # Should not work with 0 days
        delete_retention_policy = RetentionPolicy(enabled=True, days=0)

        with self.assertRaises(HttpResponseError):
            await bsc.set_service_properties(
                delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = await bsc.get_service_properties()
        self._assert_delete_retention_policy_not_equal(
            received_props['delete_retention_policy'], delete_retention_policy)

        # Should not work with 366 days
        delete_retention_policy = RetentionPolicy(enabled=True, days=366)

        with self.assertRaises(HttpResponseError):
            await bsc.set_service_properties(
                delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = await bsc.get_service_properties()
        self._assert_delete_retention_policy_not_equal(
            received_props['delete_retention_policy'], delete_retention_policy)
def main(event: func.EventGridEvent):
    result = json.dumps({
        'id': event.id,
        'data': event.get_json(),
        'topic': event.topic,
        'subject': event.subject,
        'event_type': event.event_type,
    })

    logging.info('Python EventGrid trigger processed an event: %s', result)

    blob_service_client = BlobServiceClient.from_connection_string(
        os.environ.get('ORIGIN_STORAGE_CONNECTION_STRING'))

    # Create a retention policy to retain deleted blobs
    delete_retention_policy = RetentionPolicy(enabled=True, days=1)

    # Set the retention policy on the service
    blob_service_client.set_service_properties(
        delete_retention_policy=delete_retention_policy)

    # Blob info to delete
    blob_url = event.get_json().get('url')
    container_name = blob_url.split("/")[-2].split("?")[0].split("-")[0]
    blob_name = blob_url.split("/")[-1].split("?")[0]

    blob_to_delete = blob_service_client.get_blob_client(
        container=container_name, blob=blob_name)

    blob_to_delete.delete_blob()
Exemple #4
0
    def test_set_delete_retention_policy_edge_cases(self, resource_group,
                                                    location, storage_account,
                                                    storage_account_key):
        bsc = BlobServiceClient(self._account_url(storage_account.name),
                                credential=storage_account_key)
        delete_retention_policy = RetentionPolicy(enabled=True, days=1)
        bsc.set_service_properties(
            delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(
            received_props['delete_retention_policy'], delete_retention_policy)

        # Should work with maximum settings
        delete_retention_policy = RetentionPolicy(enabled=True, days=365)
        bsc.set_service_properties(
            delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(
            received_props['delete_retention_policy'], delete_retention_policy)

        # Should not work with 0 days
        delete_retention_policy = RetentionPolicy(enabled=True, days=0)

        with self.assertRaises(ValidationError):
            bsc.set_service_properties(
                delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = bsc.get_service_properties()
        self._assert_delete_retention_policy_not_equal(
            received_props['delete_retention_policy'], delete_retention_policy)

        # Should not work with 366 days
        delete_retention_policy = RetentionPolicy(enabled=True, days=366)

        with self.assertRaises(HttpResponseError):
            bsc.set_service_properties(
                delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = bsc.get_service_properties()
        self._assert_delete_retention_policy_not_equal(
            received_props['delete_retention_policy'], delete_retention_policy)
    async def test_retention_too_long(self, storage_account_name, storage_account_key):
        bsc = BlobServiceClient(self.account_url(storage_account_name, "blob"), credential=storage_account_key, transport=AiohttpTestTransport())
        minute_metrics = Metrics(enabled=True, include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True, days=366))

        # Assert
        with self.assertRaises(HttpResponseError):
            await bsc.set_service_properties(None, None, minute_metrics)
Exemple #6
0
    def test_retention_too_long(self, resource_group, location, storage_account, storage_account_key):
        bsc = BlobServiceClient(self.account_url(storage_account.name, "blob"), credential=storage_account_key)
        minute_metrics = Metrics(enabled=True, include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True, days=366))

        # Assert
        self.assertRaises(HttpResponseError,
                          bsc.set_service_properties,
                          None, None, minute_metrics)
    def test_retention_too_long(self):
        # Arrange
        minute_metrics = Metrics(enabled=True, include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True, days=366))

        # Assert
        self.assertRaises(HttpResponseError,
                          self.bsc.set_service_properties,
                          None, None, minute_metrics)
    def test_set_delete_retention_policy_edge_cases(self):
        # Should work with minimum settings
        delete_retention_policy = RetentionPolicy(enabled=True, days=1)
        self.bsc.set_service_properties(
            delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = self.bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(
            received_props.delete_retention_policy, delete_retention_policy)

        # Should work with maximum settings
        delete_retention_policy = RetentionPolicy(enabled=True, days=365)
        self.bsc.set_service_properties(
            delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = self.bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(
            received_props.delete_retention_policy, delete_retention_policy)

        # Should not work with 0 days
        delete_retention_policy = RetentionPolicy(enabled=True, days=0)

        with self.assertRaises(ValidationError):
            self.bsc.set_service_properties(
                delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = self.bsc.get_service_properties()
        self._assert_delete_retention_policy_not_equal(
            received_props.delete_retention_policy, delete_retention_policy)

        # Should not work with 366 days
        delete_retention_policy = RetentionPolicy(enabled=True, days=366)

        with self.assertRaises(HttpResponseError):
            self.bsc.set_service_properties(
                delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = self.bsc.get_service_properties()
        self._assert_delete_retention_policy_not_equal(
            received_props.delete_retention_policy, delete_retention_policy)
    def test_set_logging(self):
        # Arrange
        logging = Logging(read=True, write=True, delete=True, retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        self.bsc.set_service_properties(logging=logging)

        # Assert
        received_props = self.bsc.get_service_properties()
        self._assert_logging_equal(received_props.logging, logging)
Exemple #10
0
    def test_set_hour_metrics(self, resource_group, location, storage_account, storage_account_key):
        bsc = BlobServiceClient(self.account_url(storage_account.name, "blob"), credential=storage_account_key)
        hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        bsc.set_service_properties(hour_metrics=hour_metrics)

        # Assert
        received_props = bsc.get_service_properties()
        self._assert_metrics_equal(received_props['hour_metrics'], hour_metrics)
Exemple #11
0
    def test_set_disabled_delete_retention_policy(self, resource_group, location, storage_account, storage_account_key):
        bsc = BlobServiceClient(self.account_url(storage_account.name, "blob"), credential=storage_account_key)
        delete_retention_policy = RetentionPolicy(enabled=False)

        # Act
        bsc.set_service_properties(delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(received_props['delete_retention_policy'], delete_retention_policy)
Exemple #12
0
    async def test_set_delete_retention_policy(self, resource_group, location, storage_account, storage_account_key):
        bsc = BlobServiceClient(self._account_url(storage_account.name), credential=storage_account_key, transport=AiohttpTestTransport())
        delete_retention_policy = RetentionPolicy(enabled=True, days=2)

        # Act
        await bsc.set_service_properties(delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = await bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(received_props['delete_retention_policy'], delete_retention_policy)
    def test_set_delete_retention_policy(self, storage_account_name, storage_account_key):
        bsc = BlobServiceClient(self.account_url(storage_account_name, "blob"), credential=storage_account_key)
        delete_retention_policy = RetentionPolicy(enabled=True, days=2)

        # Act
        bsc.set_service_properties(delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(received_props['delete_retention_policy'], delete_retention_policy)
    def test_set_hour_metrics(self):
        # Arrange
        hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        self.bsc.set_service_properties(hour_metrics=hour_metrics)

        # Assert
        received_props = self.bsc.get_service_properties()
        self._assert_metrics_equal(received_props.hour_metrics, hour_metrics)
    async def test_set_disabled_delete_retention_policy(self, storage_account_name, storage_account_key):
        bsc = BlobServiceClient(self.account_url(storage_account_name, "blob"), credential=storage_account_key, transport=AiohttpTestTransport())
        delete_retention_policy = RetentionPolicy(enabled=False)

        # Act
        await bsc.set_service_properties(delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = await bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(received_props['delete_retention_policy'], delete_retention_policy)
    def test_set_disabled_delete_retention_policy(self):
        # Arrange
        delete_retention_policy = RetentionPolicy(enabled=False)

        # Act
        self.bsc.set_service_properties(delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = self.bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(received_props.delete_retention_policy, delete_retention_policy)
    async def test_set_hour_metrics(self, storage_account_name, storage_account_key):
        bsc = BlobServiceClient(self.account_url(storage_account_name, "blob"), credential=storage_account_key, transport=AiohttpTestTransport())
        hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        await bsc.set_service_properties(hour_metrics=hour_metrics)

        # Assert
        received_props = await bsc.get_service_properties()
        self._assert_metrics_equal(received_props['hour_metrics'], hour_metrics)
    def test_set_minute_metrics(self, storage_account_name, storage_account_key):
        bsc = BlobServiceClient(self.account_url(storage_account_name, "blob"), credential=storage_account_key)
        minute_metrics = Metrics(enabled=True, include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        bsc.set_service_properties(minute_metrics=minute_metrics)

        # Assert
        received_props = bsc.get_service_properties()
        self._assert_metrics_equal(received_props['minute_metrics'], minute_metrics)
Exemple #19
0
    async def test_set_minute_metrics(self, resource_group, location, storage_account, storage_account_key):
        bsc = BlobServiceClient(self._account_url(storage_account.name), credential=storage_account_key, transport=AiohttpTestTransport())
        minute_metrics = Metrics(enabled=True, include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        await bsc.set_service_properties(minute_metrics=minute_metrics)

        # Assert
        received_props = await bsc.get_service_properties()
        self._assert_metrics_equal(received_props['minute_metrics'], minute_metrics)
Exemple #20
0
    async def _test_set_delete_retention_policy_async(self):
        # Arrange
        delete_retention_policy = RetentionPolicy(enabled=True, days=2)

        # Act
        await self.bsc.set_service_properties(
            delete_retention_policy=delete_retention_policy)

        # Assert
        received_props = await self.bsc.get_service_properties()
        self._assert_delete_retention_policy_equal(
            received_props.delete_retention_policy, delete_retention_policy)
Exemple #21
0
    def test_soft_delete_and_undelete_blob(self):

        # Instantiate a BlobServiceClient using a connection string
        from azure.storage.blob import BlobServiceClient
        blob_service_client = BlobServiceClient.from_connection_string(
            self.connection_string)

        # Create a retention policy to retain deleted blobs
        from azure.storage.blob import RetentionPolicy
        delete_retention_policy = RetentionPolicy(enabled=True, days=1)

        # Set the retention policy on the service
        blob_service_client.set_service_properties(
            delete_retention_policy=delete_retention_policy)

        # Instantiate a ContainerClient
        container_client = blob_service_client.get_container_client(
            "containerforblobs")

        # Create new Container
        try:
            container_client.create_container()
        except ResourceExistsError:
            # Container already created
            pass

        # Upload a blob to the container
        with open(SOURCE_FILE, "rb") as data:
            container_client.upload_blob(name="my_blob", data=data)

        # Get the blob client
        blob_client = blob_service_client.get_blob_client(
            "containerforblobs", "my_blob")

        # Soft delete blob in the container (blob can be recovered with undelete)
        blob_client.delete_blob()

        # [START undelete_blob]
        # Undelete the blob before the retention policy expires
        blob_client.undelete_blob()
        # [END undelete_blob]

        # [START get_blob_properties]
        properties = blob_client.get_blob_properties()
        # [END get_blob_properties]

        assert properties is not None

        # Delete container
        blob_service_client.delete_container("containerforblobs")
Exemple #22
0
    async def _test_set_logging_async(self):
        # Arrange
        logging = BlobAnalyticsLogging(read=True,
                                       write=True,
                                       delete=True,
                                       retention_policy=RetentionPolicy(
                                           enabled=True, days=5))

        # Act
        await self.bsc.set_service_properties(analytics_logging=logging)

        # Assert
        received_props = await self.bsc.get_service_properties()
        self._assert_logging_equal(received_props.logging, logging)
Exemple #23
0
    async def _test_set_minute_metrics_async(self):
        # Arrange
        minute_metrics = Metrics(enabled=True,
                                 include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True,
                                                                  days=5))

        # Act
        await self.bsc.set_service_properties(minute_metrics=minute_metrics)

        # Assert
        received_props = await self.bsc.get_service_properties()
        self._assert_metrics_equal(received_props.minute_metrics,
                                   minute_metrics)
Exemple #24
0
    def test_set_logging(self, resource_group, location, storage_account,
                         storage_account_key):
        bsc = BlobServiceClient(self._account_url(storage_account.name),
                                credential=storage_account_key)
        logging = BlobAnalyticsLogging(read=True,
                                       write=True,
                                       delete=True,
                                       retention_policy=RetentionPolicy(
                                           enabled=True, days=5))

        # Act
        bsc.set_service_properties(analytics_logging=logging)

        # Assert
        received_props = bsc.get_service_properties()
        self._assert_logging_equal(received_props['analytics_logging'],
                                   logging)
    async def test_set_logging(self, storage_account_name, storage_account_key):
        bsc = BlobServiceClient(self.account_url(storage_account_name, "blob"), credential=storage_account_key, transport=AiohttpTestTransport())
        logging = BlobAnalyticsLogging(read=True, write=True, delete=True, retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        await bsc.set_service_properties(analytics_logging=logging)

        # Assert
        received_props = await bsc.get_service_properties()
        self._assert_logging_equal(received_props['analytics_logging'], logging)