Quick.CloudStorage.Provider.Amazon.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.CloudStorage.Provider.Amazon
  4. Description : CloudStorage Amazon provider
  5. Author : Kike Pérez
  6. Version : 1.8
  7. Created : 14/10/2018
  8. Modified : 07/10/2019
  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.CloudStorage.Provider.Amazon;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. System.SysUtils,
  27. Quick.Commons,
  28. Quick.CloudStorage,
  29. Quick.Amazon;
  30. type
  31. TCloudStorageAmazonProvider = class(TCloudStorageProvider)
  32. private
  33. fAmazon : TQuickAmazon;
  34. fAmazonID : string;
  35. fAmazonKey: string;
  36. fAmazonRegion : string;
  37. fSecure : Boolean;
  38. fCurrentBuckeet: string;
  39. procedure SetSecure(const Value: Boolean);
  40. public
  41. constructor Create; overload;
  42. constructor Create(const aAccountID, aAccountKey, aAWSRegion : string); overload;
  43. destructor Destroy; override;
  44. property Secure : Boolean read fSecure write SetSecure;
  45. procedure OpenDir(const aPath : string); override;
  46. function GetFile(const aPath: string; out stream : TStream) : Boolean; override;
  47. end;
  48. implementation
  49. { TCloudExplorerProvider }
  50. constructor TCloudStorageAmazonProvider.Create;
  51. begin
  52. fAmazon := TQuickAmazon.Create;
  53. fAmazon.AmazonProtocol := amHTTPS;
  54. end;
  55. constructor TCloudStorageAmazonProvider.Create(const aAccountID, aAccountKey, aAWSRegion : string);
  56. begin
  57. Create;
  58. fAmazonID := aAccountID;
  59. fAmazonKey := aAccountKey;
  60. fAmazonRegion := aAWSRegion;
  61. fAmazon.AccountName := fAmazonID;
  62. fAmazon.AccountKey := fAmazonKey;
  63. fAmazon.AWSRegion := TQuickAmazon.GetAWSRegion(fAmazonRegion);
  64. end;
  65. destructor TCloudStorageAmazonProvider.Destroy;
  66. begin
  67. if Assigned(fAmazon) then fAmazon.Free;
  68. inherited;
  69. end;
  70. function TCloudStorageAmazonProvider.GetFile(const aPath: string; out stream : TStream) : Boolean;
  71. begin
  72. end;
  73. procedure TCloudStorageAmazonProvider.OpenDir(const aPath : string);
  74. var
  75. lista : TAmazonObjects;
  76. Blob : TAmazonObject;
  77. i : Integer;
  78. azurefilter : string;
  79. DirItem : TCloudItem;
  80. respinfo : TAmazonResponseInfo;
  81. begin
  82. if aPath = '..' then
  83. begin
  84. CurrentPath := RemoveLastPathSegment(CurrentPath);
  85. end
  86. else
  87. begin
  88. if CurrentPath = '' then CurrentPath := aPath
  89. else CurrentPath := CurrentPath + aPath;
  90. end;
  91. if Assigned(OnBeginReadDir) then OnBeginReadDir(CurrentPath);
  92. if CurrentPath.StartsWith('/') then CurrentPath := Copy(CurrentPath,2,CurrentPath.Length);
  93. if (not CurrentPath.IsEmpty) and (not CurrentPath.EndsWith('/')) then CurrentPath := CurrentPath + '/';
  94. Status := stRetrieving;
  95. lista := fAmazon.ListObjects(RootFolder,CurrentPath,fAmazon.AWSRegion,respinfo);
  96. try
  97. if CurrentPath <> '' then
  98. begin
  99. if Assigned(OnGetListItem) then
  100. begin
  101. DirItem := TCloudItem.Create;
  102. try
  103. DirItem.Name := '..';
  104. DirItem.IsDir := True;
  105. DirItem.Date := 0;
  106. OnGetListItem(DirItem);
  107. finally
  108. DirItem.Free;
  109. end;
  110. end;
  111. end;
  112. if respinfo.StatusCode = 200 then
  113. begin
  114. for Blob in lista do
  115. begin
  116. DirItem := TCloudItem.Create;
  117. try
  118. if Blob.Name.StartsWith(CurrentPath) then Blob.Name := StringReplace(Blob.Name,CurrentPath,'',[]);
  119. if Blob.Name.Contains('/') then
  120. begin
  121. DirItem.IsDir := True;
  122. DirItem.Name := Copy(Blob.Name,1,Blob.Name.IndexOf('/'));
  123. end
  124. else
  125. begin
  126. DirItem.IsDir := False;
  127. DirItem.Name := Blob.Name;
  128. DirItem.Size := Blob.Size;
  129. DirItem.Date := Blob.Modified;
  130. end;
  131. if Assigned(OnGetListItem) then OnGetListItem(DirItem);
  132. finally
  133. DirItem.Free;
  134. end;
  135. end;
  136. Status := stDone;
  137. end
  138. else Status := stFailed;
  139. finally
  140. lista.Free;
  141. ResponseInfo.Get(respinfo.StatusCode,respinfo.StatusMsg);
  142. end;
  143. end;
  144. procedure TCloudStorageAmazonProvider.SetSecure(const Value: Boolean);
  145. begin
  146. fSecure := Value;
  147. if Value then fAmazon.AmazonProtocol := TAmazonProtocol.amHTTPS
  148. else fAmazon.AmazonProtocol := TAmazonProtocol.amHTTP;
  149. end;
  150. end.