def test_create_blob_service_empty_credentials(self): # Arrange # Act bad_account = CloudStorageAccount('', '') with self.assertRaises(WindowsAzureError): service = bad_account.create_blob_service()
def get_storage_account_details(subscription_id, creds, resource_group_name, account_name): storage_client = StorageManagementClient(creds, subscription_id) account_result = storage_client.storage_accounts.get_properties( resource_group_name, account_name, ) storage_account_keys = storage_client.storage_accounts.list_keys( resource_group_name, account_name, ) account_key = storage_account_keys.key1 account = CloudStorageAccount(account_name, account_key) blob_service = account.create_blob_service() file_service = account.create_file_service() queue_service = account.create_queue_service() table_service = account.create_table_service() model = StorageAccountDetails() model.account_props = account_result model.account_keys = storage_account_keys model.blob_containers = blob_service.iterate_containers() model.queues = queue_service.iterate_queues() #TODO: find out why listing shares doesn't work #model.shares = file_service.iterate_shares() model.shares = [] model.tables = table_service.iterate_tables() model.blob_service_properties = blob_service.get_blob_service_properties() model.queue_service_properties = queue_service.get_queue_service_properties( ) model.table_service_properties = table_service.get_table_service_properties( ) return model
def test_create_blob_service_empty_credentials(self): # Arrange # Act bad_account = CloudStorageAccount('', '') with self.assertRaises(ValueError): service = bad_account.create_blob_service()
def get_storage_account_details(creds, resource_group_name, account_name): storage_client = StorageManagementClient(creds) account_result = storage_client.storage_accounts.get_properties(resource_group_name, account_name) keys_result = storage_client.storage_accounts.list_keys(resource_group_name, account_name) account_key = keys_result.storage_account_keys.key1 account = CloudStorageAccount(account_name, account_key) blob_service = account.create_blob_service() file_service = account.create_file_service() queue_service = account.create_queue_service() table_service = account.create_table_service() model = StorageAccountDetails() model.account_props = account_result.storage_account model.account_keys = keys_result.storage_account_keys model.blob_containers = blob_service.iterate_containers() model.queues = queue_service.iterate_queues() # TODO: find out why listing shares doesn't work # model.shares = file_service.iterate_shares() model.shares = [] model.tables = table_service.iterate_tables() model.blob_service_properties = blob_service.get_blob_service_properties() model.queue_service_properties = queue_service.get_queue_service_properties() model.table_service_properties = table_service.get_table_service_properties() return model
class CloudStorageAccountTest(AzureTestCase): def setUp(self): self.account = CloudStorageAccount( account_name=credentials.getStorageServicesName(), account_key=credentials.getStorageServicesKey()) #--Test cases -------------------------------------------------------- def test_create_blob_service(self): # Arrange # Act service = self.account.create_blob_service() # Assert self.assertIsNotNone(service) self.assertIsInstance(service, BlobService) self.assertEqual(service.account_name, credentials.getStorageServicesName()) self.assertEqual(service.account_key, credentials.getStorageServicesKey()) def test_create_blob_service_empty_credentials(self): # Arrange # Act bad_account = CloudStorageAccount('', '') with self.assertRaises(WindowsAzureError): service = bad_account.create_blob_service() # Assert def test_create_table_service(self): # Arrange # Act service = self.account.create_table_service() # Assert self.assertIsNotNone(service) self.assertIsInstance(service, TableService) self.assertEqual(service.account_name, credentials.getStorageServicesName()) self.assertEqual(service.account_key, credentials.getStorageServicesKey()) def test_create_queue_service(self): # Arrange # Act service = self.account.create_queue_service() # Assert self.assertIsNotNone(service) self.assertIsInstance(service, QueueService) self.assertEqual(service.account_name, credentials.getStorageServicesName()) self.assertEqual(service.account_key, credentials.getStorageServicesKey())
class StorageAccountTest(ExtendedTestCase): def setUp(self): self.account_name = 'storagename' self.account_key = 'NzhL3hKZbJBuJ2484dPTR+xF30kYaWSSCbs2BzLgVVI1woqeST/1IgqaLm6QAOTxtGvxctSNbIR/1hW8yH+bJg==' self.account = CloudStorageAccount(self.account_name, self.account_key) #--Test cases -------------------------------------------------------- def test_create_blob_service(self): # Arrange # Act service = self.account.create_blob_service() # Assert self.assertIsNotNone(service) self.assertIsInstance(service, BlobService) self.assertEqual(service.account_name, self.account_name) self.assertEqual(service.account_key, self.account_key) def test_create_blob_service_empty_credentials(self): # Arrange # Act bad_account = CloudStorageAccount('', '') with self.assertRaises(ValueError): service = bad_account.create_blob_service() # Assert def test_create_table_service(self): # Arrange # Act service = self.account.create_table_service() # Assert self.assertIsNotNone(service) self.assertIsInstance(service, TableService) self.assertEqual(service.account_name, self.account_name) self.assertEqual(service.account_key, self.account_key) def test_create_queue_service(self): # Arrange # Act service = self.account.create_queue_service() # Assert self.assertIsNotNone(service) self.assertIsInstance(service, QueueService) self.assertEqual(service.account_name, self.account_name) self.assertEqual(service.account_key, self.account_key)
class StorageAccountTest(StorageTestCase): def setUp(self): self.account_name = 'storagename' self.account_key = 'NzhL3hKZbJBuJ2484dPTR+xF30kYaWSSCbs2BzLgVVI1woqeST/1IgqaLm6QAOTxtGvxctSNbIR/1hW8yH+bJg==' self.account = CloudStorageAccount(self.account_name, self.account_key) #--Test cases -------------------------------------------------------- def test_create_blob_service(self): # Arrange # Act service = self.account.create_blob_service() # Assert self.assertIsNotNone(service) self.assertIsInstance(service, BlobService) self.assertEqual(service.account_name, self.account_name) self.assertEqual(service.account_key, self.account_key) def test_create_blob_service_empty_credentials(self): # Arrange # Act bad_account = CloudStorageAccount('', '') with self.assertRaises(ValueError): service = bad_account.create_blob_service() # Assert def test_create_table_service(self): # Arrange # Act service = self.account.create_table_service() # Assert self.assertIsNotNone(service) self.assertIsInstance(service, TableService) self.assertEqual(service.account_name, self.account_name) self.assertEqual(service.account_key, self.account_key) def test_create_queue_service(self): # Arrange # Act service = self.account.create_queue_service() # Assert self.assertIsNotNone(service) self.assertIsInstance(service, QueueService) self.assertEqual(service.account_name, self.account_name) self.assertEqual(service.account_key, self.account_key)
def store_file(data, file_name, params=None): from django.conf import settings from azure.storage import CloudStorageAccount file_name = '{}.{}'.format(file_name, settings.DEFAULT_FILE_FORMAT) storage_client = CloudStorageAccount(settings.AZURE_ACCOUNT_NAME, settings.AZURE_ACCOUNT_KEY) blob_service = storage_client.create_blob_service() blob_service.put_block_blob_from_bytes( settings.AZURE_CONTAINER, file_name, data, content_language=settings.DEFAULT_FILE_CONTENT_LANGUAGE, x_ms_blob_content_type=settings.DEFAULT_FILE_CONTENT_TYPE ) file_link = blob_service.make_blob_url(settings.AZURE_CONTAINER, file_name) create_record(file_link, file_name, params)
# # The CloudStorageAccount provides factory methods for the queue, table, and # blob services. # # See http://go.microsoft.com/fwlink/?linkid=246933 for Storage documentation. # STORAGE_ACCOUNT_NAME = '__paste_your_storage_account_name_here__' STORAGE_ACCOUNT_KEY = '__paste_your_storage_key_here__' if os.environ.get('EMULATED', '').lower() == 'true': # Running in the emulator, so use the development storage account storage_account = CloudStorageAccount(None, None) else: storage_account = CloudStorageAccount(STORAGE_ACCOUNT_NAME, STORAGE_ACCOUNT_KEY) blob_service = storage_account.create_blob_service() table_service = storage_account.create_table_service() queue_service = storage_account.create_queue_service() # # Service Bus is a messaging solution for applications. It sits between # components of your applications and enables them to exchange messages in a # loosely coupled way for improved scale and resiliency. # # See http://go.microsoft.com/fwlink/?linkid=246934 for Service Bus documentation. # SERVICE_BUS_NAMESPACE = '__paste_your_service_bus_namespace_here__' SERVICE_BUS_KEY = '__paste_your_service_bus_key_here__' bus_service = ServiceBusService(SERVICE_BUS_NAMESPACE, SERVICE_BUS_KEY, issuer='owner')
class AzCmd(object): _client = None _service = None def __init__(self, account, key): self.connect(account, key) def connect(self, account, key): if self._client is None: from azure.storage import CloudStorageAccount self._client = CloudStorageAccount(account, key) if self._service is None: self._service = self._client.create_blob_service() def mkdir(self, container_name): container = self._get_container_name(container_name) return self._service.create_container(container) def list_containers(self): return self._service.list_containers() def rmdir(self, container_name): container_name = container_name.rstrip('/') + '/' container = self._get_container_name(container_name) if container == container_name: return self._service.delete_container(container) else: blobprefix = self._get_blob_prefix(container_name) blobs = self._service.list_blobs(container, blobprefix) for blob in blobs: self._service.delete_blob(container, blob.name) def put(self, local, remote): container = self._get_container_name(remote) blobname = self._get_blob_name(remote) try: self._service.put_block_blob_from_path(container, blobname, local) except azure.common.AzureMissingResourceHttpError: self.mkdir(container) self._service.put_block_blob_from_path(container, blobname, local) def get(self, remote, local): container = self._get_container_name(remote) blobname = self._get_blob_name(remote) self._service.get_blob_to_path(container, blobname, local) def ls(self, path): path = path.strip('/') + '/' container = self._get_container_name(path) prefix = self._get_blob_prefix(path) bloblist = list() lastPrefix = None for blob in self._service.list_blobs(container, prefix).blobs: blobprefix = self._get_blob_prefix("%s/%s" % (container, blob.name)) if blobprefix is None: if prefix is not None: blob.name = blob.name[len(prefix) + 1:] setattr(blob, 'directory', False) bloblist.append(blob) else: if blobprefix == prefix: blob.name = blob.name[len(prefix) + 1:] setattr(blob, 'directory', False) bloblist.append(blob) elif blobprefix != lastPrefix: if prefix is not None: thisprefix = blobprefix[len(prefix) + 1:] else: thisprefix = blobprefix if len(thisprefix.split('/')) < 2: blob.name = thisprefix setattr(blob, 'directory', True) bloblist.append(blob) lastPrefix = blobprefix return bloblist def rm(self, remote): container = self._get_container_name(remote) blobname = self._get_blob_name(remote) self._service.delete_blob(container, blobname) def _get_container_name(self, path): container, sep, blobname = path.partition('/') return container def _get_blob_prefix(self, path): blobname = self._get_blob_name(path) prefix, sep, filename = blobname.rpartition('/') if len(prefix) > 0: return prefix else: return None def _get_blob_name(self, path): container, sep, blobname = path.partition('/') return blobname
# The CloudStorageAccount provides factory methods for the queue, table, and # blob services. # # See http://go.microsoft.com/fwlink/?linkid=246933 for Storage documentation. # STORAGE_ACCOUNT_NAME = '__paste_your_storage_account_name_here__' STORAGE_ACCOUNT_KEY = '__paste_your_storage_key_here__' if os.environ.get('EMULATED', '').lower() == 'true': # Running in the emulator, so use the development storage account storage_account = CloudStorageAccount(None, None) else: storage_account = CloudStorageAccount(STORAGE_ACCOUNT_NAME, STORAGE_ACCOUNT_KEY) blob_service = storage_account.create_blob_service() table_service = storage_account.create_table_service() queue_service = storage_account.create_queue_service() # # Service Bus is a messaging solution for applications. It sits between # components of your applications and enables them to exchange messages in a # loosely coupled way for improved scale and resiliency. # # See http://go.microsoft.com/fwlink/?linkid=246934 for Service Bus documentation. # SERVICE_BUS_NAMESPACE = '__paste_your_service_bus_namespace_here__' SERVICE_BUS_KEY = '__paste_your_service_bus_key_here__' bus_service = ServiceBusService(SERVICE_BUS_NAMESPACE, SERVICE_BUS_KEY, issuer='owner')