-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuPrincipal.pas
212 lines (177 loc) · 5.09 KB
/
uPrincipal.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
unit uPrincipal;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.Cloud.CloudAPI,
Data.Cloud.AmazonAPI, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.Buttons, Vcl.FileCtrl;
type
TForm1 = class(TForm)
amcAmazon: TAmazonConnectionInfo;
stat1: TStatusBar;
lbl1: TLabel;
lbl2: TLabel;
btnLoad: TSpeedButton;
btndownload: TSpeedButton;
btnexcluir: TSpeedButton;
btnbuckets: TSpeedButton;
btnArquivos: TSpeedButton;
lstlistabuckts: TListBox;
lstListaArquivos: TListBox;
edtBucket: TEdit;
btnBucket: TSpeedButton;
dlgOpen1: TOpenDialog;
AmazonConnectionInfo1: TAmazonConnectionInfo;
procedure FormCreate(Sender: TObject);
procedure btnbucketsClick(Sender: TObject);
procedure btnArquivosClick(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
procedure btndownloadClick(Sender: TObject);
procedure btnexcluirClick(Sender: TObject);
procedure btnBucketClick(Sender: TObject);
private
{ Private declarations }
s3: TAmazonStorageService;
s3Regiao: TAmazonRegion;
sRegiao: String;
public
{ Public declarations }
end;
const
Acceskey = 'SUA CHAVE AQUI';
Secretkey = 'SUA SENHA AQUI';
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnBucketClick(Sender: TObject);
begin
s3.CreateBucket(edtBucket.Text, TAmazonACLType.amzbaPrivate, TAmazonRegion.amzrUSWest1, nil);
end;
procedure TForm1.btnArquivosClick(Sender: TObject);
var
strBuckt: String;
bucktInfo: TAmazonBucketResult;
ObjInfo : TAmazonObjectResult;
begin
strBuckt := lstlistabuckts.Items[lstlistabuckts.ItemIndex];
bucktInfo := s3.GetBucket(strBuckt, nil);
lstListaArquivos.Items.Clear;
for ObjInfo in bucktInfo.Objects do
begin
lstListaArquivos.Items.Add(ObjInfo.Name);
end;
end;
procedure TForm1.btnbucketsClick(Sender: TObject);
var
respInfo: TCloudResponseInfo;
list: TStrings;
i: integer;
begin
respInfo := TCloudResponseInfo.Create;
try
list := s3.ListBuckets(respInfo);
lstlistabuckts.Items.Clear;
if Assigned(list) then
for i := 0 to Pred( list.Count ) do
begin
lstlistabuckts.Items.Add(list.Names[i]);
end;
finally
respInfo.Free;
list.Free;
end;
end;
procedure TForm1.btndownloadClick(Sender: TObject);
var
fStream: TStream;
sDir, sFile: String;
begin
fStream := TMemoryStream.Create;
try
sFile := lstListaArquivos.Items[lstListaArquivos.ItemIndex];
Screen.Cursor := crHourGlass;
s3.GetObject(lstlistabuckts.Items[lstlistabuckts.ItemIndex], sFile, fStream);
fStream.Position := 0;
sDir := ExtractFilePath(ParamStr(0));
if selectDirectory('Selecione a pasta', 'C:\', sDir) then
begin
TMemoryStream(fStream).SaveToFile(sDir+PathDelim+sFile);
MessageDlg('Arquivo salvo com sucesso.', mtInformation, [mbOK],0);
end;
finally
fStream.Free;
Screen.Cursor := crDefault;
end;
end;
procedure TForm1.btnexcluirClick(Sender: TObject);
var
sfile: String;
begin
sfile := lstListaArquivos.Items[lstListaArquivos.ItemIndex];
if MessageDlg('Deseja realmente excluir este arquivo?', mtConfirmation, [mbYes, mbNo],0) = mrYes then
begin
try
try
Screen.Cursor := crHourGlass;
s3.DeleteObject(lstlistabuckts.Items[lstlistabuckts.ItemIndex], sfile);
finally
Screen.Cursor := crDefault;
end;
except
MessageDlg('Erro ao excluir o arquivo.', mtError, [mbOK], 0);
end;
end;
end;
procedure TForm1.btnLoadClick(Sender: TObject);
var
sFile : String;
fContents : TBytes;
fReader : TBinaryReader;
sMeta : TStringList;
begin
if dlgOpen1.Execute then
begin
sFile := ExtractFileName(dlgOpen1.FileName);
fReader := TBinaryReader.Create(dlgOpen1.FileName);
try
fContents := fReader.ReadBytes(fReader.BaseStream.Size);
finally
fReader.Free;
end;
try
try
sMeta := TStringList.Create;
sMeta.Add('Content-type=*.jpg');
Screen.Cursor := crHourGlass;
try
s3.UploadObject(lstlistabuckts.Items[lstlistabuckts.ItemIndex], sFile, fContents, False, sMeta);
MessageDlg('Operação executada com sucesso.', mtInformation, [mbOK], 0);
except
on e: Exception do
begin
ShowMessage(e.Message);
end;
end;
finally
Screen.Cursor := crDefault;
sMeta.Free;
end;
except
on e: exception do
begin
Screen.Cursor := crDefault;
MessageDlg('Erro ao executar o upload.', mtError, [mbOK], 0);
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
amcAmazon.AccountName := Acceskey;
amcAmazon.AccountKey := Secretkey;
s3 := TAmazonStorageService.Create(amcAmazon);
sRegiao := TAmazonStorageService.GetRegionString(s3Regiao);
lbl1.Caption := amcAmazon.StorageEndpoint;
lbl2.Caption := sRegiao;
end;
end.