Skip to content

Commit 8ccb723

Browse files
author
Rakshith Bhyravabhotla
authored
Samples for Files (#6730)
* samples for files * update README * update readme * comments
1 parent 6fafa36 commit 8ccb723

11 files changed

+897
-36
lines changed

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

+88-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,87 @@ with open("./SampleSource.txt", "rb") as source_file:
126126
file_client.upload_file(source_file)
127127
```
128128

129+
### Download a file
130+
Download a file to the share
131+
132+
```python
133+
from azure.storage.file import FileClient
134+
135+
file_client = FileClient.from_connection_string("my_connection_string", share="share", file_path="myfile")
136+
137+
with open("DEST_FILE", "wb") as data:
138+
file_client.download_file(data)
139+
```
140+
141+
### List contents of a directory.
142+
Lists all the directories and files under the directory.
143+
144+
```python
145+
from azure.storage.file import ShareClient
146+
share = ShareClient.from_connection_string(self.connection_string, "subdirshare")
147+
parent_dir = share.get_directory_client(directory_path="parentdir")
148+
149+
my_list = list(parent_dir.list_directories_and_files())
150+
print(my_list)
151+
```
152+
153+
### Client creation with a connection string
154+
Create the FileServiceClient using the connection string to your Azure Storage account.
155+
156+
```python
157+
from azure.storage.file.aio import FileServiceClient
158+
159+
service = FileServiceClient.from_connection_string("my_connection_string")
160+
```
161+
162+
### Create a file share asynchronously
163+
Create a file share to store your files.
164+
165+
```python
166+
from azure.storage.file.aio import ShareClient
167+
168+
share = ShareClient.from_connection_string("my_connection_string", share="myshare")
169+
await share.create_share()
170+
```
171+
172+
### Upload a file asynchronously
173+
Upload a file to the share
174+
175+
```python
176+
from azure.storage.file.aio import FileClient
177+
178+
file_client = FileClient.from_connection_string("my_connection_string", share="share", file_path="myfile")
179+
180+
with open("./SampleSource.txt", "rb") as source_file:
181+
await file_client.upload_file(source_file)
182+
```
183+
184+
### Download a file asynchronously
185+
Download a file to the share
186+
187+
```python
188+
from azure.storage.file.aio import FileClient
189+
190+
file_client = FileClient.from_connection_string("my_connection_string", share="share", file_path="myfile")
191+
192+
with open("DEST_FILE", "wb") as data:
193+
await file_client.download_file(data)
194+
```
195+
196+
### List contents of a directory asynchronously
197+
Lists all the directories and files under the directory.
198+
199+
```python
200+
from azure.storage.file import ShareClient
201+
share = ShareClient.from_connection_string(self.connection_string, "subdirshare")
202+
parent_dir = share.get_directory_client(directory_path="parentdir")
203+
204+
my_files = []
205+
async for item in parent_dir.list_directories_and_files():
206+
my_list.append(item)
207+
print(my_list)
208+
```
209+
129210
## Troubleshooting
130211
Storage File clients raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/docs/exceptions.md).
131212

@@ -139,38 +220,44 @@ Get started with our [File samples](https://github.com/Azure/azure-sdk-for-pytho
139220
Several Storage File 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 File:
140221

141222
* [`test_file_samples_hello_world.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_hello_world.py) - Examples found in this article:
223+
* [`test_file_samples_hello_world.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_hello_world_async.py) - Examples found in this article:
142224
* Client creation
143225
* Create a file share
144226
* Upload a file
145227

146228
* [`test_file_samples_authentication.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_authentication.py) - Examples for authenticating and creating the client:
229+
* [`test_file_samples_authentication.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_authentication_async.py) - Examples for authenticating and creating the client:
147230
* From a connection string
148231
* From a shared access key
149232
* From a shared access signature token
150233

151234
* [`test_file_samples_service.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_service.py) - Examples for interacting with the file service:
235+
* [`test_file_samples_service.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_service_async.py) - Examples for interacting with the file service:
152236
* Get and set service properties
153237
* Create, list, and delete shares
154238
* Get a share client
155239

156240
* [`test_file_samples_share.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_share.py) - Examples for interacting with file shares:
241+
* [`test_file_samples_share.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_share_async.py) - Examples for interacting with file shares:
157242
* Create a share snapshot
158243
* Set share quota and metadata
159244
* List directories and files
160245
* Get the directory or file client to interact with a specific entity
161246

162247
* [`test_file_samples_directory.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_directory.py) - Examples for interacting with directories:
248+
* [`test_file_samples_directory.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_directory_async.py) - Examples for interacting with directories:
163249
* Create a directory and add files
164250
* Create and delete subdirectories
165251
* Get the subdirectory client
166252

167253
* [`test_file_samples_file.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_file.py) - Examples for interacting with files:
254+
* [`test_file_samples_file.py`]([async version]https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-file/tests/test_file_samples_file_async.py) - Examples for interacting with files:
168255
* Create, upload, download, and delete files
169256
* Copy a file from a URL
170257

