def test_add_block_to_append_blob_with_container_sas(self): # SAS URL is calculated from storage key, so this test runs live only if TestMode.need_recording_file(self.test_mode): return # Arrange token = self.bs.generate_container_shared_access_signature( self.container_name, permission=ContainerPermissions.READ + ContainerPermissions.CREATE + ContainerPermissions.ADD, expiry=datetime.utcnow() + timedelta(hours=1), ) bs_with_sas = AppendBlobService( account_name=self.settings.STORAGE_ACCOUNT_NAME, sas_token=token, protocol=self.settings.PROTOCOL) # Act test_blob_name = "testblobname" text_blob_content = "test-blob-content" bs_with_sas.create_blob(self.container_name, blob_name=test_blob_name) bs_with_sas.append_blob_from_text(self.container_name, blob_name=test_blob_name, text=text_blob_content) blob_content = bs_with_sas.get_blob_to_text(self.container_name, blob_name=test_blob_name) # Assert self.assertEqual(blob_content.content, text_blob_content)
def azure_blob_storage(): try: # To create a blob service or connection to azure blob storage block_blob_service = BlockBlobService(account_name='account_name', account_key='account_key') # To create a container container_name ='container_name' block_blob_service.create_container(container_name) # Set access to a container such as public ,readonly,private block_blob_service.set_container_acl(container_name, public_access=PublicAccess.Container) # Create a file to upload to azure blob storage local_file_name ="Test.txt" path_to_file =os.path.join('local_path', local_file_name) # To write to the file local_file = open(full_path_to_file, 'w') local_file.write("hi peoplee") local_file.close() # Upload the created file, use local_file_name for the blob name block_blob_service.create_blob_from_path(container_name, local_file_name, full_path_to_file) # To List all the blobs in the container container_List = block_blob_service.list_blobs(container_name) for file in container_List: print("Blob name: "+{file.name}) # Download the blob(s). download_file_path = os.path.join('local_path', 'local_file_name') block_blob_service.get_blob_to_path(container_name, local_file_name, download_file_path) #delete a blob block_blob_service.delete_blob('container_name', 'blob_name') #Append to a blob service append_blob_service = AppendBlobService(account_name='myaccount', account_key='mykey') # The same containers can hold all types of blobs append_blob_service.create_container('container_name') #To append file must exists append_blob_service.create_blob('container_name', 'append_blob') #Append to a blob service append_blob_service.append_blob_from_text('container_name', 'append_blob', 'Hello, world!') append_blob = append_blob_service.get_blob_to_text('container_name', 'append_blob') # Clean up resources. This includes the container and the temp files block_blob_service.delete_container(container_name) except Exception as e:
#from azure.storage.blob import ContentSettings #block_blob_service.create_blob_from_path( # 'myseccontainer', # 'remotesecfiles3.txt', # 'localfiles3.txt', # content_settings=ContentSettings(content_type='text/html') # ) #### To list the blobs in a container, use the list_blobs method. This method returns a generator. #### The following code outputs the name of each blob in a container to the console. #generator = block_blob_service.list_blobs('myseccontainer') #for blob in generator: # print(blob.name) #### The following example demonstrates using get_blob_to_path to download the contents of the myblob blob and store it to the out-sunset.png file. #block_blob_service.get_blob_to_path('myseccontainer', 'remotesecf.txt', 'fromazure-out.txt') #### Finally, to delete a blob, call delete_blob. block_blob_service.delete_blob('myseccontainer', 'remotesecf.txt') #### The example below creates a new append blob and appends some data to it, simulating a simple logging operation. from azure.storage.blob import AppendBlobService append_blob_service = AppendBlobService(myaccount, mykey) #The same containers can hold all types of blobs append_blob_service.create_container('mycontainer') #Append blobs must be created before they are appended to append_blob_service.create_blob('mycontainer', 'myappendblob') append_blob_service.append_blob_from_text('mycontainer', 'myappendblob', u'Sinaq, cumle!') append_blob = append_blob_service.get_blob_to_text('mycontainer', 'myappendblob') print(append_blob)
FROM Kwinana_Plc_notification t1 join Kwinana_Plc_refference_data t2 on t1.Commodity = t2.Commodity order by AutoId DESC""" def wa_timenow(): #get current perth time dtn = datetime.now() + timedelta(hours=8) return dtn cursor.execute(sql) row = cursor.fetchone() outval = '' if row[0] == None: outval = ",".join([str(i) for i in row[1:]]) #connect to request-log blob and log request append_blob_service = AppendBlobService(account_name=myaccount, account_key=mykey) append_blob_service.append_blob_from_text( 'requestlogs', 'request.txt', "%s,%s " % (wa_timenow(), outval)) append_blob_service.append_blob_from_text('requestlogs', 'request.txt', "\n") append_blob = append_blob_service.get_blob_to_text('requestlogs', 'request.txt') #send response response = open(os.environ['res'], 'w') response.write(outval) response.close()
# generator = block_blob_service.list_blobs('mycontainer') # for blob in generator: # print(blob.name) # block_blob_service.get_blob_to_path('mycontainer', 'myblockblob', 'out-sunset.png') # block_blob_service.delete_blob('mycontainer', 'myblockblob') from azure.storage.blob import AppendBlobService append_blob_service = AppendBlobService(account_name='bobur', account_key='6e60FZapOXAmUbFBw0SpE1lHRP3RkXOMYRaalWmRBoz4+xI5tvjaJzxXuYyt+yfWxjPXpz5X3PmyIFiQmSkjbw==') # The same containers can hold all types of blobs append_blob_service.create_container('myappendcontainer') # Append blobs must be created before they are appended to append_blob_service.create_blob('myappendcontainer', 'myappendblob') append_blob_service.append_blob_from_text('myappendcontainer', 'myappendblob', u'Hello, world!') append_blob = append_blob_service.get_blob_to_text('myappendcontainer', 'myappendblob') print append_blob.content