Develop with Storage API on Azure Government
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.
- Review Compare Azure Government and global Azure and click on a service of interest to see variations between Azure Government and global Azure.
- Download and install the latest version of Azure Storage Explorer.
Connecting Storage Explorer 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.
For more information about Azure Storage Explorer, see Get started with Storage Explorer.
Connecting to the Storage API
Prerequisites
- 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 C#.
Go to the Azure Government portal and select your storage account and then click the "Access Keys" tab:
Copy/paste the storage account connection string.
C#
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.
Add these two lines of C# code to connect:
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);
At this point, we can interact with Storage as we normally would. For example, if we want to retrieve a specific entity from our Table Storage, we could do it like this:
var tableClient = tableServiceClient.GetTableClient("Contacts"); ContactEntity contact = tableClient.GetEntity<ContactEntity>("gov-partition1", "0fb52a6c-3784-4dc5-aa6d-ecda4426dbda"); Console.WriteLine($"Contact: {contact.FirstName} {contact.LastName}");
Java
Download the Azure Tables client library for Java and configure your project correctly.
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.import com.azure.data.tables.implementation.ModelHelper; import com.azure.data.tables.models.*; import java.util.HashMap; public class test { public static final String storageConnectionString = System.getEnv("AZURE_STORAGE_CONNECTION_STRING"); public static void main(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(); } } }
Node.js
Download the Azure Storage Blob client library for Node.js and configure your application correctly.
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.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();
Python
Download the Azure Storage Blob client library for Python.
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.# 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 ='ml-gov-demo' container = blob_service_client.get_container_client(container=container_name) generator = container.list_blobs() for blob in generator: print("\t Blob name: " + blob.name)
PHP
Download the Azure Storage SDK for PHP.
The code below accesses Azure Table Storage using the Azure Storage API. In the
connectionString
variable, you'll notice that there's aTableEndpoint
parameter. Depending on which service you're using, you must define the parameter and set it to the endpoint for that service:BlobEndpoint= //ends with 'blob.core.usgovcloudapi.net'
QueueEndpoint= //ends with 'queue.core.usgovcloudapi.net'
TableEndpoint= //ends with 'table.core.usgovcloudapi.net'
Note
You can find these endpoints by navigating to your Storage Account from the portal. Paste in your storage account name, key, and service endpoint in the
connectionString
variable.<?php require_once "vendor/autoload.php"; use WindowsAzure\Common\ServicesBuilder; use MicrosoftAzure\Storage\Common\ServiceException; $connectionString = 'DefaultEndpointsProtocol=http;AccountName=<accountname>;AccountKey=<accountkey>;TableEndpoint=http://<storageaccountname>.table.core.usgovcloudapi.net/'; $tableRestProxy = ServicesBuilder::getInstance()->createTableService($connectionString); try { // Create table. $tableRestProxy->createTable("test"); } catch(ServiceException $e){ $code = $e->getCode(); $error_message = $e->getMessage(); } ?>
Next steps
- Read more about Azure Storage.
- Subscribe to the Azure Government blog
- Get help on Stack Overflow by using the azure-gov tag
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for