8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Delphi 10.3 を使用してAWS S3からファイルを取得する

Last updated at Posted at 2019-05-21

やりたいこと

AWS S3からDelphi10.3を使用して
ファイルの取得を行う

環境

Windows 10
Delphi 10.3

実装

Formへ
AmazonconnectionInfoを張り付ける
image.png

AmazonconnectionInfo1の
プロパティ
AccountKey
AccountName
にAWSにて発行した値を挿入する
image.png

対象のコード

const
  BUCKET_NAME = 'your-bucket-name';
function TForm1.getUsersData(selectDir: TSelectDirOpt; userId : String):boolean;
var
  s3 : TAmazonStorageService;
  stream : TStringStream;
  objFolder : string;
  LFileName : String;
begin
  s3 := TAmazonStorageService.Create(AmazonConnectionInfo1);
  stream := TStringStream.Create;
  objFolder := 'your-folder/';
  LFileName := objFolder + your-file-name + '.json';
  if s3.GetObject(BUCKET_NAME, LFileName, stream) then
  begin
    stream.SaveToFile('c:\' + LFileName)
  end else
  begin
    showmessage('Download failed(score landmark)');
  end;
end;

※LFileNameには取得したいファイル名を記述してください。

所要時間

5分でできた。

追記

シンプルに書くとこんな感じ

GetObject from S3


procedure TForm1.btnDownloadClick(Sender: TObject);
var
  s3 :TAmazonStorageService;
  strstr : TStringStream;
begin
  s3 := TAmazonStorageService.Create(AmazonConnectionInfo1);
  strstr := TStringStream.Create;
  try
    if s3.GetObject(BUCKET_NAME, OBJ_NAME, strstr) then
      ShowMessage(strstr.DataString)
    else
      ShowMessage('Download failed');
  finally
    strstr.Free;
    s3.Free;
  end;
end;

uploadObject to S3


procedure TForm1.btnUploadClick(Sender: TObject);
var
  s3 :TAmazonStorageService;
  strstr : TStringStream;
begin
  s3 := TAmazonStorageService.Create(AmazonConnectionInfo1);
  strstr := TStringStream.Create(edit1.Text);
  try
    if s3.UploadObject(BUCKET_NAME, OBJ_NAME, strstr.Bytes) then
      ShowMessage('OK')
    else
      ShowMessage('Upload failed');
  finally
    strstr.Free;
    s3.Free;
  end;
end;

※ edit1.textは任意で、好きな値を入れて

8
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?