2017-05-24 61 views
0

我在單元Data.Cloud.AmazonAPI中使用UploadObject將一些文件上傳到AWS S3,它可以工作,但是如何在上傳或下載時獲取進度信息?這是我的代碼:在delphi中使用AmazonAPI上傳文件時獲取進度

function TForm1.UploadFile(LocalFilePath: string; RemoteFileName: string; Bucket: string): Boolean; 
var 
    Service: TAmazonStorageService; 
    ConAmazon: TAmazonConnectionInfo; 
    FS: TFileStream; 
    Content: TBytes; 
begin 
    try 
    ConAmazon := TAmazonConnectionInfo.Create(nil); 
    ConAmazon.AccountKey := 'MtJqIM7WyjJA*********************'; 
    ConAmazon.AccountName := 'AKIAIXVAH*********'; 
    ConAmazon.QueueEndpoint := 'queue.amazonaws.com'; 
    ConAmazon.StorageEndpoint := 's3-eu-west-1.amazonaws.com'; 
    ConAmazon.TableEndpoint := 'sdb.amazonaws.com'; 
    ConAmazon.UseDefaultEndpoints := True; 
    Service := TAmazonStorageService.Create(ConAmazon); 
    if FileExists(LocalFilePath) then 
    begin 
     FS := TFileStream.Create(LocalFilePath, fmOpenRead); 
     Content := ByteContent(FS); 
     FS.Free; 
     Result := Service.UploadObject(Bucket, RemoteFileName, Content, True, nil, nil, amzbaPrivate, nil, OnProgress); 
    end 
    else 
     Result := False; 
    finally 
    ConAmazon.Free; 
    Service.Free; 
    end; 
end; 
+0

他們是怎麼亞馬遜文檔說? –

+1

你可以看看這個:https://gist.github.com/sandfox/1370593。我還沒有徹底解決,但它似乎支持一個進度條來顯示上傳的進度。 – MartynA

回答

0

如需下載請檢查這個問題Can I monitor the progress of an S3 download using the Cloud.AmazonAPI? 對於上載是相似的,但你需要創建TAmazonStorageService子如下

type 
    TProgressAmazonStorageService = class(TAmazonStorageService) 
    function IssuePutRequest(URL: string; Headers: TStringList; 
          QueryParameters: TStringList; const QueryPrefix: string; 
          ResponseInfo: TCloudResponseInfo; 
          Content: TStream; out ResponseString: string): TCloudHTTP; overload; override; 
    end; 

function TProgressAmazonStorageService.IssuePutRequest(URL: string; Headers: TStringList; 
          QueryParameters: TStringList; const QueryPrefix: string; 
          ResponseInfo: TCloudResponseInfo; 
          Content: TStream; out ResponseString: string): TCloudHTTP; 
var 
    ProgressStream: TProgressStream; 
begin 
    Result := PrepareRequest('PUT', Headers, QueryParameters, QueryPrefix, URL); 

    try 
    ProgressStream := TProgressStream.Create(Content); 
    try 
     ProgressStream.OnProgress := Form1.OnProgress; 
     Form1.ProgressBar1.Max := Content.Size; 
     Form1.ProgressBar1.Value := 0; 

     if Content <> nil then 
     ResponseString := Result.Put(URL, ProgressStream) 
     else 
     ResponseString := Result.Put(URL); 
    finally 
     ProgressStream.Free; 
    end; 

    PopulateResponseInfo(Result, ResponseInfo); 
    except 
    on E: Exception do 
    begin 
     Result.Free; 
     Raise; 
    end; 
    end; 
end; 

進步函數看起來像這樣

procedure TForm1.OnProgress(const ACount: Int64); 
begin 
    Form1.ProgressBar1.Value := Form1.ProgressBar1.Value + ACount; 
    Application.ProcessMessages; 
end; 

TProgressStream這樣

type 
    TOnProgressEvent = procedure(const ACount: Int64) of object; 
    TProgressStream = class(TStream) 
    strict private 
    FStream: TStream; 
    protected 
    function GetSize: Int64; override; 
    procedure SetSize(NewSize: Longint); overload; override; 
    procedure SetSize(const NewSize: Int64); overload; override; 
    public 
    OnProgress: TOnProgressEvent; 
    function Read(var Buffer; Count: Longint): Longint; overload; override; 
    function Write(const Buffer; Count: Longint): Longint; overload; override; 
    function Read(Buffer: TBytes; Offset, Count: Longint): Longint; overload; override; 
    function Write(const Buffer: TBytes; Offset, Count: Longint): Longint; overload; override; 
    function Seek(Offset: Longint; Origin: Word): Longint; overload; override; 
    function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; override; 
    constructor Create(const AStream: TStream); 
    end; 

constructor TProgressStream.Create(const AStream: TStream); 
begin 
    FStream := AStream; 
end; 

function TProgressStream.GetSize: Int64; 
begin 
    Result := FStream.Size; 
end; 

procedure TProgressStream.SetSize(NewSize: Longint); 
begin 
    FStream.Size := NewSize; 
end; 

procedure TProgressStream.SetSize(const NewSize: Int64); 
begin 
    FStream.Size := NewSize; 
end; 

function TProgressStream.Read(var Buffer; Count: Longint): Longint; 
begin 
    Result := FStream.Read(Buffer, Count); 
end; 

function TProgressStream.Write(const Buffer; Count: Longint): Longint; 
begin 
    Result := FStream.Write(Buffer, Count); 
end; 

function TProgressStream.Read(Buffer: TBytes; Offset, Count: Longint): Longint; 
begin 
    if Assigned(OnProgress) then 
    begin 
    OnProgress(Count); 
    end; 
    Result := FStream.Read(Buffer, Offset, Count); 
end; 

function TProgressStream.Write(const Buffer: TBytes; Offset, Count: Longint): Longint; 
begin 
    Result := FStream.Write(Buffer, Offset, Count); 
end; 

function TProgressStream.Seek(Offset: Longint; Origin: Word): Longint; 
begin 
    Result := FStream.Seek(Offset, Origin); 
end; 

function TProgressStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; 
begin 
    Result := FStream.Seek(Offset, Origin); 
end; 
+0

這對我很有幫助,非常感謝。 – cnwalker