def make_blob_sas_url(account_name,
                      account_key,
                      container_name,
                      blob_name,
                      permission='w',
                      duration=16):
    """
    Generate a Blob SAS URL to allow a client to upload a file.

    account_name: Storage account name.
    account_key: Storage account key.
    container_name: Storage container.
    blob_name: Blob name.
    duration: A timedelta representing duration until SAS expiration.
       SAS start date will be utcnow() minus one minute. Expiry date
       is start date plus duration.

    Returns the SAS URL.
    """
    sas = SharedAccessSignature(account_name, account_key)
    resource_path = '%s/%s' % (container_name, blob_name)
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start = datetime.datetime.utcnow() - datetime.timedelta(minutes=5)
    expiry = start + datetime.timedelta(minutes=duration)
    sap = SharedAccessPolicy(
        AccessPolicy(start.strftime(date_format), expiry.strftime(date_format),
                     permission))
    signed_query = sas.generate_signed_query_string(resource_path,
                                                    RESOURCE_BLOB, sap)
    sas.permission_set = [Permission('/' + resource_path, signed_query)]

    res = WebResource()
    res.properties[SIGNED_RESOURCE_TYPE] = RESOURCE_BLOB
    res.properties[SHARED_ACCESS_PERMISSION] = permission
    res.path = '/{0}'.format(resource_path)
    res.request_url = 'https://{0}.blob.core.windows.net/{1}/{2}'.format(
        account_name, container_name, blob_name)
    res = sas.sign_request(res)
    return res.request_url
Exemplo n.º 2
0
def make_blob_sas_url(account_name,
                      account_key,
                      container_name,
                      blob_name,
                      duration=16):
    """
    Generate a Blob SAS URL to allow a client to upload a file.

    account_name: Storage account name.
    account_key: Storage account key.
    container_name: Storage container.
    blob_name: Blob name.
    duration: A timedelta representing duration until SAS expiration.
       SAS start date will be utcnow() minus one minute. Expiry date
       is start date plus duration.

    Returns the SAS URL.
    """
    sas = SharedAccessSignature(account_name, account_key)
    resource_path = '%s/%s' % (container_name, blob_name)
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start = datetime.datetime.utcnow() - datetime.timedelta(minutes=5)
    expiry = start + datetime.timedelta(minutes=duration)
    sap = SharedAccessPolicy(AccessPolicy(
            start.strftime(date_format), 
            expiry.strftime(date_format),
            'w'))
    signed_query = sas.generate_signed_query_string(resource_path, RESOURCE_BLOB, sap)
    sas.permission_set = [Permission('/' + resource_path, signed_query)]

    res = WebResource()
    res.properties[SIGNED_RESOURCE_TYPE] = RESOURCE_BLOB
    res.properties[SHARED_ACCESS_PERMISSION] = 'w'
    res.path = '/{0}'.format(resource_path)
    res.request_url = 'https://{0}.blob.core.windows.net/{1}/{2}'.format(account_name, container_name, blob_name)
    res = sas.sign_request(res)
    return res.request_url