Skip to content

Commit

Permalink
update format (#22)
Browse files Browse the repository at this point in the history
Co-authored-by: Tong Xu (MSFT) <[email protected]>
  • Loading branch information
2 people authored and jongio committed Jan 16, 2020
1 parent 95022ee commit e4ccb6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
28 changes: 16 additions & 12 deletions v11/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@



import os, uuid, sys
import os
import uuid
import sys
from azure.storage.blob import BlockBlobService, PublicAccess

# ---------------------------------------------------------------------------------------------------------
Expand All @@ -42,21 +44,23 @@

def run_sample():
try:
# Create the BlockBlockService that is used to call the Blob service for the storage account
blob_service_client = BlockBlobService(account_name='accountname', account_key='accountkey')
# Create the BlockBlobService that is used to call the Blob service for the storage account
blob_service_client = BlockBlobService(
account_name='accountname', account_key='accountkey')

# Create a container called 'quickstartblobs'.
container_name = 'quickstartblobs'
blob_service_client.create_container(container_name)

# Set the permission so the blobs are public.
blob_service_client.set_container_acl(container_name, public_access=PublicAccess.Container)
blob_service_client.set_container_acl(
container_name, public_access=PublicAccess.Container)

# Create Sample folder if it not exists, and create a file in folder Sample to test the upload and download.
local_path = os.path.expanduser("~/Sample")
if not os.path.exists(local_path):
os.makedirs(os.path.expanduser("~/Sample"))
local_file_name ="QuickStart_" + str(uuid.uuid4()) + ".txt"
local_file_name = "QuickStart_" + str(uuid.uuid4()) + ".txt"
full_path_to_file = os.path.join(local_path, local_file_name)

# Write text to the file.
Expand All @@ -68,7 +72,8 @@ def run_sample():
print("\nUploading to Blob storage as blob" + local_file_name)

# Upload the created file, use local_file_name for the blob name
blob_service_client.create_blob_from_path(container_name, local_file_name, full_path_to_file)
blob_service_client.create_blob_from_path(
container_name, local_file_name, full_path_to_file)

# List the blobs in the container
print("\nList blobs in the container")
Expand All @@ -78,9 +83,11 @@ def run_sample():

# Download the blob(s).
# Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
full_path_to_file2 = os.path.join(local_path, str.replace(local_file_name ,'.txt', '_DOWNLOADED.txt'))
full_path_to_file2 = os.path.join(local_path, str.replace(
local_file_name ,'.txt', '_DOWNLOADED.txt'))
print("\nDownloading blob to " + full_path_to_file2)
blob_service_client.get_blob_to_path(container_name, local_file_name, full_path_to_file2)
blob_service_client.get_blob_to_path(
container_name, local_file_name, full_path_to_file2)

sys.stdout.write("Sample finished running. When you hit <any key>, the sample will be deleted and the sample "
"application will exit.")
Expand All @@ -97,7 +104,4 @@ def run_sample():

# Main method.
if __name__ == '__main__':
run_sample()



run_sample()
20 changes: 11 additions & 9 deletions v12/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@



import os, uuid, sys
import os
import uuid
import sys
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, PublicAccess

# ---------------------------------------------------------------------------------------------------------
Expand All @@ -48,13 +50,14 @@ def run_sample():

# Create a container called 'quickstartblobs' and Set the permission so the blobs are public.
container_name = 'quickstartblobs'
blob_service_client.create_container(container_name, public_access=PublicAccess.Container)
blob_service_client.create_container(
container_name, public_access=PublicAccess.Container)

# Create Sample folder if it not exists, and create a file in folder Sample to test the upload and download.
local_path = os.path.expanduser("~/Sample")
if not os.path.exists(local_path):
os.makedirs(os.path.expanduser("~/Sample"))
local_file_name ="QuickStart_" + str(uuid.uuid4()) + ".txt"
local_file_name = "QuickStart_" + str(uuid.uuid4()) + ".txt"
full_path_to_file = os.path.join(local_path, local_file_name)

# Write text to the file.
Expand All @@ -66,7 +69,8 @@ def run_sample():
print("\nUploading to Blob storage as blob" + local_file_name)

# Upload the created file, use local_file_name for the blob name
blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)
blob_client = blob_service_client.get_blob_client(
container=container_name, blob=local_file_name)
with open(full_path_to_file, "rb") as data:
blob_client.upload_blob(data)

Expand All @@ -79,7 +83,8 @@ def run_sample():

# Download the blob(s).
# Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
full_path_to_file2 = os.path.join(local_path, str.replace(local_file_name ,'.txt', '_DOWNLOADED.txt'))
full_path_to_file2 = os.path.join(local_path, str.replace(
local_file_name ,'.txt', '_DOWNLOADED.txt'))
print("\nDownloading blob to " + full_path_to_file2)
with open(full_path_to_file2, "wb") as my_blob:
my_blob.writelines([blob_client.download_blob().readall()])
Expand All @@ -99,7 +104,4 @@ def run_sample():

# Main method.
if __name__ == '__main__':
run_sample()



run_sample()

0 comments on commit e4ccb6a

Please sign in to comment.