19
19
20
20
package org .elasticsearch .cloud .azure ;
21
21
22
- import com .microsoft .windowsazure .services .blob .BlobConfiguration ;
23
- import com .microsoft .windowsazure .services .blob .BlobContract ;
24
- import com .microsoft .windowsazure .services .blob .BlobService ;
25
- import com .microsoft .windowsazure .services .blob .client .CloudBlobClient ;
26
- import com .microsoft .windowsazure .services .blob .client .CloudBlobContainer ;
27
- import com .microsoft .windowsazure .services .blob .client .CloudBlockBlob ;
28
- import com .microsoft .windowsazure .services .blob .client .ListBlobItem ;
29
- import com .microsoft .windowsazure .services .blob .models .BlobProperties ;
30
- import com .microsoft .windowsazure .services .blob .models .GetBlobResult ;
31
- import com .microsoft .windowsazure .services .blob .models .ListBlobsOptions ;
32
- import com .microsoft .windowsazure .services .blob .models .ListBlobsResult ;
33
- import com .microsoft .windowsazure .services .core .Configuration ;
34
- import com .microsoft .windowsazure .services .core .ServiceException ;
35
- import com .microsoft .windowsazure .services .core .storage .CloudStorageAccount ;
36
- import com .microsoft .windowsazure .services .core .storage .StorageException ;
22
+ import com .microsoft .azure .storage .CloudStorageAccount ;
23
+ import com .microsoft .azure .storage .StorageException ;
24
+ import com .microsoft .azure .storage .blob .*;
37
25
import org .elasticsearch .ElasticsearchException ;
38
26
import org .elasticsearch .common .blobstore .BlobMetaData ;
39
27
import org .elasticsearch .common .blobstore .support .PlainBlobMetaData ;
48
36
import java .io .OutputStream ;
49
37
import java .net .URI ;
50
38
import java .net .URISyntaxException ;
51
- import java .util .List ;
52
39
53
40
/**
54
41
*
@@ -60,10 +47,7 @@ public class AzureStorageServiceImpl extends AbstractLifecycleComponent<AzureSto
60
47
private final String key ;
61
48
private final String blob ;
62
49
63
- private CloudStorageAccount storage_account ;
64
50
private CloudBlobClient client ;
65
- private BlobContract service ;
66
-
67
51
68
52
@ Inject
69
53
public AzureStorageServiceImpl (Settings settings , SettingsFilter settingsFilter ) {
@@ -84,14 +68,11 @@ public AzureStorageServiceImpl(Settings settings, SettingsFilter settingsFilter)
84
68
+ "AccountName=" + account +";"
85
69
+ "AccountKey=" + key ;
86
70
87
- Configuration configuration = Configuration .getInstance ();
88
- configuration .setProperty (BlobConfiguration .ACCOUNT_NAME , account );
89
- configuration .setProperty (BlobConfiguration .ACCOUNT_KEY , key );
90
- configuration .setProperty (BlobConfiguration .URI , blob );
91
- service = BlobService .create (configuration );
71
+ // Retrieve storage account from connection-string.
72
+ CloudStorageAccount storageAccount = CloudStorageAccount .parse (storageConnectionString );
92
73
93
- storage_account = CloudStorageAccount . parse ( storageConnectionString );
94
- client = storage_account .createCloudBlobClient ();
74
+ // Create the blob client.
75
+ client = storageAccount .createCloudBlobClient ();
95
76
}
96
77
} catch (Exception e ) {
97
78
// Can not start Azure Storage Client
@@ -129,27 +110,23 @@ public void createContainer(String container) throws URISyntaxException, Storage
129
110
try {
130
111
CloudBlobContainer blob_container = client .getContainerReference (container );
131
112
logger .trace ("creating container [{}]" , container );
132
- blob_container .createIfNotExist ();
113
+ blob_container .createIfNotExists ();
133
114
} catch (IllegalArgumentException e ) {
134
115
logger .trace ("fails creating container [{}]" , container , e .getMessage ());
135
116
throw new RepositoryException (container , e .getMessage ());
136
117
}
137
118
}
138
119
139
120
@ Override
140
- public void deleteFiles (String container , String path ) throws URISyntaxException , StorageException , ServiceException {
121
+ public void deleteFiles (String container , String path ) throws URISyntaxException , StorageException {
141
122
logger .trace ("delete files container [{}], path [{}]" , container , path );
142
123
143
124
// Container name must be lower case.
144
125
CloudBlobContainer blob_container = client .getContainerReference (container );
145
126
if (blob_container .exists ()) {
146
- ListBlobsOptions options = new ListBlobsOptions ();
147
- options .setPrefix (path );
148
-
149
- List <ListBlobsResult .BlobEntry > blobs = service .listBlobs (container , options ).getBlobs ();
150
- for (ListBlobsResult .BlobEntry blob : blobs ) {
151
- logger .trace ("removing in container [{}], path [{}], blob [{}]" , container , path , blob .getName ());
152
- service .deleteBlob (container , blob .getName ());
127
+ for (ListBlobItem blobItem : blob_container .listBlobs (path )) {
128
+ logger .trace ("removing blob [{}]" , blobItem .getUri ());
129
+ deleteBlob (container , blobItem .getUri ().toString ());
153
130
}
154
131
}
155
132
}
@@ -180,10 +157,9 @@ public void deleteBlob(String container, String blob) throws URISyntaxException,
180
157
}
181
158
182
159
@ Override
183
- public InputStream getInputStream (String container , String blob ) throws ServiceException {
160
+ public InputStream getInputStream (String container , String blob ) throws URISyntaxException , StorageException {
184
161
logger .trace ("reading container [{}], blob [{}]" , container , blob );
185
- GetBlobResult blobResult = service .getBlob (container , blob );
186
- return blobResult .getContentStream ();
162
+ return client .getContainerReference (container ).getBlockBlobReference (blob ).openInputStream ();
187
163
}
188
164
189
165
@ Override
@@ -193,21 +169,20 @@ public OutputStream getOutputStream(String container, String blob) throws URISyn
193
169
}
194
170
195
171
@ Override
196
- public ImmutableMap <String , BlobMetaData > listBlobsByPrefix (String container , String keyPath , String prefix ) throws URISyntaxException , StorageException , ServiceException {
172
+ public ImmutableMap <String , BlobMetaData > listBlobsByPrefix (String container , String keyPath , String prefix ) throws URISyntaxException , StorageException {
197
173
logger .debug ("listing container [{}], keyPath [{}], prefix [{}]" , container , keyPath , prefix );
198
174
ImmutableMap .Builder <String , BlobMetaData > blobsBuilder = ImmutableMap .builder ();
199
175
200
176
CloudBlobContainer blob_container = client .getContainerReference (container );
201
177
if (blob_container .exists ()) {
202
- Iterable <ListBlobItem > blobs = blob_container .listBlobs (keyPath + prefix );
203
- for (ListBlobItem blob : blobs ) {
204
- URI uri = blob .getUri ();
178
+ for (ListBlobItem blobItem : blob_container .listBlobs (keyPath + prefix )) {
179
+ URI uri = blobItem .getUri ();
205
180
logger .trace ("blob url [{}]" , uri );
206
181
String blobpath = uri .getPath ().substring (container .length () + 1 );
207
- BlobProperties properties = service . getBlobProperties ( container , blobpath ).getProperties ();
182
+ BlobProperties properties = blob_container . getBlockBlobReference ( blobpath ).getProperties ();
208
183
String name = blobpath .substring (keyPath .length () + 1 );
209
- logger .trace ("blob url [{}], name [{}], size [{}]" , uri , name , properties .getContentLength ());
210
- blobsBuilder .put (name , new PlainBlobMetaData (name , properties .getContentLength ()));
184
+ logger .trace ("blob url [{}], name [{}], size [{}]" , uri , name , properties .getLength ());
185
+ blobsBuilder .put (name , new PlainBlobMetaData (name , properties .getLength ()));
211
186
}
212
187
}
213
188
@@ -221,7 +196,7 @@ public void moveBlob(String container, String sourceBlob, String targetBlob) thr
221
196
CloudBlockBlob blobSource = blob_container .getBlockBlobReference (sourceBlob );
222
197
if (blobSource .exists ()) {
223
198
CloudBlockBlob blobTarget = blob_container .getBlockBlobReference (targetBlob );
224
- blobTarget .copyFromBlob (blobSource );
199
+ blobTarget .startCopyFromBlob (blobSource );
225
200
blobSource .delete ();
226
201
logger .debug ("moveBlob container [{}], sourceBlob [{}], targetBlob [{}] -> done" , container , sourceBlob , targetBlob );
227
202
}
0 commit comments