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 ;
47
35
import java .io .OutputStream ;
48
36
import java .net .URI ;
49
37
import java .net .URISyntaxException ;
50
- import java .util .List ;
51
38
52
39
/**
53
40
*
@@ -59,10 +46,7 @@ public class AzureStorageServiceImpl extends AbstractLifecycleComponent<AzureSto
59
46
private final String key ;
60
47
private final String blob ;
61
48
62
- private CloudStorageAccount storage_account ;
63
49
private CloudBlobClient client ;
64
- private BlobContract service ;
65
-
66
50
67
51
@ Inject
68
52
public AzureStorageServiceImpl (Settings settings , SettingsFilter settingsFilter ) {
@@ -83,14 +67,11 @@ public AzureStorageServiceImpl(Settings settings, SettingsFilter settingsFilter)
83
67
+ "AccountName=" + account +";"
84
68
+ "AccountKey=" + key ;
85
69
86
- Configuration configuration = Configuration .getInstance ();
87
- configuration .setProperty (BlobConfiguration .ACCOUNT_NAME , account );
88
- configuration .setProperty (BlobConfiguration .ACCOUNT_KEY , key );
89
- configuration .setProperty (BlobConfiguration .URI , blob );
90
- service = BlobService .create (configuration );
70
+ // Retrieve storage account from connection-string.
71
+ CloudStorageAccount storageAccount = CloudStorageAccount .parse (storageConnectionString );
91
72
92
- storage_account = CloudStorageAccount . parse ( storageConnectionString );
93
- client = storage_account .createCloudBlobClient ();
73
+ // Create the blob client.
74
+ client = storageAccount .createCloudBlobClient ();
94
75
}
95
76
} catch (Exception e ) {
96
77
// Can not start Azure Storage Client
@@ -127,23 +108,19 @@ public void removeContainer(String container) throws URISyntaxException, Storage
127
108
public void createContainer (String container ) throws URISyntaxException , StorageException {
128
109
CloudBlobContainer blob_container = client .getContainerReference (container );
129
110
logger .trace ("creating container [{}]" , container );
130
- blob_container .createIfNotExist ();
111
+ blob_container .createIfNotExists ();
131
112
}
132
113
133
114
@ Override
134
- public void deleteFiles (String container , String path ) throws URISyntaxException , StorageException , ServiceException {
115
+ public void deleteFiles (String container , String path ) throws URISyntaxException , StorageException {
135
116
logger .trace ("delete files container [{}], path [{}]" , container , path );
136
117
137
118
// Container name must be lower case.
138
119
CloudBlobContainer blob_container = client .getContainerReference (container );
139
120
if (blob_container .exists ()) {
140
- ListBlobsOptions options = new ListBlobsOptions ();
141
- options .setPrefix (path );
142
-
143
- List <ListBlobsResult .BlobEntry > blobs = service .listBlobs (container , options ).getBlobs ();
144
- for (ListBlobsResult .BlobEntry blob : blobs ) {
145
- logger .trace ("removing in container [{}], path [{}], blob [{}]" , container , path , blob .getName ());
146
- service .deleteBlob (container , blob .getName ());
121
+ for (ListBlobItem blobItem : blob_container .listBlobs (path )) {
122
+ logger .trace ("removing blob [{}]" , blobItem .getUri ());
123
+ deleteBlob (container , blobItem .getUri ().toString ());
147
124
}
148
125
}
149
126
}
@@ -174,9 +151,8 @@ public void deleteBlob(String container, String blob) throws URISyntaxException,
174
151
}
175
152
176
153
@ Override
177
- public InputStream getInputStream (String container , String blob ) throws ServiceException {
178
- GetBlobResult blobResult = service .getBlob (container , blob );
179
- return blobResult .getContentStream ();
154
+ public InputStream getInputStream (String container , String blob ) throws URISyntaxException , StorageException {
155
+ return client .getContainerReference (container ).getBlockBlobReference (blob ).openInputStream ();
180
156
}
181
157
182
158
@ Override
@@ -185,21 +161,20 @@ public OutputStream getOutputStream(String container, String blob) throws URISyn
185
161
}
186
162
187
163
@ Override
188
- public ImmutableMap <String , BlobMetaData > listBlobsByPrefix (String container , String keyPath , String prefix ) throws URISyntaxException , StorageException , ServiceException {
164
+ public ImmutableMap <String , BlobMetaData > listBlobsByPrefix (String container , String keyPath , String prefix ) throws URISyntaxException , StorageException {
189
165
logger .debug ("listBlobsByPrefix container [{}], keyPath [{}], prefix [{}]" , container , keyPath , prefix );
190
166
ImmutableMap .Builder <String , BlobMetaData > blobsBuilder = ImmutableMap .builder ();
191
167
192
168
CloudBlobContainer blob_container = client .getContainerReference (container );
193
169
if (blob_container .exists ()) {
194
- Iterable <ListBlobItem > blobs = blob_container .listBlobs (keyPath + prefix );
195
- for (ListBlobItem blob : blobs ) {
196
- URI uri = blob .getUri ();
170
+ for (ListBlobItem blobItem : blob_container .listBlobs (keyPath + prefix )) {
171
+ URI uri = blobItem .getUri ();
197
172
logger .trace ("blob url [{}]" , uri );
198
173
String blobpath = uri .getPath ().substring (container .length () + 1 );
199
- BlobProperties properties = service . getBlobProperties ( container , blobpath ).getProperties ();
174
+ BlobProperties properties = blob_container . getBlockBlobReference ( blobpath ).getProperties ();
200
175
String name = blobpath .substring (keyPath .length () + 1 );
201
- logger .trace ("blob url [{}], name [{}], size [{}]" , uri , name , properties .getContentLength ());
202
- blobsBuilder .put (name , new PlainBlobMetaData (name , properties .getContentLength ()));
176
+ logger .trace ("blob url [{}], name [{}], size [{}]" , uri , name , properties .getLength ());
177
+ blobsBuilder .put (name , new PlainBlobMetaData (name , properties .getLength ()));
203
178
}
204
179
}
205
180
0 commit comments