本文整理汇总了Python中azure.storage.blob.BlockBlobService方法的典型用法代码示例。如果您正苦于以下问题:Python blob.BlockBlobService方法的具体用法?Python blob.BlockBlobService怎么用?Python blob.BlockBlobService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类azure.storage.blob
的用法示例。
在下文中一共展示了blob.BlockBlobService方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download_from_container
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def download_from_container(self, resource_group_name, account_name, container_name, files):
try:
secret_key = meta_lib.AzureMeta().list_storage_keys(resource_group_name, account_name)[0]
block_blob_service = BlockBlobService(account_name=account_name, account_key=secret_key)
for filename in files:
block_blob_service.get_blob_to_path(container_name, filename, filename)
return ''
except azure.common.AzureMissingResourceHttpError:
return ''
except Exception as err:
logging.info(
"Unable to download files from container: " + str(err) + "\n Traceback: " + traceback.print_exc(file=sys.stdout))
append_result(str({"error": "Unable to download files from container",
"error_message": str(err) + "\n Traceback: " + traceback.print_exc(
file=sys.stdout)}))
traceback.print_exc(file=sys.stdout)
示例2: list_container_content
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def list_container_content(self, resource_group_name, account_name, container_name):
try:
result = []
secret_key = list_storage_keys(resource_group_name, account_name)[0]
block_blob_service = BlockBlobService(account_name=account_name, account_key=secret_key)
content = block_blob_service.list_blobs(container_name)
for blob in content:
result.append(blob.name)
return result
except Exception as err:
logging.info(
"Unable to list container content: " + str(err) + "\n Traceback: " + traceback.print_exc(file=sys.stdout))
append_result(str({"error": "Unable to list container content",
"error_message": str(err) + "\n Traceback: " + traceback.print_exc(
file=sys.stdout)}))
traceback.print_exc(file=sys.stdout)
示例3: __init__
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def __init__(
self, blob_client: azureblob.BlockBlobService,
resource: str, blob_name: str, nglobalresources: int):
"""ContainerImageSaveThread ctor
:param azureblob.BlockBlobService blob_client: blob client
:param str resource: resource
:param str blob_name: resource blob name
:param int nglobalresources: number of global resources
"""
threading.Thread.__init__(self)
self.blob_client = blob_client
self.resource = resource
self.blob_name = blob_name
self.nglobalresources = nglobalresources
# add to downloading set
with _DIRECTDL_LOCK:
_DIRECTDL_DOWNLOADING.add(self.resource)
示例4: _check_file_and_upload
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def _check_file_and_upload(blob_client, file, key, container=None):
# type: (azure.storage.blob.BlockBlobService, tuple, str, str) -> None
"""Upload file to blob storage if necessary
:param azure.storage.blob.BlockBlobService blob_client: blob client
:param tuple file: file to upload
:param str key: blob container key
:param str container: absolute container override
"""
if file[0] is None:
return
contname = container or _STORAGE_CONTAINERS[key]
upload = True
# check if blob exists
try:
prop = blob_client.get_blob_properties(contname, file[0])
if (prop.properties.content_settings.content_md5 ==
util.compute_md5_for_file(file[1], True)):
logger.debug(
'remote file is the same for {}, skipping'.format(file[0]))
upload = False
except azure.common.AzureMissingResourceHttpError:
pass
if upload:
logger.info('uploading file {} as {!r}'.format(file[1], file[0]))
blob_client.create_blob_from_path(contname, file[0], str(file[1]))
示例5: delete_resource_file
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def delete_resource_file(blob_client, blob_name, federation_id=None):
# type: (azure.storage.blob.BlockBlobService, str) -> bool
"""Delete a resource file from blob storage
:param azure.storage.blob.BlockBlobService blob_client: blob client
:param str blob_name: blob name
:param str federation_id: federation id
"""
if util.is_not_empty(federation_id):
fedhash = hash_federation_id(federation_id)
container = '{}-{}'.format(
_STORAGE_CONTAINERS['blob_federation'], fedhash)
else:
container = _STORAGE_CONTAINERS['blob_resourcefiles']
try:
blob_client.delete_blob(container, blob_name)
logger.debug('blob {} deleted from container {}'.format(
blob_name, container))
except azure.common.AzureMissingResourceHttpError:
logger.warning('blob {} does not exist in container {}'.format(
blob_name, container))
return False
return True
示例6: upload_resource_files
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def upload_resource_files(blob_client, files):
# type: (azure.storage.blob.BlockBlobService, List[tuple]) -> dict
"""Upload resource files to blob storage
:param azure.storage.blob.BlockBlobService blob_client: blob client
:param list files: files to upload
:rtype: dict
:return: sas url dict
"""
sas_urls = {}
for file in files:
_check_file_and_upload(blob_client, file, 'blob_resourcefiles')
sas_urls[file[0]] = 'https://{}.blob.{}/{}/{}?{}'.format(
_STORAGEACCOUNT, _STORAGEACCOUNTEP,
_STORAGE_CONTAINERS['blob_resourcefiles'], file[0],
blob_client.generate_blob_shared_access_signature(
_STORAGE_CONTAINERS['blob_resourcefiles'], file[0],
permission=azureblob.BlobPermissions.READ,
expiry=datetime.datetime.utcnow() +
datetime.timedelta(days=_DEFAULT_SAS_EXPIRY_DAYS)
)
)
return sas_urls
示例7: upload_for_nonbatch
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def upload_for_nonbatch(blob_client, files, kind):
# type: (azure.storage.blob.BlockBlobService, List[tuple],
# str) -> List[str]
"""Upload files to blob storage for non-batch
:param azure.storage.blob.BlockBlobService blob_client: blob client
:param dict config: configuration dict
:param list files: files to upload
:param str kind: "remotefs", "monitoring" or "federation"
:rtype: list
:return: list of file urls
"""
if kind == 'federation':
kind = '{}_global'.format(kind.lower())
key = 'blob_{}'.format(kind.lower())
ret = []
for file in files:
_check_file_and_upload(blob_client, file, key)
ret.append('https://{}.blob.{}/{}/{}'.format(
_STORAGEACCOUNT, _STORAGEACCOUNTEP,
_STORAGE_CONTAINERS[key], file[0]))
return ret
示例8: _clear_blob_task_resourcefiles
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def _clear_blob_task_resourcefiles(blob_client, container, config):
# type: (azureblob.BlockBlobService, str, dict) -> None
"""Clear task resource file blobs in container
:param azure.storage.blob.BlockBlobService blob_client: blob client
:param str container: container to clear blobs from
:param dict config: configuration dict
"""
bs = settings.batch_shipyard_settings(config)
envfileloc = '{}taskrf-'.format(bs.storage_entity_prefix)
logger.info('deleting blobs with prefix: {}'.format(envfileloc))
try:
blobs = blob_client.list_blobs(container, prefix=envfileloc)
except azure.common.AzureMissingResourceHttpError:
logger.warning('container not found: {}'.format(container))
else:
for blob in blobs:
blob_client.delete_blob(container, blob.name)
示例9: delete_storage_containers_boot_diagnostics
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def delete_storage_containers_boot_diagnostics(
blob_client, vm_name, vm_id):
# type: (azureblob.BlockBlobService, str, str) -> None
"""Delete storage containers used for remotefs bootdiagnostics
:param azure.storage.blob.BlockBlobService blob_client: blob client
:param str vm_name: vm name
:param str vm_id: vm id
"""
name = re.sub('[\W_]+', '', vm_name) # noqa
contname = 'bootdiagnostics-{}-{}'.format(
name[0:min((9, len(name)))], vm_id)
logger.info('deleting container: {}'.format(contname))
try:
blob_client.delete_container(contname)
except azure.common.AzureMissingResourceHttpError:
logger.warning('container not found: {}'.format(contname))
示例10: cleanup_with_del_pool
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def cleanup_with_del_pool(blob_client, table_client, config, pool_id=None):
# type: (azureblob.BlockBlobService, azuretable.TableService,
# dict, str) -> None
"""Special cleanup routine in combination with delete pool
:param azure.storage.blob.BlockBlobService blob_client: blob client
:param azure.cosmosdb.table.TableService table_client: table client
:param dict config: configuration dict
:param str pool_id: pool id
"""
if util.is_none_or_empty(pool_id):
pool_id = settings.pool_id(config)
if not util.confirm_action(
config, 'delete/cleanup of Batch Shipyard metadata in storage '
'containers associated with {} pool'.format(pool_id)):
return
clear_storage_containers(
blob_client, table_client, config, tables_only=True, pool_id=pool_id)
delete_storage_containers(
blob_client, table_client, config, skip_tables=True)
示例11: get_container_sas_token
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def get_container_sas_token(block_blob_client,
container_name, blob_permissions):
"""
Obtains a shared access signature granting the specified permissions to the
container.
:param block_blob_client: A blob service client.
:type block_blob_client: `azure.storage.blob.BlockBlobService`
:param str container_name: The name of the Azure Blob storage container.
:param BlobPermissions blob_permissions:
:rtype: str
:return: A SAS token granting the specified permissions to the container.
"""
# Obtain the SAS token for the container, setting the expiry time and
# permissions. In this case, no start time is specified, so the shared
# access signature becomes valid immediately.
container_sas_token = \
block_blob_client.generate_container_shared_access_signature(
container_name,
permission=blob_permissions,
expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=2))
return container_sas_token
示例12: get_container_sas_token
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def get_container_sas_token(block_blob_client,
container_name, blob_permissions):
"""
Obtains a shared access signature granting the specified permissions to the
container.
:param block_blob_client: A blob service client.
:type block_blob_client: `azure.storage.blob.BlockBlobService`
:param str container_name: The name of the Azure Blob storage container.
:param BlobPermissions blob_permissions:
:rtype: str
:return: A SAS token granting the specified permissions to the container.
"""
# Obtain the SAS token for the container, setting the expiry time and
# permissions. In this case, no start time is specified, so the shared
# access signature becomes valid immediately.
container_sas_token = \
block_blob_client.generate_container_shared_access_signature(
container_name,
permission=blob_permissions,
expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=2))
return container_sas_token
示例13: get_blob_client
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def get_blob_client() -> blob.BlockBlobService:
if not storage_resource_id:
return blob.BlockBlobService(
account_name=storage_account_name, account_key=storage_account_key, endpoint_suffix=storage_account_suffix)
else:
credentials = ServicePrincipalCredentials(
client_id=client_id, secret=credential, tenant=tenant_id, resource="https://management.core.windows.net/")
m = RESOURCE_ID_PATTERN.match(storage_resource_id)
accountname = m.group("account")
subscription = m.group("subscription")
resourcegroup = m.group("resourcegroup")
mgmt_client = StorageManagementClient(credentials, subscription)
key = (mgmt_client.storage_accounts.list_keys(resource_group_name=resourcegroup, account_name=accountname)
.keys[0].value)
storage_client = CloudStorageAccount(accountname, key)
return storage_client.create_block_blob_service()
示例14: create_sas_token
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def create_sas_token(container_name, blob_name, permission, blob_client, expiry=None, timeout=None):
"""
Create a blob sas token
:param blob_client: The storage block blob client to use.
:type blob_client: `azure.storage.blob.BlockBlobService`
:param str container_name: The name of the container to upload the blob to.
:param str blob_name: The name of the blob to upload the local file to.
:param expiry: The SAS expiry time.
:type expiry: `datetime.datetime`
:param int timeout: timeout in minutes from now for expiry,
will only be used if expiry is not specified
:return: A SAS token
:rtype: str
"""
if expiry is None:
if timeout is None:
timeout = 30
expiry = datetime.datetime.utcnow() + datetime.timedelta(minutes=timeout)
return blob_client.generate_blob_shared_access_signature(
container_name, blob_name, permission=permission, expiry=expiry)
示例15: initialize
# 需要导入模块: from azure.storage import blob [as 别名]
# 或者: from azure.storage.blob import BlockBlobService [as 别名]
def initialize(self, host):
"""
The EventProcessorHost can't pass itself to the AzureStorageCheckpointLeaseManager
constructor because it is still being constructed. Do other initialization here
also because it might throw and hence we don't want it in the constructor.
"""
self.host = host
self.storage_client = BlockBlobService(account_name=self.storage_account_name,
account_key=self.storage_account_key,
sas_token=self.storage_sas_token,
endpoint_suffix=self.endpoint_suffix,
connection_string=self.connection_string,
request_session=self.request_session)
self.consumer_group_directory = self.storage_blob_prefix + self.host.eh_config.consumer_group
# Checkpoint Managment Methods