Quick.CloudStorage.Provider.Amazon.pas 3.8 KB

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