Quick.Azure.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. azBlobList.Free;
  446. end;
  447. finally
  448. CloudResponseInfo.Free;
  449. end;
  450. finally
  451. AzParams.Free;
  452. end;
  453. finally
  454. BlobService.Free;
  455. end;
  456. end;
  457. function TQuickAzure.DeleteBlob(azContainer,azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  458. var
  459. BlobService : TAzureBlobService;
  460. CloudResponseInfo : TCloudResponseInfo;
  461. begin
  462. Result := False;
  463. if azContainer = '' then azContainer := '$root';
  464. if azBlobName.StartsWith('/') then azBlobName := Copy(azBlobName,2,Length(azBlobName));
  465. BlobService := TAzureBlobService.Create(fconAzure);
  466. try
  467. BlobService.Timeout := fTimeout;
  468. CloudResponseInfo := TCloudResponseInfo.Create;
  469. try
  470. Result := BlobService.DeleteBlob(azContainer,azBlobName,False,EmptyStr,CloudResponseInfo);
  471. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  472. finally
  473. CloudResponseInfo.Free;
  474. end;
  475. finally
  476. BlobService.Free;
  477. end;
  478. end;
  479. function TQuickAzure.ListBlobs(azContainer : string; azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TBlobList;
  480. var
  481. BlobService : TAzureBlobService;
  482. azBlob : TAzureBlob;
  483. azBlobList : TList<TAzureBlob>;
  484. Blob : TAzureBlobObject;
  485. CloudResponseInfo : TCloudResponseInfo;
  486. cNextMarker : string;
  487. AzParams : TStrings;
  488. begin
  489. Result := TBlobList.Create(True);
  490. cNextMarker := '';
  491. if azContainer = '' then azContainer := '$root';
  492. BlobService := TAzureBlobService.Create(fconAzure);
  493. try
  494. BlobService.Timeout := fTimeout;
  495. repeat
  496. AzParams := TStringList.Create;
  497. try
  498. AzParams.Values['prefix'] := azBlobsStartWith;
  499. if not Recursive then AzParams.Values['delimiter'] := '/';
  500. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  501. CloudResponseInfo := TCloudResponseInfo.Create;
  502. try
  503. azBlobList := BlobService.ListBlobs(azContainer,cNextMarker,AzParams,CloudResponseInfo);
  504. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  505. if Assigned(azBlobList) then
  506. begin
  507. try
  508. for azBlob in azBlobList do
  509. begin
  510. Blob := TAzureBlobObject.Create;
  511. Blob.Name := azBlob.Name;
  512. Blob.Size := StrToInt64Def(azBlob.Properties.Values['Content-Length'],0);
  513. Blob.LastModified := GMT2DateTime(azBlob.Properties.Values['Last-Modified']);
  514. Result.Add(Blob);
  515. end;
  516. finally
  517. //frees azbloblist objects
  518. for azBlob in azBlobList do azBlob.Free;
  519. azBlobList.Free;
  520. end;
  521. end;
  522. finally
  523. CloudResponseInfo.Free;
  524. end;
  525. finally
  526. FreeAndNil(AzParams);
  527. end;
  528. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  529. finally
  530. BlobService.Free;
  531. end;
  532. end;
  533. function TQuickAzure.ListBlobsNames(azContainer : string; azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TStrings;
  534. var
  535. BlobService : TAzureBlobService;
  536. azBlob : TAzureBlob;
  537. azBlobList : TList<TAzureBlob>;
  538. CloudResponseInfo : TCloudResponseInfo;
  539. cNextMarker : string;
  540. AzParams : TStrings;
  541. begin
  542. Result := TStringList.Create;
  543. cNextMarker := '';
  544. if azContainer = '' then azContainer := '$root';
  545. BlobService := TAzureBlobService.Create(fconAzure);
  546. CloudResponseInfo := TCloudResponseInfo.Create;
  547. try
  548. BlobService.Timeout := fTimeout;
  549. repeat
  550. AzParams := TStringList.Create;
  551. try
  552. AzParams.Values['prefix'] := azBlobsStartWith;
  553. if not Recursive then AzParams.Values['delimiter'] := '/';
  554. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  555. azBlobList := BlobService.ListBlobs(azContainer,cNextMarker,AzParams,CloudResponseInfo);
  556. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  557. if Assigned(azBlobList) then
  558. begin
  559. Result.BeginUpdate;
  560. try
  561. for azBlob in azBlobList do Result.Add(azBlob.Name);
  562. finally
  563. Result.EndUpdate;
  564. //frees bloblist objects
  565. for azBlob in azBlobList do azBlob.Free;
  566. azBlobList.Free;
  567. end;
  568. end;
  569. finally
  570. AzParams.Free;
  571. end;
  572. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  573. finally
  574. BlobService.Free;
  575. CloudResponseInfo.Free;
  576. end;
  577. end;
  578. function TQuickAzure.ExistsContainer(azContainer : string) : Boolean;
  579. var
  580. Container : string;
  581. Containers : TStrings;
  582. ResponseInfo : TAzureResponseInfo;
  583. begin
  584. Result := False;
  585. Containers := ListContainers(azContainer,ResponseInfo);
  586. try
  587. if (ResponseInfo.StatusCode = 200) and (Assigned(Containers)) then
  588. begin
  589. for Container in Containers do
  590. begin
  591. if Container = azContainer then
  592. begin
  593. Result := True;
  594. Break;
  595. end;
  596. end;
  597. end;
  598. finally
  599. Containers.Free;
  600. end;
  601. end;
  602. function TQuickAzure.ListContainers(azContainersStartWith : string; azResponseInfo : TAzureResponseInfo) : TStrings;
  603. var
  604. BlobService : TAzureBlobService;
  605. CloudResponseInfo : TCloudResponseInfo;
  606. cNextMarker : string;
  607. AzParams : TStrings;
  608. AzContainer : TAzureContainer;
  609. AzContainers : TList<TAzureContainer>;
  610. begin
  611. Result := TStringList.Create;
  612. cNextMarker := '';
  613. BlobService := TAzureBlobService.Create(fconAzure);
  614. CloudResponseInfo := TCloudResponseInfo.Create;
  615. try
  616. BlobService.Timeout := fTimeout;
  617. repeat
  618. AzParams := TStringList.Create;
  619. try
  620. if azContainersStartWith <> '' then AzParams.Values['prefix'] := azContainersStartWith;
  621. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  622. AzContainers := BlobService.ListContainers(cNextMarker,AzParams,CloudResponseInfo);
  623. try
  624. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  625. if (azResponseInfo.StatusCode = 200) and (Assigned(AzContainer)) then
  626. begin
  627. for AzContainer in AzContainers do
  628. begin
  629. Result.Add(AzContainer.Name);
  630. end;
  631. end;
  632. finally
  633. if Assigned(AzContainer) then
  634. begin
  635. //frees ContainerList objects
  636. for AzContainer in AzContainers do AzContainer.Free;
  637. AzContainers.Free;
  638. end;
  639. end;
  640. finally
  641. AzParams.Free;
  642. end;
  643. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  644. finally
  645. BlobService.Free;
  646. CloudResponseInfo.Free;
  647. end;
  648. end;
  649. function TQuickAzure.CreateContainer(azContainer : string; azPublicAccess : TBlobPublicAccess; out azResponseInfo : TAzureResponseInfo) : Boolean;
  650. var
  651. BlobService : TAzureBlobService;
  652. CloudResponseInfo : TCloudResponseInfo;
  653. begin
  654. Result := False;
  655. if azContainer = '' then Exit;
  656. BlobService := TAzureBlobService.Create(fconAzure);
  657. try
  658. BlobService.Timeout := fTimeout;
  659. CloudResponseInfo := TCloudResponseInfo.Create;
  660. Result := BlobService.CreateContainer(azContainer,nil,azPublicAccess,CloudResponseInfo);
  661. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  662. finally
  663. BlobService.Free;
  664. CloudResponseInfo.Free;
  665. end;
  666. end;
  667. function TQuickAzure.DeleteContainer(azContainer : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
  668. var
  669. BlobService : TAzureBlobService;
  670. CloudResponseInfo : TCloudResponseInfo;
  671. begin
  672. Result := False;
  673. if azContainer = '' then Exit;
  674. BlobService := TAzureBlobService.Create(fconAzure);
  675. try
  676. BlobService.Timeout := fTimeout;
  677. CloudResponseInfo := TCloudResponseInfo.Create;
  678. Result := BlobService.DeleteContainer(azContainer,CloudResponseInfo);
  679. azResponseInfo := GetResponseInfo(CloudResponseInfo);
  680. finally
  681. BlobService.Free;
  682. CloudResponseInfo.Free;
  683. end;
  684. end;
  685. end.