Home > other >  How to use PowerShell management Azure blobs
How to use PowerShell management Azure blobs

Time:09-21

how to manage the Azure blob
Azure Blob storage is used to store large amounts of unstructured data services (such as text or binary data), the data can be access via HTTP or HTTPS from all over the world, this section assumes that the user is familiar with Azure Blob storage service concept,

How to create a container
Each Blob in the Azure storage must be in a container, you can use the New - AzureStorageContainer pre-existing cmdlets create dedicated container:
 $StorageContainerName="yourcontainername" 
New - AzureStorageContainer -name $StorageContainerName - Permission Off

The
reference
Note
Have three levels of anonymous read access: Off, Blob and Container, to prevent to anonymous access Blob, please send the Permission parameter is set to Off, by default, the new Container is a special Container, and can only access by the account owner, to allow for anonymous Blob resources public read access, but does not allow access to a list of Container metadata or containers of Blob, please set the Permission parameters for Blob, to allow for Blob resources, Container metadata and containers of Blob completely open read access list, please send the Permission parameter is set to the Container, for details please refer to the management of the Container and anonymous read access Blob,


how to upload the Blob to container
Azure Blob storage support Block Blob Blob, and Page for more details, please refer to the Understanding of Block Blobs, Append Blobs, and Page Blobs (understand Blob, additional Blob Blob) and Page,
To upload a Blob to a container, you can use the Set - AzureStorageBlobContent pre-existing cmdlets, by default, this command will upload a local file to block Blob, if you want to specify the type of the Blob, can use - BlobType parameters,
The following example will run the Get - ChildItem pre-existing cmdlets to Get all the files in the specified folder, and then, through the use of the pipeline operator to pass these files to a pre-existing cmdlets, under the Set - AzureStorageBlobContent pre-existing cmdlets upload a local file to the container:
 Get ChildItem - Path C: \ '\ * | Set - AzureStorageBlobContent - Container "yourcontainername" 


how to download a Blob from the container
The following example demonstrates how to download a Blob from the container, the example first use storage account context (including storage account name and its main access key) and Azure storage connection is established, and then, the sample using the Get - AzureStorageBlob pre-existing cmdlets retrieval Blob reference, next, the sample using the Get - AzureStorageBlobContent pre-existing cmdlets to Blob downloaded to the local target folder,
 # Define the variables. 
$ContainerName="yourcontainername"
$DestinationFolder="C: \ DownloadImages"

# Define the storage account and context.
$StorageAccountName="yourstorageaccount"
$StorageAccountKey="Storage key for yourstorageaccount ends with=="
$Ctx=New AzureStorageContext - StorageAccountName $StorageAccountName - StorageAccountKey $StorageAccountKey

# List all blobs in a container.
$blobs=Get AzureStorageBlob - Container $ContainerName - Context $Ctx

# Download blobs from a container.
New - Item - Path $DestinationFolder - ItemType Directory - Force
$blobs | Get - $DestinationFolder AzureStorageBlobContent - Destination - the Context $Ctx


how to copy a Blob to another storage containers
Can store account and regional asynchronous replication across the Blob, the following example shows how in two different storage account, copies a Blob of storage containers to another storage containers, the example first set the source and target variables stored account, and create the storage context of each account, next, the example USES the Start - AzureStorageBlobCopy pre-existing cmdlets to Blob containers from the source is copied to the target container, this example assumes the source storage account, the target account and the container has been in existence,
 # Define the source storage into account and the context. 
$SourceStorageAccountName="yoursourcestorageaccount"
$SourceStorageAccountKey="Storage key for yoursourcestorageaccount"
$SrcContainerName="yoursrccontainername"
$SourceContext=New AzureStorageContext - StorageAccountName $SourceStorageAccountName - StorageAccountKey $SourceStorageAccountKey

# Define the destination storage account and context.
$DestStorageAccountName="yourdeststorageaccount"
$DestStorageAccountKey="Storage key for yourdeststorageaccount"
$DestContainerName="DestContainerName"
$DestContext=New AzureStorageContext - StorageAccountName $DestStorageAccountName - StorageAccountKey $DestStorageAccountKey

# Get a reference to blobs in the source container.
$blobs=Get AzureStorageBlob - Container $SrcContainerName - Context $SourceContext

# Copy blobs from one container to another.
$blobs | Start - AzureStorageBlobCopy - DestContainer $DestContainerName - DestContext $DestContext

Please note that this example performs asynchronous replication,

how to copy a Blob from the auxiliary position
From a enabled RA - GRS account assistant position copy Blob,
 # define secondary storage context using a connection string constructed from secondary endpoints. 
$SrcContext=New - AzureStorageContext - the ConnectionString DefaultEndpointsProtocol=HTTPS;" AccountName=* * *; AccountKey=* * *; BlobEndpoint=http://* * * - secondary. Blob. Core. Chinacloudapi. Cn. FileEndpoint=http://- secondary. * * * file. Core. Chinacloudapi. Cn. QueueEndpoint=http://* * * - secondary. Queue. Core. Chinacloudapi. Cn. TableEndpoint=http://* * * - secondary. Table. Core. Chinacloudapi. Cn."
Start - AzureStorageBlobCopy - Container * * * * * * - Blob - Context $SrcContext - DestContainer * * * * * * - DestBlob DestContext $DestContext


how do I remove a Blob
If you want to delete the Blob, you first need to obtain a Blob reference, then the call referred to Remove - AzureStorageBlob pre-existing cmdlets, the example below to delete all the Blob, in a given container storage account variables set the sample first, and create a context, then, the sample using the Get - AzureStorageBlob pre-existing cmdlets retrieval Blob references, and run the Remove - AzureStorageBlob pre-existing cmdlets delete Azure storage within a container of Blob,
 # Define the storage account and context. 
$StorageAccountName="yourstorageaccount"
$StorageAccountKey="Storage key for yourstorageaccount ends with=="
$ContainerName="ContainerName"
nullnullnullnullnullnullnullnullnullnull
  • Related