|
| 1 | +# Azure Queue Storage SDK for Go |
| 2 | + |
| 3 | +> Server Version: 2018-03-28 |
| 4 | +
|
| 5 | +Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in |
| 6 | +the world via authenticated calls using HTTP or HTTPS. |
| 7 | +A single queue message can be up to 64 KiB in size, and a queue can contain millions of messages, |
| 8 | +up to the total capacity limit of a storage account. |
| 9 | + |
| 10 | +[Source code][source] | [API reference documentation][docs] | [REST API documentation][rest_docs] |
| 11 | + |
| 12 | +## Getting started |
| 13 | + |
| 14 | +### Install the package |
| 15 | + |
| 16 | +Install the Azure Queue Storage SDK for Go with [go get][goget]: |
| 17 | + |
| 18 | +```Powershell |
| 19 | +go get github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue |
| 20 | +``` |
| 21 | + |
| 22 | +If you're going to authenticate with Azure Active Directory (recommended), install the [azidentity][azidentity] module. |
| 23 | +```Powershell |
| 24 | +go get github.com/Azure/azure-sdk-for-go/sdk/azidentity |
| 25 | +``` |
| 26 | + |
| 27 | +### Prerequisites |
| 28 | + |
| 29 | +A supported [Go][godevdl] version (the Azure SDK supports the two most recent Go releases). |
| 30 | + |
| 31 | +You need an [Azure subscription][azure_sub] and a |
| 32 | +[Storage Account][storage_account_docs] to use this package. |
| 33 | + |
| 34 | +To create a new Storage Account, you can use the [Azure Portal][storage_account_create_portal], |
| 35 | +[Azure PowerShell][storage_account_create_ps], or the [Azure CLI][storage_account_create_cli]. |
| 36 | +Here's an example using the Azure CLI: |
| 37 | + |
| 38 | +```Powershell |
| 39 | +az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location westus --sku Standard_LRS |
| 40 | +``` |
| 41 | + |
| 42 | +### Authenticate the client |
| 43 | + |
| 44 | +In order to interact with the Azure Queue Storage service, you'll need to create an instance of the `azqueue.ServiceClient` type. The [azidentity][azidentity] module makes it easy to add Azure Active Directory support for authenticating Azure SDK clients with their corresponding Azure services. |
| 45 | + |
| 46 | +```go |
| 47 | +// create a credential for authenticating with Azure Active Directory |
| 48 | +cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 49 | +// TODO: handle err |
| 50 | + |
| 51 | +// create an azqueue.ServiceClient for the specified storage account that uses the above credential |
| 52 | +client, err := azqueue.NewServiceClient("https://MYSTORAGEACCOUNT.queue.core.windows.net/", cred, nil) |
| 53 | +// TODO: handle err |
| 54 | +``` |
| 55 | + |
| 56 | +Learn more about enabling Azure Active Directory for authentication with Azure Storage in [our documentation][storage_ad] and [our samples](#next-steps). |
| 57 | + |
| 58 | +## Key concepts |
| 59 | +The following components make up the Azure Queue Service: |
| 60 | +* The storage account itself |
| 61 | +* A queue within the storage account, which contains a set of messages |
| 62 | +* A message within a queue, in any format, of up to 64 KiB |
| 63 | + |
| 64 | +The Azure Storage Queues client library for GO allows you to interact with each of these components through the |
| 65 | +use of a dedicated client object. |
| 66 | + |
| 67 | +### Clients |
| 68 | +Two different clients are provided to interact with the various components of the Queue Service: |
| 69 | +1. ServiceClient - |
| 70 | + this client represents interaction with the Azure storage account itself, and allows you to acquire preconfigured |
| 71 | + client instances to access the queues within. It provides operations to retrieve and configure the account |
| 72 | + properties as well as list, create, and delete queues within the account. To perform operations on a specific queue, |
| 73 | + retrieve a client using the `NewQueueClient` method. |
| 74 | +2. QueueClient - |
| 75 | + this client represents interaction with a specific queue (which need not exist yet). It provides operations to |
| 76 | + create, delete, or configure a queue and includes operations to enqueue, dequeue, peek, delete, and update messages |
| 77 | + within it. |
| 78 | + |
| 79 | +### Messages |
| 80 | +* **Enqueue** - Adds a message to the queue and optionally sets a visibility timeout for the message. |
| 81 | +* **Dequeue** - Retrieves a message from the queue and makes it invisible to other consumers. |
| 82 | +* **Peek** - Retrieves a message from the front of the queue, without changing the message visibility. |
| 83 | +* **Update** - Updates the visibility timeout of a message and/or the message contents. |
| 84 | +* **Delete** - Deletes a specified message from the queue. |
| 85 | +* **Clear** - Clears all messages from the queue. |
| 86 | + |
| 87 | +### Goroutine safety |
| 88 | +We guarantee that all client instance methods are goroutine-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/golang_introduction.html#thread-safety)). This ensures that the recommendation of reusing client instances is always safe, even across goroutines. |
| 89 | + |
| 90 | +### About Queue metadata |
| 91 | +Queue metadata name/value pairs are valid HTTP headers and should adhere to all restrictions governing HTTP headers. Metadata names must be valid HTTP header names, may contain only ASCII characters, and should be treated as case-insensitive. Base64-encode or URL-encode metadata values containing non-ASCII characters. |
| 92 | + |
| 93 | +### Additional concepts |
| 94 | +<!-- CLIENT COMMON BAR --> |
| 95 | +[Client options](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/policy#ClientOptions) | |
| 96 | +[Accessing the response](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime#WithCaptureResponse) | |
| 97 | +[Handling failures](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore#ResponseError) | |
| 98 | +[Logging](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/log) |
| 99 | +<!-- CLIENT COMMON BAR --> |
| 100 | + |
| 101 | +## Examples |
| 102 | + |
| 103 | +### Queue Manipulation |
| 104 | + |
| 105 | +```go |
| 106 | +const ( |
| 107 | + accountName = "MYSTORAGEACCOUNT" |
| 108 | + accountKey = "ACCOUNT_KEY" |
| 109 | + queueName = "samplequeue" |
| 110 | +) |
| 111 | +``` |
| 112 | + |
| 113 | +### Exploring Queue Service APIs |
| 114 | + |
| 115 | +```go |
| 116 | +// shared key credential set up |
| 117 | +cred := azqueue.NewSharedKeyCredential(accountName, accountKey) |
| 118 | + |
| 119 | +// instantiate service client |
| 120 | +serviceClient, err := azqueue.NewServiceClientWithSharedKeyCredential(account, cred, nil) |
| 121 | +// TODO: handle error |
| 122 | + |
| 123 | +// 1. create queue |
| 124 | +queueClient := serviceClient.NewQueueClient(queueName) |
| 125 | +_, err = queueClient.Create(context.TODO(), nil) |
| 126 | +// TODO: handle error |
| 127 | + |
| 128 | +// 2. enqueue message |
| 129 | +_, err = queueClient.EnqueueMessage(context.TODO(), message, nil) |
| 130 | +// TODO: handle error |
| 131 | + |
| 132 | +// 3. dequeue message |
| 133 | +_, err = queueClient.DequeueMessage(context.TODO(), nil) |
| 134 | +// TODO: handle error |
| 135 | + |
| 136 | +// 4. delete queue |
| 137 | +_, err =queueClient.Delete(context.TODO(), nil) |
| 138 | +// TODO: handle error |
| 139 | +``` |
| 140 | + |
| 141 | +### Enumerating queues |
| 142 | + |
| 143 | +```go |
| 144 | +const ( |
| 145 | + account = "https://MYSTORAGEACCOUNT.queue.core.windows.net/" |
| 146 | +) |
| 147 | + |
| 148 | +// authenticate with Azure Active Directory |
| 149 | +cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 150 | +// TODO: handle error |
| 151 | + |
| 152 | +// create a client for the specified storage account |
| 153 | +client, err := azqueue.NewServiceClient(account, cred, nil) |
| 154 | +// TODO: handle error |
| 155 | + |
| 156 | +// queue listings are returned across multiple pages |
| 157 | +pager := client.NewListQueuesPager(nil) |
| 158 | + |
| 159 | +// continue fetching pages until no more remain |
| 160 | +for pager.More() { |
| 161 | + resp, err := pager.NextPage(context.Background()) |
| 162 | + _require.Nil(err) |
| 163 | + // print queue name |
| 164 | + for _, queue := range resp.Queues { |
| 165 | + fmt.Println(*queue.Name) |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +## Troubleshooting |
| 171 | + |
| 172 | +All queue service operations will return an |
| 173 | +[*azcore.ResponseError][azcore_response_error] on failure with a |
| 174 | +populated `ErrorCode` field. Many of these errors are recoverable. |
| 175 | +The [queueerror][queue_error] package provides the possible Storage error codes |
| 176 | +along with various helper facilities for error handling. |
| 177 | + |
| 178 | +```go |
| 179 | +const ( |
| 180 | + connectionString = "<connection_string>" |
| 181 | + queueName = "samplequeue" |
| 182 | +) |
| 183 | + |
| 184 | +// create a client with the provided connection string |
| 185 | +client, err := azqueue.NewServiceClientFromConnectionString(connectionString, nil) |
| 186 | +// TODO: handle error |
| 187 | + |
| 188 | +// try to delete the queue, avoiding any potential race conditions with an in-progress or completed deletion |
| 189 | +_, err = client.DeleteQueue(context.TODO(), queueName, nil) |
| 190 | + |
| 191 | +if queueerror.HasCode(err, queueerror.QueueBeingDeleted, queueerror.QueueNotFound) { |
| 192 | + // ignore any errors if the queue is being deleted or already has been deleted |
| 193 | +} else if err != nil { |
| 194 | + // TODO: some other error |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +## Next steps |
| 199 | + |
| 200 | +Get started with our [Queue samples][samples]. They contain complete examples of the above snippets and more. |
| 201 | + |
| 202 | +## Contributing |
| 203 | + |
| 204 | +See the [Storage CONTRIBUTING.md][storage_contrib] for details on building, |
| 205 | +testing, and contributing to this library. |
| 206 | + |
| 207 | +This project welcomes contributions and suggestions. Most contributions require |
| 208 | +you to agree to a Contributor License Agreement (CLA) declaring that you have |
| 209 | +the right to, and actually do, grant us the rights to use your contribution. For |
| 210 | +details, visit [cla.microsoft.com][cla]. |
| 211 | + |
| 212 | +This project has adopted the [Microsoft Open Source Code of Conduct][coc]. |
| 213 | +For more information see the [Code of Conduct FAQ][coc_faq] |
| 214 | +or contact [opencode@microsoft.com][coc_contact] with any |
| 215 | +additional questions or comments. |
| 216 | + |
| 217 | +<!-- LINKS --> |
| 218 | +[source]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azqueue |
| 219 | +[docs]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue |
| 220 | +[rest_docs]: https://docs.microsoft.com/rest/api/storageservices/queue-service-rest-api |
| 221 | +[godevdl]: https://go.dev/dl/ |
| 222 | +[goget]: https://pkg.go.dev/cmd/go#hdr-Add_dependencies_to_current_module_and_install_them |
| 223 | +[storage_account_docs]: https://docs.microsoft.com/azure/storage/common/storage-account-overview |
| 224 | +[storage_account_create_ps]: https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-powershell |
| 225 | +[storage_account_create_cli]: https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-cli |
| 226 | +[storage_account_create_portal]: https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-portal |
| 227 | +[azure_cli]: https://docs.microsoft.com/cli/azure |
| 228 | +[azure_sub]: https://azure.microsoft.com/free/ |
| 229 | +[azidentity]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity |
| 230 | +[storage_ad]: https://docs.microsoft.com/azure/storage/common/storage-auth-aad |
| 231 | +[azcore_response_error]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore#ResponseError |
| 232 | +[samples]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azqueue/samples_test.go |
| 233 | +[queue_error]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azqueue/queueerror/error_codes.go |
| 234 | +[queue]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azqueue/queue_client.go |
| 235 | +[sas]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azqueue/sas |
| 236 | +[service]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/storage/azqueue/service_client.go |
| 237 | +[storage_contrib]: https://github.com/Azure/azure-sdk-for-go/blob/main/CONTRIBUTING.md |
| 238 | +[cla]: https://cla.microsoft.com |
| 239 | +[coc]: https://opensource.microsoft.com/codeofconduct/ |
| 240 | +[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ |
| 241 | +[coc_contact]: mailto:opencode@microsoft.com |
0 commit comments