Quick.CloudStorage.Provider.Azure.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. unit Quick.CloudStorage.Provider.Azure;
  2. interface
  3. uses
  4. Classes,
  5. System.SysUtils,
  6. System.Generics.Collections,
  7. Quick.Commons,
  8. Quick.CloudStorage,
  9. IPPeerClient,
  10. Data.Cloud.CloudAPI,
  11. Data.Cloud.AzureAPI;
  12. type
  13. TCloudStorageAzureProvider = class(TCloudStorageProvider)
  14. private
  15. fAzureConnection : TAzureConnectionInfo;
  16. fAzureID : string;
  17. fAzureKey : string;
  18. procedure SetSecure(aValue: Boolean); override;
  19. function ListContainers(azContainersStartWith : string; azResponseInfo : TResponseInfo) : TStrings;
  20. public
  21. constructor Create; overload; override;
  22. constructor Create(const aAccountName, aAccountKey : string); overload;
  23. destructor Destroy; override;
  24. function GetRootFolders : TStrings; override;
  25. procedure OpenDir(const aPath : string); override;
  26. function GetFile(const aPath: string; out stream : TStream) : Boolean; override;
  27. function GetURL(const aPath : string) : string; override;
  28. end;
  29. implementation
  30. { TCloudExplorerProvider }
  31. constructor TCloudStorageAzureProvider.Create;
  32. begin
  33. fAzureConnection := TAzureConnectionInfo.Create(nil);
  34. end;
  35. constructor TCloudStorageAzureProvider.Create(const aAccountName, aAccountKey : string);
  36. begin
  37. inherited Create;
  38. Create;
  39. fAzureID := aAccountName;
  40. fAzureKey := aAccountKey;
  41. fAzureConnection.AccountName := aAccountName;
  42. fAzureConnection.AccountKey := aAccountKey;
  43. end;
  44. destructor TCloudStorageAzureProvider.Destroy;
  45. begin
  46. if Assigned(fAzureConnection) then fAzureConnection.Free;
  47. inherited;
  48. end;
  49. function TCloudStorageAzureProvider.GetFile(const aPath: string; out stream : TStream) : Boolean;
  50. var
  51. BlobService : TAzureBlobService;
  52. CloudResponseInfo : TCloudResponseInfo;
  53. begin
  54. BlobService := TAzureBlobService.Create(fAzureConnection);
  55. try
  56. CloudResponseInfo := TCloudResponseInfo.Create;
  57. try
  58. Result := BlobService.GetBlob(RootFolder,aPath,stream,'',CloudResponseInfo);
  59. if not Result then raise Exception.CreateFmt('Cloud error %d : %s',[CloudResponseInfo.StatusCode,CloudResponseInfo.StatusMessage]);
  60. finally
  61. CloudResponseInfo.Free;
  62. end;
  63. finally
  64. BlobService.Free;
  65. end;
  66. end;
  67. function TCloudStorageAzureProvider.GetRootFolders: TStrings;
  68. var
  69. respinfo : TResponseInfo;
  70. begin
  71. Result := ListContainers('',respinfo);
  72. end;
  73. function TCloudStorageAzureProvider.GetURL(const aPath: string): string;
  74. begin
  75. Result := Format('https://%s.blob.core.windows.net/%s/%s',[fAzureConnection.AccountName,RootFolder,aPath]);
  76. end;
  77. function TCloudStorageAzureProvider.ListContainers(azContainersStartWith : string; azResponseInfo : TResponseInfo) : TStrings;
  78. var
  79. BlobService : TAzureBlobService;
  80. CloudResponseInfo : TCloudResponseInfo;
  81. cNextMarker : string;
  82. AzParams : TStrings;
  83. AzContainer : TAzureContainer;
  84. AzContainers : TList<TAzureContainer>;
  85. begin
  86. Result := TStringList.Create;
  87. cNextMarker := '';
  88. BlobService := TAzureBlobService.Create(fAzureConnection);
  89. CloudResponseInfo := TCloudResponseInfo.Create;
  90. try
  91. BlobService.Timeout := Timeout;
  92. repeat
  93. AzParams := TStringList.Create;
  94. try
  95. if azContainersStartWith <> '' then AzParams.Values['prefix'] := azContainersStartWith;
  96. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  97. AzContainers := BlobService.ListContainers(cNextMarker,AzParams,CloudResponseInfo);
  98. try
  99. azResponseInfo.Get(CloudResponseInfo);
  100. if (azResponseInfo.StatusCode = 200) and (Assigned(AzContainers)) then
  101. begin
  102. for AzContainer in AzContainers do
  103. begin
  104. Result.Add(AzContainer.Name);
  105. end;
  106. end;
  107. finally
  108. if Assigned(AzContainer) then
  109. begin
  110. //frees ContainerList objects
  111. for AzContainer in AzContainers do AzContainer.Free;
  112. AzContainers.Free;
  113. end;
  114. end;
  115. finally
  116. AzParams.Free;
  117. end;
  118. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  119. finally
  120. BlobService.Free;
  121. CloudResponseInfo.Free;
  122. end;
  123. end;
  124. procedure TCloudStorageAzureProvider.OpenDir(const aPath: string);
  125. var
  126. BlobService : TAzureBlobService;
  127. azBlob : TAzureBlob;
  128. azBlobList : TList<TAzureBlob>;
  129. DirItem : TCloudItem;
  130. CloudResponseInfo : TCloudResponseInfo;
  131. cNextMarker : string;
  132. AzParams : TStrings;
  133. azResponseInfo : TResponseInfo;
  134. azContainer : string;
  135. begin
  136. Status := stSearching;
  137. cNextMarker := '';
  138. if aPath = '..' then
  139. begin
  140. CurrentPath := RemoveLastPathSegment(CurrentPath);
  141. end
  142. else
  143. begin
  144. if (CurrentPath = '') or (aPath.StartsWith('/')) then CurrentPath := aPath
  145. else CurrentPath := CurrentPath + aPath;
  146. end;
  147. if Assigned(OnBeginReadDir) then OnBeginReadDir(CurrentPath);
  148. if CurrentPath.StartsWith('/') then CurrentPath := Copy(CurrentPath,2,CurrentPath.Length);
  149. if (not CurrentPath.IsEmpty) and (not CurrentPath.EndsWith('/')) then CurrentPath := CurrentPath + '/';
  150. azContainer := RootFolder;
  151. if azContainer = '' then azContainer := '$root';
  152. BlobService := TAzureBlobService.Create(fAzureConnection);
  153. try
  154. BlobService.Timeout := Timeout;
  155. Status := stRetrieving;
  156. if Assigned(OnGetListItem) then
  157. begin
  158. DirItem := TCloudItem.Create;
  159. try
  160. DirItem.Name := '..';
  161. DirItem.IsDir := True;
  162. DirItem.Date := 0;
  163. OnGetListItem(DirItem);
  164. finally
  165. DirItem.Free;
  166. end;
  167. end;
  168. repeat
  169. if not (Status in [stSearching,stRetrieving]) then Exit;
  170. AzParams := TStringList.Create;
  171. try
  172. if fCancelOperation then
  173. begin
  174. fCancelOperation := False;
  175. Exit;
  176. end;
  177. AzParams.Values['prefix'] := CurrentPath;
  178. //if not Recursive then
  179. AzParams.Values['delimiter'] := '/';
  180. AzParams.Values['maxresults'] := '100';
  181. if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
  182. CloudResponseInfo := TCloudResponseInfo.Create;
  183. try
  184. azBlobList := BlobService.ListBlobs(azContainer,cNextMarker,AzParams,CloudResponseInfo);
  185. azResponseInfo.Get(CloudResponseInfo);
  186. if azResponseInfo.StatusCode = 200 then
  187. begin
  188. try
  189. for azBlob in azBlobList do
  190. begin
  191. if not (Status in [stSearching,stRetrieving]) then Exit;
  192. if fCancelOperation then
  193. begin
  194. fCancelOperation := False;
  195. Exit;
  196. end;
  197. DirItem := TCloudItem.Create;
  198. try
  199. DirItem.Name := azBlob.Name;
  200. if DirItem.Name.StartsWith(CurrentPath) then DirItem.Name := StringReplace(DirItem.Name,CurrentPath,'',[]);
  201. if DirItem.Name.Contains('/') then
  202. begin
  203. DirItem.IsDir := True;
  204. DirItem.Name := Copy(DirItem.Name,1,DirItem.Name.IndexOf('/'));
  205. end
  206. else
  207. begin
  208. DirItem.IsDir := False;
  209. DirItem.Size := StrToInt64Def(azBlob.Properties.Values['Content-Length'],0);
  210. DirItem.Date := GMT2DateTime(azBlob.Properties.Values['Last-Modified']);
  211. end;
  212. if Assigned(OnGetListItem) then OnGetListItem(DirItem);
  213. finally
  214. DirItem.Free;
  215. end;
  216. azBlob.Free;
  217. end;
  218. finally
  219. //frees azbloblist objects
  220. //for azBlob in azBlobList do azBlob.Free;
  221. azBlobList.Free;
  222. end;
  223. end
  224. else
  225. begin
  226. Status := stFailed;
  227. Exit;
  228. end;
  229. finally
  230. CloudResponseInfo.Free;
  231. end;
  232. finally
  233. FreeAndNil(AzParams);
  234. end;
  235. if Assigned(OnRefreshReadDir) then OnRefreshReadDir(CurrentPath);
  236. until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
  237. Status := stDone;
  238. finally
  239. BlobService.Free;
  240. if Assigned(OnEndReadDir) then OnEndReadDir(CurrentPath);
  241. end;
  242. end;
  243. {procedure TCloudStorageAzureProvider.OpenDir(const aPath : string);
  244. var
  245. lista : TBlobList;
  246. Blob : TAzureBlobObject;
  247. i : Integer;
  248. azurefilter : string;
  249. DirItem : TCloudItem;
  250. respinfo : TAzureResponseInfo;
  251. begin
  252. if aPath = '..' then
  253. begin
  254. CurrentPath := RemoveLastPathSegment(CurrentPath);
  255. end
  256. else
  257. begin
  258. if CurrentPath = '' then CurrentPath := aPath
  259. else CurrentPath := CurrentPath + aPath;
  260. end;
  261. if Assigned(OnBeginReadDir) then OnBeginReadDir(CurrentPath);
  262. if CurrentPath.StartsWith('/') then CurrentPath := Copy(CurrentPath,2,CurrentPath.Length);
  263. if (not CurrentPath.IsEmpty) and (not CurrentPath.EndsWith('/')) then CurrentPath := CurrentPath + '/';
  264. Status := stRetrieving;
  265. lista := fAzure.ListBlobs(RootFolder,CurrentPath,False,respinfo);
  266. try
  267. if Assigned(lista) then
  268. begin
  269. if Assigned(OnGetListItem) then
  270. begin
  271. DirItem := TCloudItem.Create;
  272. try
  273. DirItem.Name := '..';
  274. DirItem.IsDir := True;
  275. DirItem.Date := 0;
  276. OnGetListItem(DirItem);
  277. finally
  278. DirItem.Free;
  279. end;
  280. end;
  281. end;
  282. if respinfo.StatusCode = 200 then
  283. begin
  284. for Blob in lista do
  285. begin
  286. DirItem := TCloudItem.Create;
  287. try
  288. if Blob.Name.StartsWith(CurrentPath) then Blob.Name := StringReplace(Blob.Name,CurrentPath,'',[]);
  289. if Blob.Name.Contains('/') then
  290. begin
  291. DirItem.IsDir := True;
  292. DirItem.Name := Copy(Blob.Name,1,Blob.Name.IndexOf('/'));
  293. end
  294. else
  295. begin
  296. DirItem.IsDir := False;
  297. DirItem.Name := Blob.Name;
  298. DirItem.Size := Blob.Size;
  299. DirItem.Date := Blob.LastModified;
  300. end;
  301. if Assigned(OnGetListItem) then OnGetListItem(DirItem);
  302. finally
  303. DirItem.Free;
  304. end;
  305. end;
  306. Status := stDone;
  307. end
  308. else Status := stFailed;
  309. finally
  310. lista.Free;
  311. ResponseInfo.Get(respinfo.StatusCode,respinfo.StatusMsg);
  312. end;
  313. end;}
  314. procedure TCloudStorageAzureProvider.SetSecure(aValue: Boolean);
  315. begin
  316. inherited;
  317. if aValue then fAzureConnection.Protocol := 'HTTPS'
  318. else fAzureConnection.Protocol := 'HTTP';
  319. end;
  320. end.