Skip to content

Commit 6fafa36

Browse files
Async blob code samples (#6728)
* working on async blob samples * fix name * adds start copy blob sample and fixes typos * add async suffix to test samples * update rtypes to async * update readme * update readme link to docs * add copy blob to example list
1 parent 674af83 commit 6fafa36

16 files changed

+1580
-108
lines changed

sdk/storage/azure-storage-blob/README.md

+45-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Blob storage is ideal for:
99
* Storing data for backup and restore, disaster recovery, and archiving
1010
* Storing data for analysis by an on-premises or Azure-hosted service
1111

12-
[Source code](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/azure/storage/blob) | [Package (PyPi)](https://pypi.org/project/azure-storage-blob/) | [API reference documentation](https://docs.microsoft.com/rest/api/storageservices/blob-service-rest-api) | [Product documentation](https://docs.microsoft.com/azure/storage/) | [Samples](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests)
12+
[Source code](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/azure/storage/blob) | [Package (PyPi)](https://pypi.org/project/azure-storage-blob/) | [API reference documentation](https://azure.github.io/azure-sdk-for-python/ref/azure.storage.blob.html) | [Product documentation](https://docs.microsoft.com/azure/storage/) | [Samples](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests)
1313

1414

1515
## Getting started
@@ -124,6 +124,17 @@ blob = BlobClient.from_connection_string("my_connection_string", container="myco
124124
with open("./SampleSource.txt", "rb") as data:
125125
blob.upload_blob(data)
126126
```
127+
Use the async client to upload a blob to your container.
128+
129+
```python
130+
from azure.storage.blob.aio import BlobClient
131+
132+
blob = BlobClient.from_connection_string("my_connection_string", container="mycontainer", blob="my_blob")
133+
134+
with open("./SampleSource.txt", "rb") as data:
135+
await blob.upload_blob(data)
136+
```
137+
127138
### Downloading a blob
128139
Download a blob from your container.
129140

@@ -137,8 +148,21 @@ with open("./BlockDestination.txt", "wb") as my_blob:
137148
my_blob.writelines(blob_data.content_as_bytes())
138149
```
139150

151+
Download a blob asynchronously.
152+
153+
```python
154+
from azure.storage.blob.aio import BlobClient
155+
156+
blob = BlobClient.from_connection_string("my_connection_string", container="mycontainer", blob="my_blob")
157+
158+
with open("./BlockDestination.txt", "wb") as my_blob:
159+
stream = await blob.download_blob()
160+
data = await stream.content_as_bytes()
161+
my_blob.write(data)
162+
```
163+
140164
### Enumerating blobs
141-
List the blob in your container.
165+
List the blobs in your container.
142166

143167
```python
144168
from azure.storage.blob import ContainerClient
@@ -150,6 +174,19 @@ for blob in blob_list:
150174
print(blob.name + '\n')
151175
```
152176

177+
List the blobs asynchronously.
178+
179+
```python
180+
from azure.storage.blob.aio import ContainerClient
181+
182+
container = ContainerClient.from_connection_string("my_connection_string", container="mycontainer")
183+
184+
blob_list = []
185+
async for blob in container.list_blobs():
186+
blob_list.append(blob)
187+
print(blob_list)
188+
```
189+
153190
## Troubleshooting
154191
Storage Blob clients raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/docs/exceptions.md).
155192

@@ -163,27 +200,27 @@ Get started with our [Blob samples](https://github.com/Azure/azure-sdk-for-pytho
163200

164201
Several Storage Blobs Python SDK samples are available to you in the SDK's GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Storage Blobs:
165202

166-
* [`test_blob_samples_hello_world.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_hello_world.py) - Examples for common Storage Blob tasks:
203+
* [`test_blob_samples_hello_world.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_hello_world.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_hello_world_async.py)) - Examples for common Storage Blob tasks:
167204
* Set up a container
168205
* Create a block, page, or append blob
169206
* Upload blobs
170207
* Download blobs
171208
* Delete blobs
172209

173-
* [`test_blob_samples_authentication.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_authentication.py) - Examples for authenticating and creating the client:
210+
* [`test_blob_samples_authentication.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_authentication.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_authentication_async.py)) - Examples for authenticating and creating the client:
174211
* From a connection string
175212
* From a shared access key
176213
* From a shared access signature token
177214
* From active directory
178215

179-
* [`test_blob_samples_service.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_service.py) - Examples for interacting with the blob service:
216+
* [`test_blob_samples_service.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_service.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_service_async.py)) - Examples for interacting with the blob service:
180217
* Get account information
181218
* Get and set service properties
182219
* Get service statistics
183220
* Create, list, and delete containers
184221
* Get the Blob or Container client
185222

186-
* [`test_blob_samples_containers.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_containers.py) - Examples for interacting with containers:
223+
* [`test_blob_samples_containers.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_containers.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_containers_async.py)) - Examples for interacting with containers:
187224
* Create a container and delete containers
188225
* Set metadata on containers
189226
* Get container properties
@@ -192,12 +229,13 @@ Several Storage Blobs Python SDK samples are available to you in the SDK's GitHu
192229
* Upload, list, delete blobs in container
193230
* Get the blob client to interact with a specific blob
194231

195-
* [`test_blob_samples_common.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_common.py) - Examples common to all types of blobs:
232+
* [`test_blob_samples_common.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_common.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob/tests/test_blob_samples_common_async.py)) - Examples common to all types of blobs:
196233
* Create a snapshot
197234
* Delete a blob snapshot
198235
* Soft delete a blob
199236
* Undelete a blob
200237
* Acquire a lease on a blob
238+
* Copy a blob from a URL
201239

202240

203241
### Additional documentation

0 commit comments

Comments
 (0)