Azure Government uses the same underlying technologies as commercial Azure, enabling you to use the development tools you're already familiar with. If you don't have an Azure Government subscription, create a free account before you begin.
Prerequisites
Review Guidance for developers. This article discusses Azure Government's unique URLs and endpoints for managing your environment. You must know about these endpoints to connect to Azure Government.
The Microsoft Azure Storage Explorer is a cross-platform tool for working with Azure Storage. Government customers can now take advantage of all the latest features of the Azure Storage Explorer such as creating and managing blobs, queues, tables, and file shares.
Getting Started with Storage Explorer
Open the Azure Storage Explorer desktop application.
You'll be prompted to add an Azure account; in the dropdown choose the "Azure US Government" option:
Sign in to your Azure Government account and you can see all of your resources. The Storage Explorer should look similar to the screenshot below. Click on your Storage Account to see the blob containers, file shares, Queues, and Tables.
Have an active Azure Government subscription. If you don't have an Azure Government subscription, create a free account before you begin.
Download Visual Studio 2019.
Getting Started with Storage API
One important difference to remember when connecting with the Storage API is that the URL for storage in Azure Government is different than the URL for storage in commercial Azure. Specifically, the domain ends with core.usgovcloudapi.net, rather than core.windows.net. These endpoint differences must be taken into account when you connect to storage in Azure Government with a client library.
Application requests to Azure Storage must be authorized. Using the DefaultAzureCredential class provided by the Azure Identity client library is the recommended approach for implementing passwordless connections to Azure services in your code.
You can also authorize requests to Azure Storage by using the account access key. However, this approach should be used with caution. Developers must be diligent to never expose the access key in an unsecure location. Anyone who has the access key is able to authorize requests against the storage account, and effectively has access to all the data. DefaultAzureCredential offers improved management and security benefits over the account key to allow passwordless authentication. Both options are demonstrated in the following examples.
C#/.NET
Open Visual Studio and create a new project. Add a reference to the Azure Tables client library for .NET. This package contains classes for connecting to your Storage Table account.
An easy and secure way to authorize access and connect to Azure Storage is to obtain an OAuth token by creating a DefaultAzureCredential instance. You can then use that credential to create a TableServiceClient object, as shown in the following code example:
C#
var credentialOptions = new DefaultAzureCredentialOptions()
{
AuthorityHost = AzureAuthorityHosts.AzureGovernment,
};
var credential = new DefaultAzureCredential(credentialOptions);
var storageTableUri = Environment.GetEnvironmentVariable("STORAGE_TABLE_URI");
var tableServiceClient = new TableServiceClient(
new Uri(storageTableUri)
credential);
Add these lines of C# code to connect using a connection string:
C#
var connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
var tableServiceClient = new TableServiceClient(connectionString);
You can also connect using an account key, as shown in the following code example:
C#
var credentials = new TableSharedKeyCredential(
storageAccountName,
Environment.GetEnvironmentVariable("STORAGE_ACCOUNT_KEY"));
var storageTableUri = Environment.GetEnvironmentVariable("STORAGE_TABLE_URI");
var tableServiceClient = new TableServiceClient(new Uri(storageTableUri), credentials);
Important
The account access key should be used with caution. If your account access key is lost or accidentally placed in an insecure location, your service may become vulnerable. Anyone who has the access key is able to authorize requests against the storage account, and effectively has access to all the data. DefaultAzureCredential provides enhanced security features and benefits and is the recommended approach for managing authorization to Azure services.
At this point, we can interact with Storage as we normally would. The following example shows how to retrieve a specific entity from Table Storage:
An easy and secure way to authorize access and connect to Azure Storage is to obtain an OAuth token by creating a DefaultAzureCredential instance. You can then use that credential to create a TableServiceClient object, as shown in the following code example:
Java
import com.azure.data.tables.implementation.ModelHelper;
import com.azure.data.tables.models.*;
import java.util.HashMap;
publicclasstest{
publicstaticfinal String storageConnectionString = System.getEnv("AZURE_STORAGE_CONNECTION_STRING");
publicstaticvoidmain(String[] args){
try
{
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
.authorityHost("https://management.usgovcloudapi.net/.default")
.build();
// Create the table service client.
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.endpoint("https://<storage-account-name>.table.core.usgovcloudapi.net/")
.credential(credential)
.buildClient();
// Create the table if it doesn't exist.
String tableName = "Contacts";
TableClient tableClient = tableServiceClient.createTableIfNotExists(tableName);
// Create a new customer entity.
TableEntity customer1 = ModelHelper.createEntity(new HashMap<String, Object>() {{
put("PartitionKey", "Brown");
put("RowKey", "Walter");
put("Email", "Walter@contoso.com");
}});
// Insert table entry into table
tableClient.createEntity(customer1);
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
}
}
}
Create a "test" class where we'll access Azure Table Storage using the Azure Tables client library.
Copy and paste the code below, and paste your Storage Account connection string into the AZURE_STORAGE_CONNECTION_STRING environment variable.
Java
import com.azure.data.tables.implementation.ModelHelper;
import com.azure.data.tables.models.*;
import java.util.HashMap;
publicclasstest{
publicstaticfinal String storageConnectionString = System.getEnv("AZURE_STORAGE_CONNECTION_STRING");
publicstaticvoidmain(String[] args){
try
{
// Create the table service client.
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.connectionString(storageConnectionString)
.buildClient();
// Create the table if it doesn't exist.
String tableName = "Contacts";
TableClient tableClient = tableServiceClient.createTableIfNotExists(tableName);
// Create a new customer entity.
TableEntity customer1 = ModelHelper.createEntity(new HashMap<String, Object>() {{
put("PartitionKey", "Brown");
put("RowKey", "Walter");
put("Email", "Walter@contoso.com");
}});
// Insert table entry into table
tableClient.createEntity(customer1);
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
}
}
}
Important
The account access key should be used with caution. If your account access key is lost or accidentally placed in an insecure location, your service may become vulnerable. Anyone who has the access key is able to authorize requests against the storage account, and effectively has access to all the data. DefaultAzureCredential provides enhanced security features and benefits and is the recommended approach for managing authorization to Azure services.
An easy and secure way to authorize access and connect to Azure Storage is to obtain an OAuth token by creating a DefaultAzureCredential instance. You can then use that credential to create a BlobServiceClient object, as shown in the following code example:
JavaScript
const { BlobServiceClient } = require('@azure/storage-blob');
const {
DefaultAzureCredential,
DefaultAzureCredentialOptions,
AzureAuthorityHosts
} = require('@azure/identity');
const credentialOptions = new DefaultAzureCredentialOptions(
{
authorityHost: AzureAuthorityHosts.AzureGovernment
}
);
const blobServiceClient = new BlobServiceClient(
`https://<storage-account-name>.blob.core.usgovcloudapi.net`,
new DefaultAzureCredential(credentialOptions)
);
var containerClient = blobServiceClient.getContainerClient('testing');
containerClient.createIfNotExists();
The following code below connects to Azure Blob Storage and creates a Container using the Azure Storage API.
Paste your Azure Storage account connection string into the AZURE_STORAGE_CONNECTION_STRING environment variable.
JavaScript
var { BlobServiceClient } = require("@azure/storage-blob");
var storageConnectionString = process.env["AZURE_STORAGE_CONNECTION_STRING"];
var blobServiceClient = BlobServiceClient.fromConnectionString(storageConnectionString);
var containerClient = blobServiceClient.getContainerClient('testing');
containerClient.createIfNotExists();
Important
The account access key should be used with caution. If your account access key is lost or accidentally placed in an insecure location, your service may become vulnerable. Anyone who has the access key is able to authorize requests against the storage account, and effectively has access to all the data. DefaultAzureCredential provides enhanced security features and benefits and is the recommended approach for managing authorization to Azure services.
An easy and secure way to authorize access and connect to Azure Storage is to obtain an OAuth token by creating a DefaultAzureCredential instance. You can then use that credential to create a BlobServiceClient object, as shown in the following code example:
Python
from azure.identity import DefaultAzureCredential, AzureAuthorityHosts
from azure.storage.blob import BlobServiceClient
credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
blob_service_client = BlobServiceClient("https://<storage-account-name>.blob.core.usgovcloudapi.net", credential=credential)
container_name ="<container-name>"
container = blob_service_client.get_container_client(container=container_name)
generator = container.list_blobs()
for blob in generator:
print("\t Blob name: " + blob.name)
When using the Storage library for Python to connect to Azure Government, paste your Azure storage connection string in the AZURE_STORAGE_CONNECTION_STRING environment variable.
Python
# Create the BlobServiceClient that is used to call the Blob service for the storage account
connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
blob_service_client = BlobServiceClient.from_connection_string(conn_str=connection_string)
container_name ="<container-name>"
container = blob_service_client.get_container_client(container=container_name)
generator = container.list_blobs()
for blob in generator:
print("\t Blob name: " + blob.name)
Important
The account access key should be used with caution. If your account access key is lost or accidentally placed in an insecure location, your service may become vulnerable. Anyone who has the access key is able to authorize requests against the storage account, and effectively has access to all the data. DefaultAzureCredential provides enhanced security features and benefits and is the recommended approach for managing authorization to Azure services.
In this module, you'll learn about the integrations built into .NET Aspire that make it simple to store files, data, and messages in Azure Storage accounts.
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.