Create a container and upload blobs using PowerShell
Now you’ll create a container and upload some blobs. In the example, the test files are in
D:\_TestImages. That path is used when uploading those files to Blob storage.
Note These cmdlets are Azure Storage data-plane cmdlets, not Azure Service Management (ASM)
or Azure Resource Manager cmdlets, which are management-plane cmdlets. The cmdlet to create a
storage account is a management-plane cmdlet. These data-plane cmdlets can be used with both
ASM and Resource Manager storage accounts.
128
CHAPTER 4 | Azure Storage
If you’re not running the PowerShell ISE and are logged into your Azure account, do that now. You’re
going to create a script that you can save and use later. In addition to the path to your local pictures,
you will need the name and access key of your storage account.
1.
Set up variable names for the storage account name and key—$StorageAccountName and
$StorageAccountKey. Fill in your storage account name and key here.
$StorageAccountName = "yourStorageAccountName"
$StorageAccountKey = "yourStorageAccountKey"
2.
Next, you’ll define the storage account context using the storage account name and key. You will
use this context for authentication with subsequent commands against the storage account. This
is easier (and safer) than specifying the storage account name and key all the time.
$ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName `
-StorageAccountKey $StorageAccountKey
Note that there is a continuation character (the backward tick mark) at the end of the first line.
3.
Next, you’ll add a variable for the name of your container, then you’ll create the container. The
example uses test-ps.
$ContainerName = "test-ps"
#create a new container with public access to the blobs
New-AzureStorageContainer -Name $ContainerName -Context $ctx -Permission Blob
This creates a container in your storage account (as defined by the context) with a permission of
Blob, which means the blobs can be accessed on the Internet with a URL.
4.
Now you need to set a variable pointing at the local directory with the images. You can upload
any files, just remember the larger they are, the longer it will take to upload! Using a variable here
makes it easier to change it later in case you use this in multiple places.
$localFileDirectory = "D:\_TestImages\"
5.
Now you can upload a blob. First, you’ll set a variable name for the blob name to be the same as
the file name. Then, append it to the $localFileDirectory variable. The file will be uploaded from
the local disk to the specified container.
$BlobName = "SnowyCabin.jpg"
$localFile = $localFileDirectory + $BlobName
Set-AzureStorageBlobContent -File $localFile -Container $ContainerName `
-Blob $BlobName -Context $ctx
To run the script, press F5. To run parts of the script, highlight the bits you want to run and press
F8 (or click the Run Selection icon). If you have to run this repeatedly, you only want to create the
container once, so once that’s successful, only select commands starting after that. When you run
this and upload the file, you get back verification in the command window (Figure 4-23).
129
CHAPTER 4 | Azure Storage
Figure 4-23 Upload file to blob storage.
Do'stlaringiz bilan baham: |