def create_file_share_saskey( storage_settings, file_share, kind, create_share=False): # type: (StorageCredentialSettings, str, str, bool) -> str """Create a saskey for a file share with a 7day expiry time :param StorageCredentialsSettings storage_settings: storage settings :param str file_share: file share :param str kind: ingress or egress :param bool create_share: create file share :rtype: str :return: saskey """ file_client = azurefile.FileService( account_name=storage_settings.account, account_key=storage_settings.account_key, endpoint_suffix=storage_settings.endpoint) if create_share: file_client.create_share(file_share, fail_on_exist=False) if kind == 'ingress': perm = azurefile.SharePermissions(read=True, list=True) elif kind == 'egress': perm = azurefile.SharePermissions( read=True, write=True, delete=True, list=True) else: raise ValueError('{} type of transfer not supported'.format(kind)) return file_client.generate_share_shared_access_signature( file_share, perm, expiry=datetime.datetime.utcnow() + datetime.timedelta(days=_DEFAULT_SAS_EXPIRY_DAYS) )
def create_saskey( storage_settings, path, file, create, list_perm, read, write, delete, expiry_days=None): # type: (settings.StorageCredentialsSettings, str, bool, bool, bool, bool, # bool, bool, int) -> None """Create an object-level sas key :param settings.StorageCredentialsSetting storage_settings: storage settings :param str path: path :param bool file: file sas :param bool create: create perm :param bool list_perm: list perm :param bool read: read perm :param bool write: write perm :param bool delete: delete perm :param int expiry_days: expiry in days :rtype: str :return: sas token """ if expiry_days is None: expiry_days = _DEFAULT_SAS_EXPIRY_DAYS if file: client = azurefile.FileService( account_name=storage_settings.account, account_key=storage_settings.account_key, endpoint_suffix=storage_settings.endpoint) tmp = path.split('/') if len(tmp) < 1: raise ValueError('path is invalid: {}'.format(path)) share_name = tmp[0] if len(tmp) == 1: perm = azurefile.SharePermissions( read=read, write=write, delete=delete, list=list_perm) sas = client.generate_share_shared_access_signature( share_name=share_name, permission=perm, expiry=datetime.datetime.utcnow() + datetime.timedelta(days=expiry_days) ) else: if len(tmp) == 2: directory_name = '' file_name = tmp[1] else: directory_name = tmp[1] file_name = '/'.join(tmp[2:]) perm = azurefile.FilePermissions( read=read, create=create, write=write, delete=delete) sas = client.generate_file_shared_access_signature( share_name=share_name, directory_name=directory_name, file_name=file_name, permission=perm, expiry=datetime.datetime.utcnow() + datetime.timedelta(days=expiry_days) ) else: client = azureblob.BlockBlobService( account_name=storage_settings.account, account_key=storage_settings.account_key, endpoint_suffix=storage_settings.endpoint) tmp = path.split('/') if len(tmp) < 1: raise ValueError('path is invalid: {}'.format(path)) container_name = tmp[0] if len(tmp) == 1: perm = azureblob.ContainerPermissions( read=read, write=write, delete=delete, list=list_perm) sas = client.generate_container_shared_access_signature( container_name=container_name, permission=perm, expiry=datetime.datetime.utcnow() + datetime.timedelta(days=expiry_days) ) else: blob_name = '/'.join(tmp[1:]) perm = azureblob.BlobPermissions( read=read, create=create, write=write, delete=delete) sas = client.generate_blob_shared_access_signature( container_name=container_name, blob_name=blob_name, permission=perm, expiry=datetime.datetime.utcnow() + datetime.timedelta(days=expiry_days) ) return sas