Quick.Azure.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 : 20/10/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. try
  234. if azContainer = '' then azContainer := '$root';
  235. CloudResponseInfo := TCloudResponseInfo.Create;
  236. try
  237. BlobService.Timeout := fTimeout;
  238. Content := FileToArray(cFilename);
  239. if azBlobName = '' then azBlobName := cFilename;
  240. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  241. Result := BlobService.PutBlockBlob(azContainer,azBlobName,Content,EmptyStr,nil,nil,CloudResponseInfo);
  242. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  243. finally
  244. CloudResponseInfo.Free;
  245. end;
  246. finally
  247. BlobService.Free;
  248. end;
  249. end;
  250. function TQuickAzure.PutBlob(azContainer : string; cStream : TStream; azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  251. var
  252. BlobService : TAzureBlobService;
  253. Content : TArray<Byte>;
  254. CloudResponseInfo : TCloudResponseInfo;
  255. begin
  256. azResponseInfo.StatusCode := 500;
  257. if cStream.Size = 0 then
  258. begin
  259. azResponseInfo.StatusMsg := 'Stream is empty';
  260. Exit;
  261. end;
  262. if azContainer = '' then azContainer := '$root';
  263. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  264. try
  265. BlobService := TAzureBlobService.Create(fconAzure);
  266. try
  267. BlobService.Timeout := fTimeout;
  268. CloudResponseInfo := TCloudResponseInfo.Create;
  269. try
  270. Content := StreamToArray(cStream);
  271. Result := BlobService.PutBlockBlob(azContainer,azBlobName,Content,EmptyStr,nil,nil,CloudResponseInfo);
  272. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  273. finally
  274. CloudResponseInfo.Free;
  275. end;
  276. finally
  277. BlobService.Free;
  278. end;
  279. except
  280. on E : Exception do
  281. begin
  282. azResponseInfo.StatusCode := 500;
  283. azResponseInfo.StatusMsg := e.message;
  284. Result := False;
  285. end;
  286. end;
  287. end;
  288. function TQuickAzure.GetBlob(azContainer, azBlobName, cFilenameTo : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  289. var
  290. BlobService : TAzureBlobService;
  291. fs : TFileStream;
  292. CloudResponseInfo : TCloudResponseInfo;
  293. begin
  294. Result := False;
  295. if azContainer = '' then azContainer := '$root';
  296. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  297. BlobService := TAzureBlobService.Create(fconAzure);
  298. try
  299. BlobService.Timeout := fTimeout;
  300. fs := TFileStream.Create(cFilenameTo,fmCreate);
  301. try
  302. try
  303. CloudResponseInfo := TCloudResponseInfo.Create;
  304. try
  305. Result := BlobService.GetBlob(azContainer,azBlobName,fs,EmptyStr,CloudResponseInfo);
  306. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  307. finally
  308. CloudResponseInfo.Free;
  309. end;
  310. except
  311. Result := False;
  312. end;
  313. finally
  314. fs.Free;
  315. end;
  316. finally
  317. BlobService.Free;
  318. end;
  319. end;
  320. function TQuickAzure.GetBlob(azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo; out Stream : TMemoryStream) : Boolean;
  321. var
  322. BlobService : TAzureBlobService;
  323. CloudResponseInfo : TCloudResponseInfo;
  324. begin
  325. Result := False;
  326. Stream := TMemoryStream.Create;
  327. if azContainer = '' then azContainer := '$root';
  328. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  329. BlobService := TAzureBlobService.Create(fconAzure);
  330. try
  331. BlobService.Timeout := fTimeout;
  332. try
  333. CloudResponseInfo := TCloudResponseInfo.Create;
  334. try
  335. Result := BlobService.GetBlob(azContainer,azBlobName,Stream,EmptyStr,CloudResponseInfo);
  336. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  337. finally
  338. CloudResponseInfo.Free;
  339. end;
  340. except
  341. Stream := nil;
  342. end;
  343. finally
  344. BlobService.Free;
  345. end;
  346. end;
  347. function TQuickAzure.GetBlob(const azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo) : TMemoryStream;
  348. begin
  349. GetBlob(azContainer,azBlobName,azResponseInfo,Result);
  350. end;
  351. function TQuickAzure.CopyBlob(azSourceContainer, azSourceBlobName : string; azTargetContainer, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  352. var
  353. BlobService : TAzureBlobService;
  354. CloudResponseInfo : TCloudResponseInfo;
  355. begin
  356. Result := False;
  357. if azSourceContainer = '' then azSourceContainer := '$root';
  358. if azTargetContainer = '' then azTargetContainer := '$root';
  359. if azSourceBlobName.StartsWith('/') then azSourceBlobName := Copy(azSourceBlobName,2,Length(azSourceBlobName));
  360. if azTargetBlobName.StartsWith('/') then azTargetBlobName := Copy(azTargetBlobName,2,Length(azTargetBlobName));
  361. BlobService := TAzureBlobService.Create(fconAzure);
  362. try
  363. BlobService.Timeout := fTimeout;
  364. try
  365. CloudResponseInfo := TCloudResponseInfo.Create;
  366. try
  367. Result := BlobService.CopyBlob(azTargetContainer,azTargetBlobName,azSourceContainer,azSourceBlobName,'',nil,CloudResponseInfo);
  368. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  369. finally
  370. CloudResponseInfo.Free;
  371. end;
  372. except
  373. on E : Exception do
  374. begin
  375. Result := False;
  376. azResponseInfo.StatusCode := 500;
  377. azResponseInfo.StatusMsg := e.message;
  378. end;
  379. end;
  380. finally
  381. BlobService.Free;
  382. end;
  383. end;
  384. function TQuickAzure.RenameBlob(const azContainer, azSourceBlobName, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  385. var
  386. ResponseInfo : TAzureResponseInfo;
  387. begin
  388. Result := False;
  389. if CopyBlob(azContainer,azSourceBlobName,azContainer,azTargetBlobName,ResponseInfo) then
  390. begin
  391. Result := DeleteBlob(azContainer,azSourceBlobName,ResponseInfo);
  392. end;
  393. end;
  394. function TQuickAzure.ExistsObject(const azContainer, azBlobName : string) : Boolean;
  395. var
  396. azBlob : string;
  397. azBlobs : TStrings;
  398. ResponseInfo : TAzureResponseInfo;
  399. begin
  400. Result := False;
  401. azBlobs := ListBlobsNames(azContainer,azBlobName,True,ResponseInfo);
  402. try
  403. if (ResponseInfo.StatusCode = 200) and (Assigned(azBlobs)) then
  404. begin
  405. for azBlob in azBlobs do
  406. begin
  407. if azBlob = azBlobName then
  408. begin
  409. Result := True;
  410. Break;
  411. end;
  412. end;
  413. end;
  414. finally
  415. azBlobs.Free;
  416. end;
  417. end;
  418. function TQuickAzure.ExistsFolder(azContainer : string; azFolderName : string) : Boolean;
  419. var
  420. BlobService : TAzureBlobService;
  421. azBlob : TAzureBlob;
  422. azBlobList : TList<TAzureBlob>;
  423. CloudResponseInfo : TCloudResponseInfo;
  424. AzParams : TStrings;
  425. cNextMarker : string;
  426. begin
  427. Result := False;
  428. if azContainer = '' then azContainer := '$root';
  429. BlobService := TAzureBlobService.Create(fconAzure);
  430. try
  431. BlobService.Timeout := fTimeout;
  432. AzParams := TStringList.Create;
  433. try
  434. if not azFolderName.EndsWith('/') then azFolderName := azFolderName + '/';
  435. AzParams.Values['prefix'] := azFolderName;
  436. AzParams.Values['delimiter'] := '/';
  437. AzParams.Values['maxresults'] := '1';
  438. cNextMarker := '';
  439. CloudResponseInfo := TCloudResponseInfo.Create;
  440. try
  441. azBlobList := BlobService.ListBlobs(azContainer,cNextMarker,AzParams,CloudResponseInfo);
  442. try
  443. if (Assigned(azBlobList)) and (azBlobList.Count > 0) and (CloudResponseInfo.StatusCode = 200) then Result := True;
  444. finally
  445. //frees azbloblist objects
  446. for azBlob in azBlobList do azBlob.Free;
  447. azBlobList.Free;
  448. end;
  449. finally
  450. CloudResponseInfo.Free;
  451. end;
  452. finally
  453. AzParams.Free;
  454. end;
  455. finally
  456. BlobService.Free;
  457. end;
  458. end;
  459. function TQuickAzure.DeleteBlob(azContainer,azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  460. var
  461. BlobService : TAzureBlobService;
  462. CloudResponseInfo : TCloudResponseInfo;
  463. begin
  464. Result := False;
  465. if azContainer = '' then azContainer := '$root';
  466. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  467. BlobService := TAzureBlobService.Create(fconAzure);
  468. try
  469. BlobService.Timeout := fTimeout;
  470. CloudResponseInfo := TCloudResponseInfo.Create;
  471. try
  472. Result := BlobService.DeleteBlob(azContainer,azBlobName,False,EmptyStr,CloudResponseInfo);
  473. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  474. finally
  475. CloudResponseInfo.Free;
  476. end;
  477. finally
  478. BlobService.Free;
  479. end;
  480. end;
  481. function TQuickAzure.ListBlobs(azContainer : string; azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TBlobList;
  482. var
  483. BlobService : TAzureBlobService;
  484. azBlob : TAzureBlob;
  485. azBlobList : TList<TAzureBlob>;
  486. Blob : TAzureBlobObject;
  487. CloudResponseInfo : TCloudResponseInfo;
  488. cNextMarker : string;
  489. AzParams : TStrings;
  490. begin
  491. Result := TBlobList.Create(True);
  492. cNextMarker := '';
  493. if azContainer = '' then azContainer := '$root';
  494. BlobService := TAzureBlobService.Create(fconAzure);
  495. try
  496. BlobService.Timeout := fTimeout;
  497. repeat
  498. AzParams := TStringList.Create;
  499. try
  500. AzParams.Values['prefix'] := azBlobsStartWith;
  501. if not Recursive then AzParams.Values['delimiter'] := '/';
  502. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  503. CloudResponseInfo := TCloudResponseInfo.Create;
  504. try
  505. azBlobList := BlobService.ListBlobs(azContainer,cNextMarker,AzParams,CloudResponseInfo);
  506. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  507. if Assigned(azBlobList) then
  508. begin
  509. try
  510. for azBlob in azBlobList do
  511. begin
  512. Blob := TAzureBlobObject.Create;
  513. Blob.Name := azBlob.Name;
  514. Blob.Size := StrToInt64Def(azBlob.Properties.Values['Content-Length'],0);
  515. Blob.LastModified := GMT2DateTime(azBlob.Properties.Values['Last-Modified']);
  516. Result.Add(Blob);
  517. end;
  518. finally
  519. //frees azbloblist objects
  520. for azBlob in azBlobList do azBlob.Free;
  521. azBlobList.Free;
  522. end;
  523. end;
  524. finally
  525. CloudResponseInfo.Free;
  526. end;
  527. finally
  528. FreeAndNil(AzParams);
  529. end;
  530. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  531. finally
  532. BlobService.Free;
  533. end;
  534. end;
  535. function TQuickAzure.ListBlobsNames(azContainer : string; azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TStrings;
  536. var
  537. BlobService : TAzureBlobService;
  538. azBlob : TAzureBlob;
  539. azBlobList : TList<TAzureBlob>;
  540. CloudResponseInfo : TCloudResponseInfo;
  541. cNextMarker : string;
  542. AzParams : TStrings;
  543. begin
  544. Result := TStringList.Create;
  545. cNextMarker := '';
  546. if azContainer = '' then azContainer := '$root';
  547. BlobService := TAzureBlobService.Create(fconAzure);
  548. CloudResponseInfo := TCloudResponseInfo.Create;
  549. try
  550. BlobService.Timeout := fTimeout;
  551. repeat
  552. AzParams := TStringList.Create;
  553. try
  554. AzParams.Values['prefix'] := azBlobsStartWith;
  555. if not Recursive then AzParams.Values['delimiter'] := '/';
  556. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  557. azBlobList := BlobService.ListBlobs(azContainer,cNextMarker,AzParams,CloudResponseInfo);
  558. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  559. if Assigned(azBlobList) then
  560. begin
  561. Result.BeginUpdate;
  562. try
  563. for azBlob in azBlobList do Result.Add(azBlob.Name);
  564. finally
  565. Result.EndUpdate;
  566. //frees bloblist objects
  567. for azBlob in azBlobList do azBlob.Free;
  568. azBlobList.Free;
  569. end;
  570. end;
  571. finally
  572. AzParams.Free;
  573. end;
  574. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  575. finally
  576. BlobService.Free;
  577. CloudResponseInfo.Free;
  578. end;
  579. end;
  580. function TQuickAzure.ExistsContainer(azContainer : string) : Boolean;
  581. var
  582. Container : string;
  583. Containers : TStrings;
  584. ResponseInfo : TAzureResponseInfo;
  585. begin
  586. Result := False;
  587. Containers := ListContainers(azContainer,ResponseInfo);
  588. try
  589. if (ResponseInfo.StatusCode = 200) and (Assigned(Containers)) then
  590. begin
  591. for Container in Containers do
  592. begin
  593. if Container = azContainer then
  594. begin
  595. Result := True;
  596. Break;
  597. end;
  598. end;
  599. end;
  600. finally
  601. Containers.Free;
  602. end;
  603. end;
  604. function TQuickAzure.ListContainers(azContainersStartWith : string; azResponseInfo : TAzureResponseInfo) : TStrings;
  605. var
  606. BlobService : TAzureBlobService;
  607. CloudResponseInfo : TCloudResponseInfo;
  608. cNextMarker : string;
  609. AzParams : TStrings;
  610. AzContainer : TAzureContainer;
  611. AzContainers : TList<TAzureContainer>;
  612. begin
  613. Result := TStringList.Create;
  614. cNextMarker := '';
  615. BlobService := TAzureBlobService.Create(fconAzure);
  616. CloudResponseInfo := TCloudResponseInfo.Create;
  617. try
  618. BlobService.Timeout := fTimeout;
  619. repeat
  620. AzParams := TStringList.Create;
  621. try
  622. if azContainersStartWith <> '' then AzParams.Values['prefix'] := azContainersStartWith;
  623. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  624. AzContainers := BlobService.ListContainers(cNextMarker,AzParams,CloudResponseInfo);
  625. try
  626. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  627. if (azResponseInfo.StatusCode = 200) and (Assigned(AzContainer)) then
  628. begin
  629. for AzContainer in AzContainers do
  630. begin
  631. Result.Add(AzContainer.Name);
  632. end;
  633. end;
  634. finally
  635. if Assigned(AzContainer) then
  636. begin
  637. //frees ContainerList objects
  638. for AzContainer in AzContainers do AzContainer.Free;
  639. AzContainers.Free;
  640. end;
  641. end;
  642. finally
  643. AzParams.Free;
  644. end;
  645. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  646. finally
  647. BlobService.Free;
  648. CloudResponseInfo.Free;
  649. end;
  650. end;
  651. function TQuickAzure.CreateContainer(azContainer : string; azPublicAccess : TBlobPublicAccess; out azResponseInfo : TAzureResponseInfo) : Boolean;
  652. var
  653. BlobService : TAzureBlobService;
  654. CloudResponseInfo : TCloudResponseInfo;
  655. begin
  656. Result := False;
  657. if azContainer = '' then Exit;
  658. BlobService := TAzureBlobService.Create(fconAzure);
  659. try
  660. BlobService.Timeout := fTimeout;
  661. CloudResponseInfo := TCloudResponseInfo.Create;
  662. Result := BlobService.CreateContainer(azContainer,nil,azPublicAccess,CloudResponseInfo);
  663. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  664. finally
  665. BlobService.Free;
  666. CloudResponseInfo.Free;
  667. end;
  668. end;
  669. function TQuickAzure.DeleteContainer(azContainer : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  670. var
  671. BlobService : TAzureBlobService;
  672. CloudResponseInfo : TCloudResponseInfo;
  673. begin
  674. Result := False;
  675. if azContainer = '' then Exit;
  676. BlobService := TAzureBlobService.Create(fconAzure);
  677. try
  678. BlobService.Timeout := fTimeout;
  679. CloudResponseInfo := TCloudResponseInfo.Create;
  680. Result := BlobService.DeleteContainer(azContainer,CloudResponseInfo);
  681. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  682. finally
  683. BlobService.Free;
  684. CloudResponseInfo.Free;
  685. end;
  686. end;
  687. end.