Skip to content

Commit

Permalink
migration clean-up, code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tamram committed Nov 17, 2016
1 parent 47d79af commit 8e5cc57
Show file tree
Hide file tree
Showing 8 changed files with 717 additions and 588 deletions.
127 changes: 81 additions & 46 deletions articles/storage/storage-azure-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ms.workload: storage
ms.tgt_pltfrm: na
ms.devlang: na
ms.topic: article
ms.date: 10/18/2016
ms.date: 11/16/2016
ms.author: micurd

---
Expand Down Expand Up @@ -46,30 +46,33 @@ See [Assigning administrator roles in Azure Active Directory (Azure AD)](https:/
3. In the command line interface, type `azure storage` to list out all the azure storage commands and get a first impression of the functionalities the Azure CLI provides. You can type command name with **-h** parameter (for example, `azure storage share create -h`) to see details of command syntax.
4. Now, we’ll give you a simple script that shows basic Azure CLI commands to access Azure Storage. The script will first ask you to set two variables for your storage account and key. Then, the script will create a new container in this new storage account and upload an existing image file (blob) to that container. After the script lists all blobs in that container, it will download the image file to the destination directory which exists on the local computer.

#!/bin/bash
# A simple Azure storage example

export AZURE_STORAGE_ACCOUNT=<storage_account_name>
export AZURE_STORAGE_ACCESS_KEY=<storage_account_key>

export container_name=<container_name>
export blob_name=<blob_name>
export image_to_upload=<image_to_upload>
export destination_folder=<destination_folder>

echo "Creating the container..."
azure storage container create $container_name

echo "Uploading the image..."
azure storage blob upload $image_to_upload $container_name $blob_name

echo "Listing the blobs..."
azure storage blob list $container_name

echo "Downloading the image..."
azure storage blob download $container_name $blob_name $destination_folder

echo "Done"
```azurecli
#!/bin/bash
# A simple Azure storage example
export AZURE_STORAGE_ACCOUNT=<storage_account_name>
export AZURE_STORAGE_ACCESS_KEY=<storage_account_key>
export container_name=<container_name>
export blob_name=<blob_name>
export image_to_upload=<image_to_upload>
export destination_folder=<destination_folder>
echo "Creating the container..."
azure storage container create $container_name
echo "Uploading the image..."
azure storage blob upload $image_to_upload $container_name $blob_name
echo "Listing the blobs..."
azure storage blob list $container_name
echo "Downloading the image..."
azure storage blob download $container_name $blob_name $destination_folder
echo "Done"
```
5. In your local computer, open your preferred text editor (vim for example). Type the above script into your text editor.
6. Now, you need to update the script variables based on your configuration settings.
Expand All @@ -90,32 +93,42 @@ While most of the storage commands will work without an Azure subscription, we r
### Create a new storage account
To use Azure storage, you will need a storage account. You can create a new Azure storage account after you have configured your computer to connect to your subscription.
azure storage account create <account_name>
```azurecli
azure storage account create <account_name>
```

The name of your storage account must be between 3 and 24 characters in length and use numbers and lower-case letters only.

### Set a default Azure storage account in environment variables
You can have multiple storage accounts in your subscription. You can choose one of them and set it in the environment variables for all the storage commands in the same session. This enables you to run the Azure CLI storage commands without specifying the storage account and key explicitly.

export AZURE_STORAGE_ACCOUNT=<account_name>
export AZURE_STORAGE_ACCESS_KEY=<key>

```azurecli
export AZURE_STORAGE_ACCOUNT=<account_name>
export AZURE_STORAGE_ACCESS_KEY=<key>
```

Another way to set a default storage account is using connection string. Firstly get the connection string by command:

azure storage account connectionstring show <account_name>
```azurecli
azure storage account connectionstring show <account_name>
```

Then copy the output connection string and set it to environment variable:

export AZURE_STORAGE_CONNECTION_STRING=<connection_string>

```azurecli
export AZURE_STORAGE_CONNECTION_STRING=<connection_string>
```

## Create and manage blobs
Azure Blob storage is a service for storing large amounts of unstructured data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. This section assumes that you are already familiar with the Azure Blob storage concepts. For detailed information, see [Get started with Azure Blob storage using .NET](storage-dotnet-how-to-use-blobs.md) and [Blob Service Concepts](http://msdn.microsoft.com/library/azure/dd179376.aspx).

### Create a container
Every blob in Azure storage must be in a container. You can create a private container using the `azure storage container create` command:

azure storage container create mycontainer

```azurecli
azure storage container create mycontainer
```

> [!NOTE]
> There are three levels of anonymous read access: **Off**, **Blob**, and **Container**. To prevent anonymous access to blobs, set the Permission parameter to **Off**. By default, the new container is private and can be accessed only by the account owner. To allow anonymous public read access to blob resources, but not to container metadata or to the list of blobs in the container, set the Permission parameter to **Blob**. To allow full public read access to blob resources, container metadata, and the list of blobs in the container, set the Permission parameter to **Container**. For more information, see [Manage anonymous read access to containers and blobs](storage-manage-access-to-resources.md).
>
Expand All @@ -126,23 +139,29 @@ Azure Blob Storage supports block blobs and page blobs. For more information, se

To upload blobs in to a container, you can use the `azure storage blob upload`. By default, this command uploads the local files to a block blob. To specify the type for the blob, you can use the `--blobtype` parameter.

azure storage blob upload '~/images/HelloWorld.png' mycontainer myBlockBlob
```azurecli
azure storage blob upload '~/images/HelloWorld.png' mycontainer myBlockBlob
```

### Download blobs from a container
The following example demonstrates how to download blobs from a container.

azure storage blob download mycontainer myBlockBlob '~/downloadImages/downloaded.png'
```azurecli
azure storage blob download mycontainer myBlockBlob '~/downloadImages/downloaded.png'
```

### Copy blobs
You can copy blobs within or across storage accounts and regions asynchronously.

The following example demonstrates how to copy blobs from one storage account to another. In this sample we create a container where blobs are publicly, anonymously accessible.

azure storage container create mycontainer2 -a <accountName2> -k <accountKey2> -p Blob
```azurecli
azure storage container create mycontainer2 -a <accountName2> -k <accountKey2> -p Blob
azure storage blob upload '~/Images/HelloWorld.png' mycontainer2 myBlockBlob2 -a <accountName2> -k <accountKey2>
azure storage blob upload '~/Images/HelloWorld.png' mycontainer2 myBlockBlob2 -a <accountName2> -k <accountKey2>
azure storage blob copy start 'https://<accountname2>.blob.core.windows.net/mycontainer2/myBlockBlob2' mycontainer
azure storage blob copy start 'https://<accountname2>.blob.core.windows.net/mycontainer2/myBlockBlob2' mycontainer
```

This sample performs an asynchronous copy. You can monitor the status of each copy operation by running the `azure storage blob copy show` operation.

Expand All @@ -151,45 +170,61 @@ Note that the source URL provided for the copy operation must either be publicly
### Delete a blob
To delete a blob, use the below command:

azure storage blob delete mycontainer myBlockBlob2
```azurecli
azure storage blob delete mycontainer myBlockBlob2
```

## Create and manage file shares
Azure File storage offers shared storage for applications using the standard SMB protocol. Microsoft Azure virtual machines and cloud services, as well as on-premises applications, can share file data via mounted shares. You can manage file shares and file data via the Azure CLI. For more information on Azure File storage, see [Get started with Azure File storage on Windows](storage-dotnet-how-to-use-files.md) or [How to use Azure File storage with Linux](storage-how-to-use-files-linux.md).

### Create a file share
An Azure File share is an SMB file share in Azure. All directories and files must be created in a file share. An account can contain an unlimited number of shares, and a share can store an unlimited number of files, up to the capacity limits of the storage account. The following example creates a file share named **myshare**.

azure storage share create myshare
```azurecli
azure storage share create myshare
```

### Create a directory
A directory provides an optional hierarchical structure for an Azure file share. The following example creates a directory named **myDir** in the file share.

azure storage directory create myshare myDir
```azurecli
azure storage directory create myshare myDir
```

Note that directory path can include multiple levels, *e.g.*, **a/b**. However, you must ensure that all parent directories exists. For example, for path **a/b**, you must create directory **a** first, then create directory **b**.

### Upload a local file to directory
The following example uploads a file from **~/temp/samplefile.txt** to the **myDir** directory. Edit the file path so that it points to a valid file on your local machine:

azure storage file upload '~/temp/samplefile.txt' myshare myDir
```azurecli
azure storage file upload '~/temp/samplefile.txt' myshare myDir
```

Note that a file in the share can be up to 1 TB in size.

### List the files in the share root or directory
You can list the files and subdirectories in a share root or a directory using the following command:

azure storage file list myshare myDir
```azurecli
azure storage file list myshare myDir
```

Note that the directory name is optional for the listing operation. If omitted, the command lists the contents of the root directory of the share.

### Copy files
Beginning with version 0.9.8 of Azure CLI, you can copy a file to another file, a file to a blob, or a blob to a file. Below we demonstrate how to perform these copy operations using CLI commands. To copy a file to the new directory:

azure storage file copy start --source-share srcshare --source-path srcdir/hello.txt --dest-share destshare --dest-path destdir/hellocopy.txt --connection-string $srcConnectionString --dest-connection-string $destConnectionString
```azurecli
azure storage file copy start --source-share srcshare --source-path srcdir/hello.txt --dest-share destshare
--dest-path destdir/hellocopy.txt --connection-string $srcConnectionString --dest-connection-string $destConnectionString
```

To copy a blob to a file directory:

azure storage file copy start --source-container srcctn --source-blob hello2.txt --dest-share hello --dest-path hellodir/hello2copy.txt --connection-string $srcConnectionString --dest-connection-string $destConnectionString
```azurecli
azure storage file copy start --source-container srcctn --source-blob hello2.txt --dest-share hello
--dest-path hellodir/hello2copy.txt --connection-string $srcConnectionString --dest-connection-string $destConnectionString
```

## Next Steps
Here are some related articles and resources for learning more about Azure Storage.
Expand Down
89 changes: 46 additions & 43 deletions articles/storage/storage-blob-snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ms.workload: storage
ms.tgt_pltfrm: na
ms.devlang: na
ms.topic: article
ms.date: 10/18/2016
ms.date: 11/16/2016
ms.author: tamram

---
Expand All @@ -37,37 +37,38 @@ Any leases associated with the base blob do not affect the snapshot. You cannot
## Create a snapshot
The following code example shows how to create a snapshot in .NET. This example specifies separate metadata for the snapshot when it is created.

private static async Task CreateBlockBlobSnapshot(CloudBlobContainer container)
```csharp
private static async Task CreateBlockBlobSnapshot(CloudBlobContainer container)
{
// Create a new block blob in the container.
CloudBlockBlob baseBlob = container.GetBlockBlobReference("sample-base-blob.txt");

// Add blob metadata.
baseBlob.Metadata.Add("ApproxBlobCreatedDate", DateTime.UtcNow.ToString());

try
{
// Create a new block blob in the container.
CloudBlockBlob baseBlob = container.GetBlockBlobReference("sample-base-blob.txt");

// Add blob metadata.
baseBlob.Metadata.Add("ApproxBlobCreatedDate", DateTime.UtcNow.ToString());

try
{
// Upload the blob to create it, with its metadata.
await baseBlob.UploadTextAsync(string.Format("Base blob: {0}", baseBlob.Uri.ToString()));

// Sleep 5 seconds.
System.Threading.Thread.Sleep(5000);

// Create a snapshot of the base blob.
// Specify metadata at the time that the snapshot is created to specify unique metadata for the snapshot.
// If no metadata is specified when the snapshot is created, the base blob's metadata is copied to the snapshot.
Dictionary<string, string> metadata = new Dictionary<string, string>();
metadata.Add("ApproxSnapshotCreatedDate", DateTime.UtcNow.ToString());
await baseBlob.CreateSnapshotAsync(metadata, null, null, null);
}
catch (StorageException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
// Upload the blob to create it, with its metadata.
await baseBlob.UploadTextAsync(string.Format("Base blob: {0}", baseBlob.Uri.ToString()));

// Sleep 5 seconds.
System.Threading.Thread.Sleep(5000);

// Create a snapshot of the base blob.
// Specify metadata at the time that the snapshot is created to specify unique metadata for the snapshot.
// If no metadata is specified when the snapshot is created, the base blob's metadata is copied to the snapshot.
Dictionary<string, string> metadata = new Dictionary<string, string>();
metadata.Add("ApproxSnapshotCreatedDate", DateTime.UtcNow.ToString());
await baseBlob.CreateSnapshotAsync(metadata, null, null, null);
}

catch (StorageException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
```

## Copy snapshots
Copy operations involving blobs and snapshots follow these rules:
Expand Down Expand Up @@ -98,23 +99,25 @@ Using snapshots with Premium Storage follow these rules:
## Return the absolute URI to a snapshot
This C# code example creates a snapshot and writes out the absolute URI for the primary location.

//Create the blob service client object.
const string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key";
```csharp
//Create the blob service client object.
const string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key";

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

//Get a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("sample-container");
container.CreateIfNotExists();
//Get a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("sample-container");
container.CreateIfNotExists();

//Get a reference to a blob.
CloudBlockBlob blob = container.GetBlockBlobReference("sampleblob.txt");
blob.UploadText("This is a blob.");
//Get a reference to a blob.
CloudBlockBlob blob = container.GetBlockBlobReference("sampleblob.txt");
blob.UploadText("This is a blob.");

//Create a snapshot of the blob and write out its primary URI.
CloudBlockBlob blobSnapshot = blob.CreateSnapshot();
Console.WriteLine(blobSnapshot.SnapshotQualifiedStorageUri.PrimaryUri);
//Create a snapshot of the blob and write out its primary URI.
CloudBlockBlob blobSnapshot = blob.CreateSnapshot();
Console.WriteLine(blobSnapshot.SnapshotQualifiedStorageUri.PrimaryUri);
```

## Understand how snapshots accrue charges
Creating a snapshot, which is a read-only copy of a blob, can result in additional data storage charges to your account. When designing your application, it is important to be aware how these charges might accrue so that you can minimize unnecessary costs.
Expand Down
Loading

0 comments on commit 8e5cc57

Please sign in to comment.