def get_signed_url_for_permstore_blob(permstore_url):
    blob_url = URL(permstore_url)

    # create sas signature
    blob_service = __get_perm_store_service()
    sas_signature = blob_service.generate_blob_shared_access_signature(
        os.getenv('DESTINATION_CONTAINER_NAME'), blob_url.name,
        BlobPermissions.READ,
        datetime.utcnow() + timedelta(hours=1))

    logging.debug("INFO: have sas signature {}".format(sas_signature))
    signed_url = blob_url.with_query(sas_signature)
    return signed_url.as_uri()
예제 #2
0
파일: test_url.py 프로젝트: jmcs/urlpath
    def test_query(self):
        query = 'field1=value1&field1=value2&field2=hello,%20world%26python'
        url = URL('http://www.example.com/form?' + query)

        self.assertEqual(url.query, query)
        self.assertSetEqual(set(url.form), {'field1', 'field2'})
        self.assertTupleEqual(url.form.get('field1'), ('value1', 'value2'))
        self.assertTupleEqual(url.form.get('field2'), ('hello, world&python', ))
        self.assertIn('field1', url.form)
        self.assertIn('field2', url.form)
        self.assertNotIn('field3', url.form)
        self.assertNotIn('field4', url.form)

        url = url.with_query({'field3': 'value3', 'field4': [1, 2, 3]})
        self.assertSetEqual(set(url.form), {'field3', 'field4'})
        self.assertNotIn('field1', url.form)
        self.assertNotIn('field2', url.form)
        self.assertIn('field3', url.form)
        self.assertIn('field4', url.form)
        self.assertTupleEqual(url.form.get('field3'), ('value3', ))
        self.assertTupleEqual(url.form.get('field4'), ('1', '2', '3'))
예제 #3
0
    def test_query(self):
        query = 'field1=value1&field1=value2&field2=hello,%20world%26python'
        url = URL('http://www.example.com/form?' + query)

        self.assertEqual(url.query, query)
        self.assertSetEqual(set(url.form), {'field1', 'field2'})
        self.assertTupleEqual(url.form.get('field1'), ('value1', 'value2'))
        self.assertTupleEqual(url.form.get('field2'),
                              ('hello, world&python', ))
        self.assertIn('field1', url.form)
        self.assertIn('field2', url.form)
        self.assertNotIn('field3', url.form)
        self.assertNotIn('field4', url.form)

        url = url.with_query({'field3': 'value3', 'field4': [1, 2, 3]})
        self.assertSetEqual(set(url.form), {'field3', 'field4'})
        self.assertNotIn('field1', url.form)
        self.assertNotIn('field2', url.form)
        self.assertIn('field3', url.form)
        self.assertIn('field4', url.form)
        self.assertTupleEqual(url.form.get('field3'), ('value3', ))
        self.assertTupleEqual(url.form.get('field4'), ('1', '2', '3'))
예제 #4
0
    def test_add_query(self):
        query = 'field1=value1&field1=value2&field2=hello,%20world%26python'
        url = URL('http://www.example.com/form?' + query)

        ## if initial query is null, it should include the added query
        ext_url = url.with_query('').add_query({
            'field3': 'value3',
            'field4': [1, 2, 3]
        })
        self.assertSetEqual(set(ext_url.form), {'field3', 'field4'})
        self.assertTupleEqual(ext_url.form.get('field3'), ('value3', ))
        self.assertTupleEqual(ext_url.form.get('field4'), ('1', '2', '3'))
        self.assertNotIn('field1', ext_url.form)
        self.assertNotIn('field2', ext_url.form)
        self.assertIn('field3', ext_url.form)
        self.assertIn('field4', ext_url.form)

        ## if initial query exists, it should include the both fields
        ext_query = {'field3': 'value3', 'field4': [1, 2, 3]}
        ext_url = url.add_query({'field3': 'value3', 'field4': [1, 2, 3]})
        self.assertEqual(
            ext_url.query,
            '%s&%s' % (query, 'field3=value3&field4=1&field4=2&field4=3'))
        self.assertSetEqual(set(ext_url.form),
                            {'field1', 'field2', 'field3', 'field4'})
        self.assertIn('field1', ext_url.form)
        self.assertIn('field2', ext_url.form)
        self.assertIn('field3', ext_url.form)
        self.assertIn('field4', ext_url.form)
        self.assertTupleEqual(ext_url.form.get('field1'), ('value1', 'value2'))
        self.assertTupleEqual(ext_url.form.get('field2'),
                              ('hello, world&python', ))
        self.assertTupleEqual(ext_url.form.get('field3'), ('value3', ))
        self.assertTupleEqual(ext_url.form.get('field4'), ('1', '2', '3'))

        ## if added query is null, it should include original query
        self.assertEqual(url.add_query({}).query, query)
    async def _get_info(self, path, children=False, limit=None, **kwargs):
        """
        Request file or directory metadata to the API

        :param path: (str)
        :param children: (bool) if True, return metadata of the children paths
            as well
        :param limit: (int) if provided and children is True, set limit to the
            number of children returned
        :param kwargs: (dict) optional arguments passed on to requests
        :return (dict) path metadata
        """
        url = URL(self.api_url) / 'namespace' / _encode(path)
        url = url.with_query(children=children)
        if limit is not None and children:
            url = url.add_query(limit=f'{limit}')
        url = url.as_uri()
        kw = self.kwargs.copy()
        kw.update(kwargs)
        async with self.session.get(url, **kw) as r:
            if r.status == 404:
                raise FileNotFoundError(url)
            r.raise_for_status()
            return await r.json()
