Quick.Azure.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. { ***************************************************************************
  2. Copyright (c) 2015-2017 Kike Pérez
  3. Unit : Quick.Azure
  4. Description : Azure blobs operations
  5. Author : Kike Pérez
  6. Version : 1.2
  7. Created : 27/08/2015
  8. Modified : 09/05/2017
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Azure;
  22. interface
  23. uses
  24. Classes,
  25. System.SysUtils,
  26. System.Generics.Collections,
  27. IPPeerClient,
  28. Data.Cloud.CloudAPI,
  29. Data.Cloud.AzureAPI;
  30. type
  31. TAzureProtocol = (azHTTP,azHTTPS);
  32. //TAzureBlob = Data.Cloud.AzureAPI.TAzureBlob;
  33. TBlobPublicAccess = Data.Cloud.AzureAPI.TBlobPublicAccess;
  34. TAzureResponseInfo = record
  35. StatusCode : Integer;
  36. StatusMsg : string;
  37. end;
  38. TAzureBlobObject = class
  39. Name : string;
  40. Size : Int64;
  41. LastModified : TDateTime;
  42. end;
  43. TBlobList = class (TObjectList<TAzureBlobObject>);
  44. TQuickAzure = class
  45. private
  46. fconAzure : TAzureConnectionInfo;
  47. fAccountName : string;
  48. fAccountKey : string;
  49. fAzureProtocol : TAzureProtocol;
  50. fTimeOut : Integer;
  51. procedure SetAccountName(azAccountName : string);
  52. procedure SetAccountKey(azAccountKey : string);
  53. procedure SetAzureProtocol(azProtocol : TAzureProtocol);
  54. function FileToArray(cFilename : string) : TArray<Byte>;
  55. function StreamToArray(cStream : TStream) : TArray<Byte>;
  56. function GMT2DateTime(const gmtdate : string):TDateTime;
  57. public
  58. constructor Create; overload;
  59. constructor Create(azAccountName, azAccountKey : string); overload;
  60. destructor Destroy; override;
  61. property AccountName : string read fAccountName write SetAccountName;
  62. property AccountKey : string read fAccountKey write SetAccountKey;
  63. property AzureProtocol : TAzureProtocol read fAzureProtocol write SetAzureProtocol;
  64. property TimeOut : Integer read fTimeOut write fTimeOut;
  65. function PutBlob(azContainer, cFilename, azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean; overload;
  66. function PutBlob(azContainer : string; cStream : TStream; azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean; overload;
  67. function GetBlob(azContainer, azBlobName, cFilenameTo : string; out azResponseInfo : TAzureResponseInfo) : Boolean; overload;
  68. function GetBlob(azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo; out Stream : TMemoryStream) : Boolean; overload;
  69. function GetBlob(const azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo) : TMemoryStream; overload;
  70. function CopyBlob(azSourceContainer, azSourceBlobName : string; azTargetContainer, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  71. function RenameBlob(const azContainer, azSourceBlobName, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  72. function ExistsObject(const azContainer, azBlobName : string) : Boolean;
  73. function ExistsFolder(azContainer : string; azFolderName : string) : Boolean;
  74. function DeleteBlob(azContainer,azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  75. function ListBlobs(azContainer : string; azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TBlobList;
  76. function ListBlobsNames(azContainer : string; azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TStrings;
  77. function ExistsContainer(azContainer : string) : Boolean;
  78. function ListContainers(azContainersStartWith : string; azResponseInfo : TAzureResponseInfo) : TStrings;
  79. function CreateContainer(azContainer : string; azPublicAccess : TBlobPublicAccess; out azResponseInfo : TAzureResponseInfo) : Boolean;
  80. function DeleteContainer(azContainer : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  81. end;
  82. implementation
  83. constructor TQuickAzure.Create;
  84. begin
  85. inherited;
  86. fconAzure := TAzureConnectionInfo.Create(nil);
  87. fAzureProtocol := azHTTP;
  88. fTimeOut := 30;
  89. end;
  90. constructor TQuickAzure.Create(azAccountName, azAccountKey : string);
  91. begin
  92. Create;
  93. SetAccountName(azAccountName);
  94. SetAccountKey(azAccountKey);
  95. end;
  96. destructor TQuickAzure.Destroy;
  97. begin
  98. if Assigned(fconAzure) then fconAzure.Free;
  99. inherited;
  100. end;
  101. procedure TQuickAzure.SetAccountName(azAccountName : string);
  102. begin
  103. if fAccountName <> azAccountName then
  104. begin
  105. fAccountName := azAccountName;
  106. fconAzure.AccountName := azAccountName;
  107. end;
  108. end;
  109. procedure TQuickAzure.SetAccountKey(azAccountKey : string);
  110. begin
  111. if fAccountKey <> azAccountKey then
  112. begin
  113. fAccountKey := azAccountKey ;
  114. fconAzure.AccountKey := azAccountKey;
  115. end;
  116. end;
  117. procedure TQuickAzure.SetAzureProtocol(azProtocol: TAzureProtocol);
  118. begin
  119. if fAzureProtocol <> azProtocol then
  120. begin
  121. fAzureProtocol := azProtocol;
  122. if azProtocol = azHTTP then fconAzure.Protocol := 'HTTP'
  123. else fconAzure.Protocol := 'HTTPS';
  124. end;
  125. end;
  126. function TQuickAzure.FileToArray(cFilename : string) : TArray<Byte>;
  127. var
  128. fs : TFileStream;
  129. bs : TBytesStream;
  130. begin
  131. fs := TFileStream.Create(cFilename, fmOpenRead);
  132. try
  133. bs := TBytesStream.Create(Result);
  134. try
  135. bs.LoadFromStream(fs);
  136. Result := bs.Bytes;
  137. finally
  138. bs.Free
  139. end;
  140. finally
  141. fs.Free;
  142. end;
  143. end;
  144. function TQuickAzure.StreamToArray(cStream : TStream) : TArray<Byte>;
  145. var
  146. bs : TBytesStream;
  147. begin
  148. bs := TBytesStream.Create(Result);
  149. try
  150. bs.LoadFromStream(cStream);
  151. Result := bs.Bytes;
  152. finally
  153. bs.Free
  154. end;
  155. end;
  156. function TQuickAzure.GMT2DateTime(const gmtdate : string):TDateTime;
  157. function GetMonthDig(Value : string):Integer;
  158. const
  159. aMonth : array[1..12] of string = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  160. var
  161. idx : Integer;
  162. begin
  163. Result := 0;
  164. for idx := 1 to 12 do
  165. begin
  166. if CompareText(Value,aMonth[idx]) = 0 then
  167. begin
  168. Result := idx;
  169. Break;
  170. end;
  171. end;
  172. end;
  173. var
  174. i : Integer;
  175. Len : Integer;
  176. wDay, wMonth, wYear,
  177. wHour, wMinute, wSec : Word;
  178. begin
  179. //GMT Format: 'Mon, 12 Jan 2014 16:20:35 GMT'
  180. Result := 0;
  181. Len := 0;
  182. if gmtdate = '' then Exit;
  183. try
  184. for i := 0 to Length(gmtdate) do
  185. begin
  186. if gmtdate[i] in ['0'..'9'] then
  187. begin
  188. Len := i;
  189. Break;
  190. end;
  191. end;
  192. //Day
  193. wDay := StrToIntDef(Copy(gmtdate,Len,2),0);
  194. if wDay = 0 then Exit;
  195. Inc(Len,3);
  196. //Month
  197. wMonth := GetMonthDig(Copy(gmtdate,Len,3));
  198. if wMonth = 0 then Exit;
  199. Inc(Len,4);
  200. //Year
  201. wYear := StrToIntDef(Copy(gmtdate,Len,4),0);
  202. if wYear = 0 then Exit;
  203. Inc(Len,5);
  204. //Hour
  205. wHour := StrToIntDef(Copy(gmtdate,Len,2),99);
  206. if wHour = 99 then Exit;
  207. Inc(Len,3);
  208. //Min
  209. wMinute := StrToIntDef(Copy(gmtdate,Len,2),99);
  210. if wMinute = 99 then Exit;
  211. Inc(Len,3);
  212. //Sec
  213. wSec := StrToIntDef(Copy(gmtdate,Len,2),99);
  214. if wSec = 99 then Exit;
  215. Result := EncodeDate(wYear,wMonth,wDay) + EncodeTime(wHour,wMinute,wSec,0);
  216. except
  217. Result := 0;
  218. end;
  219. end;
  220. function GetResponseInfo(ResponseInfo : TCloudResponseInfo) : TAzureResponseInfo;
  221. begin
  222. Result.StatusCode := ResponseInfo.StatusCode;
  223. Result.StatusMsg := ResponseInfo.StatusMessage;
  224. end;
  225. function TQuickAzure.PutBlob(azContainer, cFilename, azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  226. var
  227. BlobService : TAzureBlobService;
  228. Content : TArray<Byte>;
  229. CloudResponseInfo : TCloudResponseInfo;
  230. begin
  231. Result := False;
  232. BlobService := TAzureBlobService.Create(fconAzure);
  233. if azContainer = '' then azContainer := '$root';
  234. CloudResponseInfo := TCloudResponseInfo.Create;
  235. try
  236. BlobService.Timeout := fTimeout;
  237. Content := FileToArray(cFilename);
  238. if azBlobName = '' then azBlobName := cFilename;
  239. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  240. Result := BlobService.PutBlockBlob(azContainer,azBlobName,Content,EmptyStr,nil,nil,CloudResponseInfo);
  241. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  242. finally
  243. BlobService.Free;
  244. CloudResponseInfo.Free;
  245. end;
  246. end;
  247. function TQuickAzure.PutBlob(azContainer : string; cStream : TStream; azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  248. var
  249. BlobService : TAzureBlobService;
  250. Content : TArray<Byte>;
  251. CloudResponseInfo : TCloudResponseInfo;
  252. begin
  253. azResponseInfo.StatusCode := 500;
  254. if cStream.Size = 0 then
  255. begin
  256. azResponseInfo.StatusMsg := 'Stream is empty';
  257. Exit;
  258. end;
  259. if azContainer = '' then azContainer := '$root';
  260. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  261. try
  262. BlobService := TAzureBlobService.Create(fconAzure);
  263. try
  264. BlobService.Timeout := fTimeout;
  265. CloudResponseInfo := TCloudResponseInfo.Create;
  266. Content := StreamToArray(cStream);
  267. Result := BlobService.PutBlockBlob(azContainer,azBlobName,Content,EmptyStr,nil,nil,CloudResponseInfo);
  268. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  269. finally
  270. BlobService.Free;
  271. CloudResponseInfo.Free;
  272. end;
  273. except
  274. on E : Exception do
  275. begin
  276. azResponseInfo.StatusCode := 500;
  277. azResponseInfo.StatusMsg := e.message;
  278. Result := False;
  279. end;
  280. end;
  281. end;
  282. function TQuickAzure.GetBlob(azContainer, azBlobName, cFilenameTo : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  283. var
  284. BlobService : TAzureBlobService;
  285. fs : TFileStream;
  286. CloudResponseInfo : TCloudResponseInfo;
  287. begin
  288. Result := False;
  289. if azContainer = '' then azContainer := '$root';
  290. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  291. BlobService := TAzureBlobService.Create(fconAzure);
  292. try
  293. BlobService.Timeout := fTimeout;
  294. CloudResponseInfo := TCloudResponseInfo.Create;
  295. fs := TFileStream.Create(cFilenameTo,fmCreate);
  296. try
  297. try
  298. Result := BlobService.GetBlob(azContainer,azBlobName,fs,EmptyStr,CloudResponseInfo);
  299. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  300. except
  301. Result := False;
  302. end;
  303. finally
  304. fs.Free;
  305. end;
  306. finally
  307. BlobService.Free;
  308. CloudResponseInfo.Free;
  309. end;
  310. end;
  311. function TQuickAzure.GetBlob(azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo; out Stream : TMemoryStream) : Boolean;
  312. var
  313. BlobService : TAzureBlobService;
  314. CloudResponseInfo : TCloudResponseInfo;
  315. begin
  316. Result := False;
  317. Stream := TMemoryStream.Create;
  318. if azContainer = '' then azContainer := '$root';
  319. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  320. BlobService := TAzureBlobService.Create(fconAzure);
  321. try
  322. BlobService.Timeout := fTimeout;
  323. CloudResponseInfo := TCloudResponseInfo.Create;
  324. try
  325. Result := BlobService.GetBlob(azContainer,azBlobName,Stream,EmptyStr,CloudResponseInfo);
  326. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  327. except
  328. Stream := nil;
  329. end;
  330. finally
  331. BlobService.Free;
  332. CloudResponseInfo.Free;
  333. end;
  334. end;
  335. function TQuickAzure.GetBlob(const azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo) : TMemoryStream;
  336. begin
  337. GetBlob(azContainer,azBlobName,azResponseInfo,Result);
  338. end;
  339. function TQuickAzure.CopyBlob(azSourceContainer, azSourceBlobName : string; azTargetContainer, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  340. var
  341. BlobService : TAzureBlobService;
  342. CloudResponseInfo : TCloudResponseInfo;
  343. begin
  344. Result := False;
  345. if azSourceContainer = '' then azSourceContainer := '$root';
  346. if azTargetContainer = '' then azTargetContainer := '$root';
  347. if azSourceBlobName.StartsWith('/') then azSourceBlobName := Copy(azSourceBlobName,2,Length(azSourceBlobName));
  348. if azTargetBlobName.StartsWith('/') then azTargetBlobName := Copy(azTargetBlobName,2,Length(azTargetBlobName));
  349. BlobService := TAzureBlobService.Create(fconAzure);
  350. try
  351. BlobService.Timeout := fTimeout;
  352. CloudResponseInfo := TCloudResponseInfo.Create;
  353. try
  354. Result := BlobService.CopyBlob(azTargetContainer,azTargetBlobName,azSourceContainer,azSourceBlobName,'',nil,CloudResponseInfo);
  355. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  356. except
  357. on E : Exception do
  358. begin
  359. Result := False;
  360. azResponseInfo.StatusCode := 500;
  361. azResponseInfo.StatusMsg := e.message;
  362. end;
  363. end;
  364. finally
  365. BlobService.Free;
  366. CloudResponseInfo.Free;
  367. end;
  368. end;
  369. function TQuickAzure.RenameBlob(const azContainer, azSourceBlobName, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  370. var
  371. ResponseInfo : TAzureResponseInfo;
  372. begin
  373. Result := False;
  374. if CopyBlob(azContainer,azSourceBlobName,azContainer,azTargetBlobName,ResponseInfo) then
  375. begin
  376. Result := DeleteBlob(azContainer,azSourceBlobName,ResponseInfo);
  377. end;
  378. end;
  379. function TQuickAzure.ExistsObject(const azContainer, azBlobName : string) : Boolean;
  380. var
  381. azBlob : string;
  382. azBlobs : TStrings;
  383. ResponseInfo : TAzureResponseInfo;
  384. begin
  385. Result := False;
  386. azBlobs := ListBlobsNames(azContainer,azBlobName,True,ResponseInfo);
  387. try
  388. if (ResponseInfo.StatusCode = 200) and (Assigned(azBlobs)) then
  389. begin
  390. for azBlob in azBlobs do
  391. begin
  392. if azBlob = azBlobName then
  393. begin
  394. Result := True;
  395. Break;
  396. end;
  397. end;
  398. end;
  399. finally
  400. azBlobs.Free;
  401. end;
  402. end;
  403. function TQuickAzure.ExistsFolder(azContainer : string; azFolderName : string) : Boolean;
  404. var
  405. BlobService : TAzureBlobService;
  406. azBlob : TAzureBlob;
  407. azBlobList : TList<TAzureBlob>;
  408. CloudResponseInfo : TCloudResponseInfo;
  409. AzParams : TStrings;
  410. cNextMarker : string;
  411. begin
  412. Result := False;
  413. if azContainer = '' then azContainer := '$root';
  414. BlobService := TAzureBlobService.Create(fconAzure);
  415. CloudResponseInfo := TCloudResponseInfo.Create;
  416. try
  417. BlobService.Timeout := fTimeout;
  418. AzParams := TStringList.Create;
  419. try
  420. if not azFolderName.EndsWith('/') then azFolderName := azFolderName + '/';
  421. AzParams.Values['prefix'] := azFolderName;
  422. AzParams.Values['delimiter'] := '/';
  423. AzParams.Values['maxresults'] := '1';
  424. cNextMarker := '';
  425. azBlobList := BlobService.ListBlobs(azContainer,cNextMarker,AzParams,CloudResponseInfo);
  426. try
  427. if (Assigned(azBlobList)) and (azBlobList.Count > 0) and (CloudResponseInfo.StatusCode = 200) then Result := True;
  428. finally
  429. azBlobList.Free;
  430. end;
  431. finally
  432. AzParams.Free;
  433. end;
  434. finally
  435. BlobService.Free;
  436. CloudResponseInfo.Free;
  437. end;
  438. end;
  439. function TQuickAzure.DeleteBlob(azContainer,azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  440. var
  441. BlobService : TAzureBlobService;
  442. CloudResponseInfo : TCloudResponseInfo;
  443. begin
  444. Result := False;
  445. if azContainer = '' then azContainer := '$root';
  446. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  447. BlobService := TAzureBlobService.Create(fconAzure);
  448. try
  449. BlobService.Timeout := fTimeout;
  450. CloudResponseInfo := TCloudResponseInfo.Create;
  451. Result := BlobService.DeleteBlob(azContainer,azBlobName,False,EmptyStr,CloudResponseInfo);
  452. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  453. finally
  454. BlobService.Free;
  455. CloudResponseInfo.Free;
  456. end;
  457. end;
  458. function TQuickAzure.ListBlobs(azContainer : string; azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TBlobList;
  459. var
  460. BlobService : TAzureBlobService;
  461. azBlob : TAzureBlob;
  462. azBlobList : TList<TAzureBlob>;
  463. Blob : TAzureBlobObject;
  464. CloudResponseInfo : TCloudResponseInfo;
  465. cNextMarker : string;
  466. AzParams : TStrings;
  467. begin
  468. Result := TBlobList.Create(True);
  469. cNextMarker := '';
  470. if azContainer = '' then azContainer := '$root';
  471. BlobService := TAzureBlobService.Create(fconAzure);
  472. CloudResponseInfo := TCloudResponseInfo.Create;
  473. try
  474. BlobService.Timeout := fTimeout;
  475. repeat
  476. AzParams := TStringList.Create;
  477. try
  478. AzParams.Values['prefix'] := azBlobsStartWith;
  479. if not Recursive then AzParams.Values['delimiter'] := '/';
  480. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  481. azBlobList := BlobService.ListBlobs(azContainer,cNextMarker,AzParams,CloudResponseInfo);
  482. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  483. if Assigned(azBlobList) then
  484. begin
  485. try
  486. for azBlob in azBlobList do
  487. begin
  488. Blob := TAzureBlobObject.Create;
  489. Blob.Name := azBlob.Name;
  490. Blob.Size := StrToInt64Def(azBlob.Properties.Values['Content-Length'],0);
  491. Blob.LastModified := GMT2DateTime(azBlob.Properties.Values['Last-Modified']);
  492. Result.Add(Blob);
  493. end;
  494. finally
  495. //frees azbloblist objects
  496. for azBlob in azBlobList do azBlob.Free;
  497. azBlobList.Free;
  498. end;
  499. end;
  500. finally
  501. AzParams.Free;
  502. end;
  503. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  504. finally
  505. BlobService.Free;
  506. CloudResponseInfo.Free;
  507. end;
  508. end;
  509. function TQuickAzure.ListBlobsNames(azContainer : string; azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TStrings;
  510. var
  511. BlobService : TAzureBlobService;
  512. azBlob : TAzureBlob;
  513. azBlobList : TList<TAzureBlob>;
  514. CloudResponseInfo : TCloudResponseInfo;
  515. cNextMarker : string;
  516. AzParams : TStrings;
  517. begin
  518. Result := TStringList.Create;
  519. cNextMarker := '';
  520. if azContainer = '' then azContainer := '$root';
  521. BlobService := TAzureBlobService.Create(fconAzure);
  522. CloudResponseInfo := TCloudResponseInfo.Create;
  523. try
  524. BlobService.Timeout := fTimeout;
  525. repeat
  526. AzParams := TStringList.Create;
  527. try
  528. AzParams.Values['prefix'] := azBlobsStartWith;
  529. if not Recursive then AzParams.Values['delimiter'] := '/';
  530. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  531. azBlobList := BlobService.ListBlobs(azContainer,cNextMarker,AzParams,CloudResponseInfo);
  532. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  533. if Assigned(azBlobList) then
  534. begin
  535. Result.BeginUpdate;
  536. try
  537. for azBlob in azBlobList do Result.Add(azBlob.Name);
  538. finally
  539. Result.EndUpdate;
  540. //frees bloblist objects
  541. for azBlob in azBlobList do azBlob.Free;
  542. azBlobList.Free;
  543. end;
  544. end;
  545. finally
  546. AzParams.Free;
  547. end;
  548. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  549. finally
  550. BlobService.Free;
  551. CloudResponseInfo.Free;
  552. end;
  553. end;
  554. function TQuickAzure.ExistsContainer(azContainer : string) : Boolean;
  555. var
  556. Container : string;
  557. Containers : TStrings;
  558. ResponseInfo : TAzureResponseInfo;
  559. begin
  560. Result := False;
  561. Containers := ListContainers(azContainer,ResponseInfo);
  562. try
  563. if (ResponseInfo.StatusCode = 200) and (Assigned(Containers)) then
  564. begin
  565. for Container in Containers do
  566. begin
  567. if Container = azContainer then
  568. begin
  569. Result := True;
  570. Break;
  571. end;
  572. end;
  573. end;
  574. finally
  575. Containers.Free;
  576. end;
  577. end;
  578. function TQuickAzure.ListContainers(azContainersStartWith : string; azResponseInfo : TAzureResponseInfo) : TStrings;
  579. var
  580. BlobService : TAzureBlobService;
  581. CloudResponseInfo : TCloudResponseInfo;
  582. cNextMarker : string;
  583. AzParams : TStrings;
  584. AzContainer : TAzureContainer;
  585. AzContainers : TList<TAzureContainer>;
  586. begin
  587. Result := TStringList.Create;
  588. cNextMarker := '';
  589. BlobService := TAzureBlobService.Create(fconAzure);
  590. CloudResponseInfo := TCloudResponseInfo.Create;
  591. try
  592. BlobService.Timeout := fTimeout;
  593. repeat
  594. AzParams := TStringList.Create;
  595. try
  596. if azContainersStartWith <> '' then AzParams.Values['prefix'] := azContainersStartWith;
  597. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  598. AzContainers := BlobService.ListContainers(cNextMarker,AzParams,CloudResponseInfo);
  599. try
  600. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  601. if (azResponseInfo.StatusCode = 200) and (Assigned(AzContainer)) then
  602. begin
  603. for AzContainer in AzContainers do
  604. begin
  605. Result.Add(AzContainer.Name);
  606. end;
  607. end;
  608. finally
  609. if Assigned(AzContainer) then
  610. begin
  611. //frees ContainerList objects
  612. for AzContainer in AzContainers do AzContainer.Free;
  613. AzContainers.Free;
  614. end;
  615. end;
  616. finally
  617. AzParams.Free;
  618. end;
  619. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  620. finally
  621. BlobService.Free;
  622. CloudResponseInfo.Free;
  623. end;
  624. end;
  625. function TQuickAzure.CreateContainer(azContainer : string; azPublicAccess : TBlobPublicAccess; out azResponseInfo : TAzureResponseInfo) : Boolean;
  626. var
  627. BlobService : TAzureBlobService;
  628. CloudResponseInfo : TCloudResponseInfo;
  629. begin
  630. Result := False;
  631. if azContainer = '' then Exit;
  632. BlobService := TAzureBlobService.Create(fconAzure);
  633. try
  634. BlobService.Timeout := fTimeout;
  635. CloudResponseInfo := TCloudResponseInfo.Create;
  636. Result := BlobService.CreateContainer(azContainer,nil,azPublicAccess,CloudResponseInfo);
  637. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  638. finally
  639. BlobService.Free;
  640. CloudResponseInfo.Free;
  641. end;
  642. end;
  643. function TQuickAzure.DeleteContainer(azContainer : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  644. var
  645. BlobService : TAzureBlobService;
  646. CloudResponseInfo : TCloudResponseInfo;
  647. begin
  648. Result := False;
  649. if azContainer = '' then Exit;
  650. BlobService := TAzureBlobService.Create(fconAzure);
  651. try
  652. BlobService.Timeout := fTimeout;
  653. CloudResponseInfo := TCloudResponseInfo.Create;
  654. Result := BlobService.DeleteContainer(azContainer,CloudResponseInfo);
  655. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  656. finally
  657. BlobService.Free;
  658. CloudResponseInfo.Free;
  659. end;
  660. end;
  661. end.