|
1 |
| -# sdk/storage/azure_storage_blob |
| 1 | +# Azure Storage Blob client library for Rust |
2 | 2 |
|
3 |
| -TODO TODO TODO |
| 3 | +Azure Blob storage is Microsoft's object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data, such as text or binary data. |
| 4 | + |
| 5 | +[Source code] | [Package (crates.io)] | [API reference documentation] | [REST API documentation] | [Product documentation] |
| 6 | + |
| 7 | +## Getting started |
| 8 | + |
| 9 | +### Install the package |
| 10 | + |
| 11 | +Install the Azure Storage Blob client library for Rust with [cargo]: |
| 12 | + |
| 13 | +```sh |
| 14 | +cargo add azure_storage_blob |
| 15 | +``` |
| 16 | + |
| 17 | +### Prerequisites |
| 18 | + |
| 19 | +* You must have an [Azure subscription] and an [Azure storage account] to use this package. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +### Create a storage account |
| 24 | +If you wish to create a new storage account, you can use the |
| 25 | +[Azure Portal], [Azure PowerShell], or [Azure CLI]: |
| 26 | +```bash |
| 27 | +# Create a new resource group to hold the storage account - |
| 28 | +# if using an existing resource group, skip this step |
| 29 | +az group create --name my-resource-group --location westus2 |
| 30 | + |
| 31 | +# Create the storage account |
| 32 | +az storage account create -n my-storage-account-name -g my-resource-group |
| 33 | +``` |
| 34 | + |
| 35 | +#### Authenticate the client |
| 36 | + |
| 37 | +In order to interact with the Azure Blob Storage service, you'll need to create an instance of a client, `BlobClient`, `BlobContainerClient`, or `BlobServiceClient`. The [Azure Identity] library makes it easy to add Microsoft Entra ID support for authenticating Azure SDK clients with their corresponding Azure services: |
| 38 | +```rust |
| 39 | +use azure_storage_blob::BlobClient; |
| 40 | +use azure_identity::DefaultAzureCredential; |
| 41 | + |
| 42 | +// Create a BlobClient that will authenticate through Microsoft Entra ID |
| 43 | +let credential = DefaultAzureCredential::new()?; |
| 44 | +let blob_client = BlobClient::new( |
| 45 | + "https://<storage_account_name>.blob.core.windows.net/", // endpoint |
| 46 | + "container_name".to_string(), // container name |
| 47 | + "blob_name".to_string(), // blob name |
| 48 | + credential, // credential |
| 49 | + Some(BlobClientOptions::default()), // BlobClient options |
| 50 | +)?; |
| 51 | +``` |
| 52 | + |
| 53 | +#### Permissions |
| 54 | +You may need to specify RBAC roles to access Blob Storage via Microsoft Entra ID. Please see [Assign an Azure role for access to blob data] for more details. |
| 55 | + |
| 56 | +## Examples |
| 57 | + |
| 58 | +### Create `BlobClient` |
| 59 | +```rust |
| 60 | +use azure_storage_blob::BlobClient; |
| 61 | +use azure_identity::DefaultAzureCredential; |
| 62 | + |
| 63 | +// Create a BlobClient that will authenticate through Microsoft Entra ID |
| 64 | +let credential = DefaultAzureCredential::new()?; |
| 65 | +let blob_client = BlobClient::new( |
| 66 | + "https://<storage_account_name>.blob.core.windows.net/", // endpoint |
| 67 | + "container_name".to_string(), // container name |
| 68 | + "blob_name".to_string(), // blob name |
| 69 | + credential, // credential |
| 70 | + Some(BlobClientOptions::default()), // BlobClient options |
| 71 | +)?; |
| 72 | +``` |
| 73 | +### Upload Blob |
| 74 | +```rust |
| 75 | +use azure_storage_blob::BlobClient; |
| 76 | +use azure_identity::DefaultAzureCredential; |
| 77 | + |
| 78 | +let credential = DefaultAzureCredential::new()?; |
| 79 | +let blob_client = BlobClient::new( |
| 80 | + "https://<storage_account_name>.blob.core.windows.net/", |
| 81 | + "container_name".to_string(), |
| 82 | + "blob_name".to_string(), |
| 83 | + credential, |
| 84 | + Some(BlobClientOptions::default()), |
| 85 | +)?; |
| 86 | + |
| 87 | +blob_client |
| 88 | + .upload( |
| 89 | + RequestContent::from(data.to_vec()), // data |
| 90 | + false, // overwrite |
| 91 | + u64::try_from(data.len())?, // content length |
| 92 | + None, // upload options |
| 93 | + ) |
| 94 | + .await?; |
| 95 | +``` |
| 96 | + |
| 97 | +### Get Blob Properties |
| 98 | +```rust |
| 99 | +use azure_storage_blob::BlobClient; |
| 100 | +use azure_identity::DefaultAzureCredential; |
| 101 | + |
| 102 | +let credential = DefaultAzureCredential::new()?; |
| 103 | +let blob_client = BlobClient::new( |
| 104 | + "https://<storage_account_name>.blob.core.windows.net/", |
| 105 | + "container_name".to_string(), |
| 106 | + "blob_name".to_string(), |
| 107 | + credential, |
| 108 | + Some(BlobClientOptions::default()), |
| 109 | +)?; |
| 110 | +let blob_properties = blob_client.get_properties( |
| 111 | + None // get properties options |
| 112 | + ) |
| 113 | + .await?; |
| 114 | +``` |
| 115 | + |
| 116 | + |
| 117 | +## Next steps |
| 118 | + |
| 119 | +### Provide feedback |
| 120 | + |
| 121 | +If you encounter bugs or have suggestions, [open an issue](https://github.com/Azure/azure-sdk-for-rust/issues). |
| 122 | + |
| 123 | +## Contributing |
| 124 | + |
| 125 | +This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit [https://cla.microsoft.com](https://cla.microsoft.com). |
| 126 | + |
| 127 | +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You'll only need to do this once across all repos using our CLA. |
| 128 | + |
| 129 | +This project has adopted the [Microsoft Open Source Code of Conduct ](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
| 130 | + |
| 131 | +<!-- LINKS --> |
| 132 | +[Azure subscription]: https://azure.microsoft.com/free/ |
| 133 | +[Azure storage account]: https://learn.microsoft.com/azure/storage/common/storage-account-overview |
| 134 | +[Azure Portal]: https://learn.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-portal |
| 135 | +[Azure PowerShell]: https://learn.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-powershell |
| 136 | +[Azure CLI]: https://learn.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-cli |
| 137 | +[cargo]: https://dev-doc.rust-lang.org/stable/cargo/commands/cargo.html |
| 138 | +[Azure Identity]: https://github.com/Azure/azure-sdk-for-rust/tree/main/sdk/identity/azure_identity |
| 139 | +[API reference documentation]: https://docs.rs/crate/azure_storage_blob/latest |
| 140 | +[Package (crates.io)]: https://crates.io/crates/azure_storage_blob |
| 141 | +[Source code]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/storage/azure-storage-blob |
| 142 | +[REST API documentation]: https://learn.microsoft.com/rest/api/storageservices/blob-service-rest-api |
| 143 | +[Product documentation]: https://learn.microsoft.com/azure/storage/blobs/storage-blobs-overview |
| 144 | +[Assign an Azure role for access to blob data]: https://learn.microsoft.com/azure/storage/blobs/assign-azure-role-data-access?tabs=portal |
0 commit comments