예제 #6
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    user_name = req.params.get('userName')

    if not user_name:
        return func.HttpResponse(
            status_code=401,
            headers=DEFAULT_RETURN_HEADER,
            body=json.dumps({"error": "invalid userName given or omitted"})
        )

    try:
        req_body = req.get_json()
        logging.debug(req.get_json())
        storage_account = req_body["storageAccount"]
        storage_account_key = req_body["storageAccountKey"]
        storage_container = req_body["storageContainer"]
    except ValueError:
        return func.HttpResponse(
            "ERROR: Unable to decode POST body",
            status_code=400
        )

    if not storage_container or not storage_account or not storage_account_key:
        return func.HttpResponse(
            "ERROR: storage container/account/key/queue not specified.",
            status_code=401
        )

    # Create blob service for storage account (retrieval source)
    blob_service = BlockBlobService(
        account_name=storage_account,
        account_key=storage_account_key)

    # Queue service for perm storage and queue
    queue_service = QueueService(
        account_name=os.getenv('STORAGE_ACCOUNT_NAME'),
        account_key=os.getenv('STORAGE_ACCOUNT_KEY')
    )

    queue_service.encode_function = QueueMessageFormat.text_base64encode

    try:
        blob_list = []

        for blob_object in blob_service.list_blobs(storage_container):
            blob_url = URL(
                blob_service.make_blob_url(
                    storage_container,
                    blob_object.name
                )
            )
            # Check for supported image types here.
            if ImageFileType.is_supported_filetype(blob_url.suffix):
                logging.debug("INFO: Building sas token for blob " + blob_object.name)
                # create sas signature
                sas_signature = blob_service.generate_blob_shared_access_signature(
                    storage_container,
                    blob_object.name,
                    BlobPermissions.READ,
                    datetime.utcnow() + timedelta(hours=1)
                )

                logging.debug("INFO: have sas signature {}".format(sas_signature))

                signed_url = blob_url.with_query(sas_signature)

                blob_list.append(signed_url.as_uri())

                logging.debug("INFO: Built signed url: {}".format(signed_url))

                msg_body = {
                    "imageUrl": signed_url.as_uri(),
                    "fileName": str(blob_url.name),
                    "fileExtension": str(blob_url.suffix),
                    "directoryComponents": get_filepath_from_url(blob_url, storage_container),
                    "userName": user_name
                }

                body_str = json.dumps(msg_body)
                queue_service.put_message("onboardqueue", body_str)
            else:
                logging.info("Blob object not supported. Object URL={}".format(blob_url.as_uri))

        return func.HttpResponse(
            status_code=202,
            headers=DEFAULT_RETURN_HEADER,
            body=json.dumps(blob_list)
        )
    except Exception as e:
        logging.error("ERROR: Could not build blob object list. Exception: " + str(e))
        return func.HttpResponse("ERROR: Could not get list of blobs in storage_container={0}. Exception={1}".format(
            storage_container, e), status_code=500)