Search code examples
amazon-web-servicesdelphisslamazon-s3ftp

from ftp ssl to Amazon S3


I have a system that works with FTP over SSL in Delphi XE5, but I am planning to switch to Amazon S3. I'm trying to make a straightforward set of commans that will set parameters, connect, put files in bucket and disconnect.

Right now I'm doing this with IDFTP

ftp := TIdFTP.Create();
  ssl := TIdSSLIOHandlerSocketOpenSSL.Create(ftp);
  ftp.IOHandler := ssl;
  ssl.SSLOptions.Method := sslvSSLv23;
  ftp.UseTLS := utUseExplicitTLS;
  ftp.Host := 'myftpserver.com';
  ftp.Username := 'user';
  ftp.Password := 'password';
  ftp.Passive := true;
  ftp.Connect;
  ftp.ChangeDir(destinefolder);

for i := 1 to3 do    
begin               //upload  file1.jpg, file2.jpg, file3.jpg
ftp.put (local + ‘file’ + inttostr(i) + ’.jpg’ , ‘file’ + inttostr(i), true, false);
end;

ssl.Free;
 ftp.Free;  

It works nice for FTPS.

I have read people saying that it is possible to use IDhttp for Amazon S3.

I wonder if there is a simple way to adapt my ftps to amazon s3.


Solution

  • Based on the AWS documentation:

    PUT Object

    Authenticating a Request in the Authorization Header

    Try this:

    Auth := '...'; // calculate/format based on Access Key Id Secret Access Key...
    
    for i := 1 to 3 do    
    begin         
      FS := TFileStream.Create(local + 'file' + inttostr(i) + '.jpg', fmOpenRead or fmShareDenyWrite);
      try
        IdHTTP1.Request.CustomHeaders.Values['Authorization'] := Auth;
        IdHTTP1.Request.BasicAuthentication := False;
        IdHTTP1.Request.Date := ...;
        IdHTTP1.Request.Expect := '100-continue';
        IdHTTP1.Request.ProtocolVersion := pv1_1;
        // other headers as needed...
        ...
        IdHTTP1.Put('http://'+BucketName+'.s3.amazonaws.com/file' + inttostr(i), FS);
      finally
        FS.Free;
      end;
    end;