def test_soft_delete_blob_without_snapshots(self): try: # Arrange self._enable_soft_delete() blob_name = self._create_block_blob() # Soft delete the blob self.bs.delete_blob(self.container_name, blob_name) blob_list = list( self.bs.list_blobs(self.container_name, include=Include(deleted=True))) # Assert self.assertEqual(len(blob_list), 1) self._assert_blob_is_soft_deleted(blob_list[0]) # list_blobs should not list soft deleted blobs if Include(deleted=True) is not specified blob_list = list(self.bs.list_blobs(self.container_name)) # Assert self.assertEqual(len(blob_list), 0) # Restore blob with undelete self.bs.undelete_blob(self.container_name, blob_name) blob_list = list( self.bs.list_blobs(self.container_name, include=Include(deleted=True))) # Assert self.assertEqual(len(blob_list), 1) self._assert_blob_not_soft_deleted(blob_list[0]) finally: self._disable_soft_delete()
def test_soft_delete_only_snapshots_of_blob(self): try: # Arrange self._enable_soft_delete() blob_name = self._create_block_blob() blob_snapshot_1 = self.bs.snapshot_blob(self.container_name, blob_name) blob_snapshot_2 = self.bs.snapshot_blob(self.container_name, blob_name) # Soft delete all snapshots self.bs.delete_blob(self.container_name, blob_name, delete_snapshots=DeleteSnapshot.Only) blob_list = list( self.bs.list_blobs(self.container_name, include=Include(deleted=True, snapshots=True))) # Assert self.assertEqual(len(blob_list), 3) for blob in blob_list: if blob.snapshot == blob_snapshot_1.snapshot: self._assert_blob_is_soft_deleted(blob) elif blob.snapshot == blob_snapshot_2.snapshot: self._assert_blob_is_soft_deleted(blob) else: self._assert_blob_not_soft_deleted(blob) # list_blobs should not list soft deleted blob snapshots if Include(deleted=True) is not specified blob_list = list( self.bs.list_blobs(self.container_name, include=Include(snapshots=True))) # Assert self.assertEqual(len(blob_list), 1) # Restore snapshots with undelete self.bs.undelete_blob(self.container_name, blob_name) blob_list = list( self.bs.list_blobs(self.container_name, include=Include(deleted=True, snapshots=True))) # Assert self.assertEqual(len(blob_list), 3) for blob in blob_list: self._assert_blob_not_soft_deleted(blob) finally: self._disable_soft_delete()
def validate_included_datasets(namespace): if namespace.include: include = namespace.include if set(include) - set('cms'): help_string = '(c)opy-info (m)etadata (s)napshots' raise ValueError('valid values are {} or a combination thereof.'.format(help_string)) namespace.include = Include('s' in include, 'm' in include, False, 'c' in include)
def test_list_blobs_with_include_multiple(self): # Arrange container_name = self._create_container() data = b'hello world' self.bs.create_blob_from_bytes (container_name, 'blob1', data, metadata={'number': '1', 'name': 'bob'}) self.bs.create_blob_from_bytes (container_name, 'blob2', data, metadata={'number': '2', 'name': 'car'}) self.bs.snapshot_blob(container_name, 'blob1') # Act blobs = list(self.bs.list_blobs(container_name, include=Include(snapshots=True, metadata=True))) # Assert self.assertEqual(len(blobs), 3) self.assertEqual(blobs[0].name, 'blob1') self.assertIsNotNone(blobs[0].snapshot) self.assertEqual(blobs[0].metadata['number'], '1') self.assertEqual(blobs[0].metadata['name'], 'bob') self.assertEqual(blobs[1].name, 'blob1') self.assertIsNone(blobs[1].snapshot) self.assertEqual(blobs[1].metadata['number'], '1') self.assertEqual(blobs[1].metadata['name'], 'bob') self.assertEqual(blobs[2].name, 'blob2') self.assertIsNone(blobs[2].snapshot) self.assertEqual(blobs[2].metadata['number'], '2') self.assertEqual(blobs[2].metadata['name'], 'car')
def test_soft_delete_with_leased_blob(self): try: # Arrange self._enable_soft_delete() blob_name = self._create_block_blob() lease_id = self.bs.acquire_blob_lease(self.container_name, blob_name) # Soft delete the blob without lease_id should fail with self.assertRaises(AzureHttpError): self.bs.delete_blob(self.container_name, blob_name) # Soft delete the blob self.bs.delete_blob(self.container_name, blob_name, lease_id=lease_id) blob_list = list( self.bs.list_blobs(self.container_name, include=Include(deleted=True))) # Assert self.assertEqual(len(blob_list), 1) self._assert_blob_is_soft_deleted(blob_list[0]) # list_blobs should not list soft deleted blobs if Include(deleted=True) is not specified blob_list = list(self.bs.list_blobs(self.container_name)) # Assert self.assertEqual(len(blob_list), 0) # Restore blob with undelete, this also gets rid of the lease self.bs.undelete_blob(self.container_name, blob_name) blob_list = list( self.bs.list_blobs(self.container_name, include=Include(deleted=True))) # Assert self.assertEqual(len(blob_list), 1) self._assert_blob_not_soft_deleted(blob_list[0]) finally: self._disable_soft_delete()
def list_blobs(self): container_name = self._create_container() self.service.create_blob_from_bytes(container_name, 'blob1', b'') self.service.create_blob_from_bytes(container_name, 'blob2', b'') self.service.create_blob_from_bytes(container_name, 'dir1/blob1', b'') # Basic # List from root blobs = list(self.service.list_blobs(container_name)) print('Basic List:') for blob in blobs: print(blob.name) # blob1, blob2, dir1/blob1 # Prefix blobs = list(self.service.list_blobs(container_name, prefix='blob')) print('Prefix List:') for blob in blobs: print(blob.name) # blob1, blob2 # Num results blobs = list(self.service.list_blobs(container_name, num_results=2)) print('Num Results List:') for blob in blobs: print(blob.name) # blob1, blob2 # Virtual 'directories' w/ delimiter blobs_and_dirs = list(self.service.list_blobs(container_name, delimiter='/')) print('Delimiter List:') for res in blobs_and_dirs: print(res.name) # dir1/, blob1, blob2 # Metadata self.service.set_blob_metadata(container_name, 'blob1', {'val1': 'foo', 'val2': 'blah'}) blobs = list(self.service.list_blobs(container_name, include=Include.METADATA)) blob = next((b for b in blobs if b.name == 'blob1'), None) metadata = blob.metadata # {'val1': 'foo', 'val2': 'blah'} # Snapshot self.service.snapshot_blob(container_name, 'blob1') blobs = list(self.service.list_blobs(container_name, include=Include.SNAPSHOTS)) print('Snapshot List:') for blob in blobs: print(blob.name + ( '-' + blob.snapshot) if blob.snapshot else '') # blob1, blob1-{snapshot_time}, blob2, dir1/blob1 # Copy source_blob_url = self.service.make_blob_url(container_name, 'blob1') self.service.copy_blob(container_name, 'copyblob', source_blob_url) blobs = list(self.service.list_blobs(container_name, include=Include.COPY)) copy_blob = next((b for b in blobs if b.name == 'copyblob'), None) copy_source = copy_blob.properties.copy.source # copy properties, including source will be populated # Uncommitted self.service.put_block(container_name, 'uncommittedblob', b'block data', 1) blobs = list(self.service.list_blobs(container_name, include=Include.UNCOMMITTED_BLOBS)) uncommitted_blob = next((b for b in blobs if b.name == 'uncommittedblob'), None) # will exist # Multiple includes blobs = list(self.service.list_blobs(container_name, include=Include(uncommitted_blobs=True, snapshots=True))) print('Multiple Includes List:') for blob in blobs: print(blob.name) # blob1, blob1, blob2, copyblob, dir1/blob1, uncommittedblob