-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorageaccount.py
264 lines (203 loc) · 10.1 KB
/
storageaccount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
FILE: blob_samples_hello_world.py
DESCRIPTION:
This sample demos basic blob operations like getting a blob client from container, uploading and downloading
a blob using the blob_client.
USAGE: python blob_samples_hello_world.py
Set the environment variables with your own values before running the sample:
1) AZURE_STORAGE_CONNECTION_STRING - the connection string to your storage account
"""
import os
from pickle import FALSE, TRUE
# set up
SOURCE_FILE = 'static/images/Sebastien/seb/tartan.jpg'
DEST_FILE = 'BlockDestination.txt'
class BlobSamples(object):
connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
#--Begin Blob Samples-----------------------------------------------------------------
def create_container_sample(self):
# Instantiate a new BlobServiceClient using a connection string
from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(self.connection_string)
# set the container value
container_name="mycontainer"
fileName=os.path.basename(SOURCE_FILE)
# List containers in the storage account and check if already existing
list_response = blob_service_client.list_containers()
for item in list_response:
if item.name == container_name:
container_exist = TRUE
print("container {} exit -- do not create".format(item.name))
if not container_exist:
try:
# Create new container in the service
container_client.create_container()
except:
print("Cannot create container")
blob_client = container_client.get_blob_client(fileName)
with open(SOURCE_FILE, "rb") as data:
blob_client.upload_blob(data, blob_type="BlockBlob")
# get the list of blobs in the container
container_client = blob_service_client.get_container_client(item.name)
blobs_list = container_client.list_blobs()
# Check if the sourcefile already exist in the container
for blobs in blobs_list:
if blobs.name == str(fileName):
print("The file {} in {} already exist -- do not upload".format(fileName, item.name))
else:
print("The file {} in {} does not exist -- uplaoding".format(fileName, item.name))
# uplaod the source file
blob_client = container_client.get_blob_client(blobs.name)
with open(SOURCE_FILE, "rb") as data:
blob_client.upload_blob(data=data, blob_type="BlockBlob", overwrite=True)
else:
try:
# Create new container in the service
container_client.create_container()
except:
print("Cannot create container")
return(1)
blob_client = container_client.get_blob_client(fileName)
with open(SOURCE_FILE, "rb") as data:
blob_client.upload_blob(data, blob_type="BlockBlob")
# if not container_exist:
# try:
# # Create new container in the service
# container_client.create_container()
# except:
# print("Cannot create container")
# return(1)
# else:
# # Instantiate a new BlobClient
# fileName=os.path.basename(SOURCE_FILE)
# blob_client = container_client.get_blob_client(fileName)
# # [START upload_a_blob]
# # Upload content to block blob
# with open(SOURCE_FILE, "rb") as data:
# blob_client.upload_blob(data, blob_type="BlockBlob")
def block_blob_sample(self):
# Instantiate a new BlobServiceClient using a connection string
from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(self.connection_string)
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client("myblockcontainersync")
try:
# Create new Container in the service
container_client.create_container()
except:
print("container already exist")
# Instantiate a new BlobClient
blob_client = container_client.get_blob_client("myblockblob")
# [START upload_a_blob]
# Upload content to block blob
with open(SOURCE_FILE, "rb") as data:
blob_client.upload_blob(data, blob_type="BlockBlob")
# [END upload_a_blob]
# [START download_a_blob]
# with open(DEST_FILE, "wb") as my_blob:
# download_stream = blob_client.download_blob()
# my_blob.write(download_stream.readall())
# [END download_a_blob]
# [START delete_blob]
#blob_client.delete_blob()
# [END delete_blob]
# finally:
# # Delete the container
# list_response = blob_service_client.list_containers()
# print(list_response)
# print("nothing")
# #container_client.delete_container()
def stream_block_blob(self):
import uuid
# Instantiate a new BlobServiceClient using a connection string - set chunk size to 1MB
from azure.storage.blob import BlobServiceClient, BlobBlock
blob_service_client = BlobServiceClient.from_connection_string(self.connection_string,
max_single_get_size=1024*1024,
max_chunk_get_size=1024*1024)
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client("containersync")
# Generate 4MB of data
data = b'a'*4*1024*1024
try:
# Create new Container in the service
container_client.create_container()
# Instantiate a new source blob client
source_blob_client = container_client.get_blob_client("source_blob")
# Upload content to block blob
source_blob_client.upload_blob(data, blob_type="BlockBlob")
destination_blob_client = container_client.get_blob_client("destination_blob")
# [START download_a_blob_in_chunk]
# This returns a StorageStreamDownloader.
stream = source_blob_client.download_blob()
block_list = []
# Read data in chunks to avoid loading all into memory at once
for chunk in stream.chunks():
# process your data (anything can be done here really. `chunk` is a byte array).
block_id = str(uuid.uuid4())
destination_blob_client.stage_block(block_id=block_id, data=chunk)
block_list.append(BlobBlock(block_id=block_id))
# [END download_a_blob_in_chunk]
# Upload the whole chunk to azure storage and make up one blob
destination_blob_client.commit_block_list(block_list)
finally:
# Delete container
container_client.delete_container()
def page_blob_sample(self):
# Instantiate a new BlobServiceClient using a connection string
from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(self.connection_string)
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client("mypagecontainersync")
try:
# Create new Container in the Service
container_client.create_container()
# Instantiate a new BlobClient
blob_client = container_client.get_blob_client("mypageblob")
# Upload content to the Page Blob
data = b'abcd'*128
blob_client.upload_blob(data, blob_type="PageBlob")
# Download Page Blob
with open(DEST_FILE, "wb") as my_blob:
download_stream = blob_client.download_blob()
my_blob.write(download_stream.readall())
# Delete Page Blob
blob_client.delete_blob()
finally:
# Delete container
container_client.delete_container()
def append_blob_sample(self):
# Instantiate a new BlobServiceClient using a connection string
from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(self.connection_string)
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client("myappendcontainersync")
try:
# Create new Container in the Service
container_client.create_container()
# Instantiate a new BlobClient
blob_client = container_client.get_blob_client("myappendblob")
# Upload content to the Page Blob
with open(SOURCE_FILE, "rb") as data:
blob_client.upload_blob(data, blob_type="AppendBlob")
# Download Append Blob
with open(DEST_FILE, "wb") as my_blob:
download_stream = blob_client.download_blob()
my_blob.write(download_stream.readall())
# Delete Append Blob
blob_client.delete_blob()
finally:
# Delete container
container_client.delete_container()
if __name__ == '__main__':
sample = BlobSamples()
sample.create_container_sample()
#sample.block_blob_sample()
# sample.append_blob_sample()
# sample.page_blob_sample()
# sample.stream_block_blob()