171258
### Additional documentation
172259

173-
For more extensive documentation on the Azure Storage File, see the [Azure Storage File documentation](https://docs.microsoft.com/azure/storage/) on docs.microsoft.com.
260+
For more extensive documentation on the Azure Storage File, see the [Azure Storage File documentation](https://azure.github.io/azure-sdk-for-python/ref/azure.storage.file) on docs.microsoft.com.
174261

175262

176263
## Contributing

sdk/storage/azure-storage-file/azure/storage/file/aio/directory_client_async.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ def get_subdirectory_client(self, directory_name, **kwargs):
120120
:param str directory_name:
121121
The name of the subdirectory.
122122
:returns: A Directory Client.
123-
:rtype: ~azure.storage.file.directory_client.DirectoryClient
123+
:rtype: ~azure.storage.file.aio.directory_client_async.DirectoryClient
124124
125125
Example:
126-
.. literalinclude:: ../tests/test_file_samples_directory.py
126+
.. literalinclude:: ../tests/test_file_samples_directory_async.py
127127
:start-after: [START get_subdirectory_client]
128128
:end-before: [END get_subdirectory_client]
129129
:language: python
@@ -154,7 +154,7 @@ async def create_directory( # type: ignore
154154
:rtype: dict(str, Any)
155155
156156
Example:
157-
.. literalinclude:: ../tests/test_file_samples_directory.py
157+
.. literalinclude:: ../tests/test_file_samples_directory_async.py
158158
:start-after: [START create_directory]
159159
:end-before: [END create_directory]
160160
:language: python
@@ -183,7 +183,7 @@ async def delete_directory(self, timeout=None, **kwargs):
183183
:rtype: None
184184
185185
Example:
186-
.. literalinclude:: ../tests/test_file_samples_directory.py
186+
.. literalinclude:: ../tests/test_file_samples_directory_async.py
187187
:start-after: [START delete_directory]
188188
:end-before: [END delete_directory]
189189
:language: python
@@ -209,7 +209,7 @@ def list_directories_and_files(self, name_starts_with=None, timeout=None, **kwar
209209
:rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.storage.file.models.DirectoryProperties]
210210
211211
Example:
212-
.. literalinclude:: ../tests/test_file_samples_directory.py
212+
.. literalinclude:: ../tests/test_file_samples_directory_async.py
213213
:start-after: [START lists_directory]
214214
:end-before: [END lists_directory]
215215
:language: python
@@ -366,10 +366,10 @@ async def create_subdirectory(
366366
:param int timeout:
367367
The timeout parameter is expressed in seconds.
368368
:returns: DirectoryClient
369-
:rtype: ~azure.storage.file.directory_client.DirectoryClient
369+
:rtype: ~azure.storage.file.aio.directory_client_async.DirectoryClient
370370
371371
Example:
372-
.. literalinclude:: ../tests/test_file_samples_directory.py
372+
.. literalinclude:: ../tests/test_file_samples_directory_async.py
373373
:start-after: [START create_subdirectory]
374374
:end-before: [END create_subdirectory]
375375
:language: python
@@ -396,7 +396,7 @@ async def delete_subdirectory(
396396
:rtype: None
397397
398398
Example:
399-
.. literalinclude:: ../tests/test_file_samples_directory.py
399+
.. literalinclude:: ../tests/test_file_samples_directory_async.py
400400
:start-after: [START delete_subdirectory]
401401
:end-before: [END delete_subdirectory]
402402
:language: python
@@ -448,10 +448,10 @@ async def upload_file(
448448
:param str encoding:
449449
Defaults to UTF-8.
450450
:returns: FileClient
451-
:rtype: ~azure.storage.file.file_client.FileClient
451+
:rtype: ~azure.storage.file.aio.file_client_async.FileClient
452452
453453
Example:
454-
.. literalinclude:: ../tests/test_file_samples_directory.py
454+
.. literalinclude:: ../tests/test_file_samples_directory_async.py
455455
:start-after: [START upload_file_to_directory]
456456
:end-before: [END upload_file_to_directory]
457457
:language: python
@@ -488,7 +488,7 @@ async def delete_file(
488488
:rtype: None
489489
490490
Example:
491-
.. literalinclude:: ../tests/test_file_samples_directory.py
491+
.. literalinclude:: ../tests/test_file_samples_directory_async.py
492492
:start-after: [START delete_file_in_directory]
493493
:end-before: [END delete_file_in_directory]
494494
:language: python

sdk/storage/azure-storage-file/azure/storage/file/aio/file_client_async.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async def create_file( # type: ignore
153153
:rtype: dict(str, Any)
154154
155155
Example:
156-
.. literalinclude:: ../tests/test_file_samples_file.py
156+
.. literalinclude:: ../tests/test_file_samples_file_async.py
157157
:start-after: [START create_file]
158158
:end-before: [END create_file]
159159
:language: python
@@ -230,7 +230,7 @@ async def upload_file(
230230
:rtype: dict(str, Any)
231231
232232
Example:
233-
.. literalinclude:: ../tests/test_file_samples_file.py
233+
.. literalinclude:: ../tests/test_file_samples_file_async.py
234234
:start-after: [START upload_file]
235235
:end-before: [END upload_file]
236236
:language: python
@@ -293,7 +293,7 @@ async def start_copy_from_url(
293293
:rtype: dict(str, Any)
294294
295295
Example:
296-
.. literalinclude:: ../tests/test_file_samples_file.py
296+
.. literalinclude:: ../tests/test_file_samples_file_async.py
297297
:start-after: [START copy_file_from_url]
298298
:end-before: [END copy_file_from_url]
299299
:language: python
@@ -368,7 +368,7 @@ async def download_file(
368368
:returns: A iterable data generator (stream)
369369
370370
Example:
371-
.. literalinclude:: ../tests/test_file_samples_file.py
371+
.. literalinclude:: ../tests/test_file_samples_file_async.py
372372
:start-after: [START download_file]
373373
:end-before: [END download_file]
374374
:language: python
@@ -407,7 +407,7 @@ async def delete_file(self, timeout=None, **kwargs):
407407
:rtype: None
408408
409409
Example:
410-
.. literalinclude:: ../tests/test_file_samples_file.py
410+
.. literalinclude:: ../tests/test_file_samples_file_async.py
411411
:start-after: [START delete_file]
412412
:end-before: [END delete_file]
413413
:language: python

sdk/storage/azure-storage-file/azure/storage/file/aio/file_service_client_async.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class FileServiceClient(AsyncStorageAccountHostsMixin, FileServiceClientBase):
6666
shared access key.
6767
6868
Example:
69-
.. literalinclude:: ../tests/test_file_samples_authentication.py
69+
.. literalinclude:: ../tests/test_file_samples_authentication_async.py
7070
:start-after: [START create_file_service_client]
7171
:end-before: [END create_file_service_client]
7272
:language: python
@@ -100,7 +100,7 @@ async def get_service_properties(self, timeout=None, **kwargs):
100100
:rtype: ~azure.storage.file._generated.models.StorageServiceProperties
101101
102102
Example:
103-
.. literalinclude:: ../tests/test_file_samples_service.py
103+
.. literalinclude:: ../tests/test_file_samples_service_async.py
104104
:start-after: [START get_service_properties]
105105
:end-before: [END get_service_properties]
106106
:language: python
@@ -143,7 +143,7 @@ async def set_service_properties(
143143
:rtype: None
144144
145145
Example:
146-
.. literalinclude:: ../tests/test_file_samples_service.py
146+
.. literalinclude:: ../tests/test_file_samples_service_async.py
147147
:start-after: [START set_service_properties]
148148
:end-before: [END set_service_properties]
149149
:language: python
@@ -185,7 +185,7 @@ def list_shares(
185185
:rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.storage.file.models.ShareProperties]
186186
187187
Example:
188-
.. literalinclude:: ../tests/test_file_samples_service.py
188+
.. literalinclude:: ../tests/test_file_samples_service_async.py
189189
:start-after: [START fsc_list_shares]
190190
:end-before: [END fsc_list_shares]
191191
:language: python
@@ -229,10 +229,10 @@ async def create_share(
229229
Quota in bytes.
230230
:param int timeout:
231231
The timeout parameter is expressed in seconds.
232-
:rtype: ~azure.storage.file.share_client.ShareClient
232+
:rtype: ~azure.storage.file.aio.share_client_async.ShareClient
233233
234234
Example:
235-
.. literalinclude:: ../tests/test_file_samples_service.py
235+
.. literalinclude:: ../tests/test_file_samples_service_async.py
236236
:start-after: [START fsc_create_shares]
237237
:end-before: [END fsc_create_shares]
238238
:language: python
@@ -265,7 +265,7 @@ async def delete_share(
265265
:rtype: None
266266
267267
Example:
268-
.. literalinclude:: ../tests/test_file_samples_service.py
268+
.. literalinclude:: ../tests/test_file_samples_service_async.py
269269
:start-after: [START fsc_delete_shares]
270270
:end-before: [END fsc_delete_shares]
271271
:language: python
@@ -288,10 +288,10 @@ def get_share_client(self, share, snapshot=None):
288288
:param str snapshot:
289289
An optional share snapshot on which to operate.
290290
:returns: A ShareClient.
291-
:rtype: ~azure.storage.file.share_client.ShareClient
291+
:rtype: ~azure.storage.file.aio.share_client_async.ShareClient
292292
293293
Example:
294-
.. literalinclude:: ../tests/test_file_samples_service.py
294+
.. literalinclude:: ../tests/test_file_samples_service_async.py
295295
:start-after: [START get_share_client]
296296
:end-before: [END get_share_client]
297297
:language: python

0 commit comments

Comments
 (0)