아마존 S3 서비스를 이용하기 위해서 현재 실무에서 이용하고 있는 함수 소스입니다.
(개발 환경은 Windows 11 Pro, RAD Studio 11 Alexandria 입니다.)
c++ 빌더로 프로그램을 만들때 필요한 콤포넌트는 AmazonConnectionInfo 입니다.
AmazonConnectionInfo 콤포넌트를 폼에 올려놓고 아래 소스를 이용하시면 됩니다.
1. 파일 업로드 함수 : AmazonS3_Upload
제가 이용하고 있는 파일들의 stContent_Type 종류는 다음과 같습니다.
"image/jpeg"
"image/png"
"image/gif"
"image/bmp"
"text/plain"
"application/pdf"
"application/zip"
"audio/mpeg"
2. 파일 삭제 함수 : AmazonS3_Delete
3. 파일 존재 유무 확인 함수 : AmazonS3_CheckExists
4. 파일 다운로드 함수 : AmazonS3_Download
5. 파일 리스트 가져오는 함수 : AmazonS3_List_String ( String 으로 리턴받을 경우. 다만 리턴 파일 리스트가 1,000개까지 밖에 안됨.)
6. 파일 리스트 가져오는 함수 : AmazonS3_List_StringList ( StringList 를 이용할 경우. 다만 리턴 파일 리스트가 1,000개까지 밖에 안됨.)
* 참고로 S3에서 파일 리스트를 리턴해 주는 파일 갯수가 1,000개까지입니다. 저의 경우는 S3 파일갯수가 1,000 개를 넘기 때문에, S3에서 제공하는 인벤토리 레포트 기능을 활용하여 레포트 파일을 만들고, 그 파일을 가져와서 파싱하여 활용하고 있습니다.
=============================================================
//---------------------------------------------------------------------------
bool __fastcall TMainForm::AmazonS3_Upload(String stBucketName, String stActName, String stActKey, String stFileNameWithPath, String stContent_Type)
{
String stDate, stFiles ;
this->AmazonConnectionInfo1->AccountName = stActName ;
this->AmazonConnectionInfo1->AccountKey = stActKey ;
try {
std::unique_ptr<TAmazonStorageService> s3(new TAmazonStorageService(AmazonConnectionInfo1));
TCloudResponseInfo* resUpload_(new TCloudResponseInfo());
std::unique_ptr<TMemoryStream> mm(new TMemoryStream());
///upload//
mm->LoadFromFile( stFileNameWithPath );
DynamicArray<Byte> arr1;
arr1.Length = mm->Size;
mm->ReadBuffer(arr1, mm->Size);
mm->Position = 0;
TStringList * stl_Header = new TStringList ;
stl_Header->Values["Content-type"] = stContent_Type ; // "image/jpeg";
s3->UploadObject(stBucketName, ExtractFileName(stFileNameWithPath), arr1, false, NULL, stl_Header, TAmazonACLType::amzbaPrivate, resUpload_);
delete stl_Header ;
///upload end//
delete resUpload_ ;
} catch(...) {
return false ;
} ;
return true ;
}
=============================================================
2. 파일 삭제 함수 : AmazonS3_Delete
//--------------------------------------------------------------------------
bool __fastcall TMainForm::AmazonS3_Delete(String stBucketName, String stActName, String stActKey, String stFileName)
{
String stDate, stFiles ;
this->AmazonConnectionInfo1->AccountName = stActName ;
this->AmazonConnectionInfo1->AccountKey = stActKey ;
std::unique_ptr<TAmazonStorageService> s3(new TAmazonStorageService(AmazonConnectionInfo1));
TCloudResponseInfo * resDelete_(new TCloudResponseInfo());
try {
///Delete file//
s3->DeleteObject(stBucketName, stFileName, resDelete_);
///Delete file end//
} catch(...) {
delete resDelete_ ;
return false ;
} ;
delete resDelete_ ;
return true ;
}
=============================================================
3. 파일 존재 유무 확인 함수 : AmazonS3_CheckExists
//--------------------------------------------------------------------------
bool __fastcall TMainForm::AmazonS3_CheckExists(String stBucketName, String stActName, String stActKey, String stFileName)
{
String stDate, stFiles ;
this->AmazonConnectionInfo1->AccountName = stActName ;
this->AmazonConnectionInfo1->AccountKey = stActKey ;
std::unique_ptr<TAmazonStorageService> s3(new TAmazonStorageService(AmazonConnectionInfo1));
TCloudResponseInfo* resCheck_(new TCloudResponseInfo());
try {
std::unique_ptr<TMemoryStream> mm(new TMemoryStream());
///download///
mm->Clear();
TAmazonGetObjectOptionals* op_(new TAmazonGetObjectOptionals());
if ( ! s3->GetObject( stBucketName, stFileName, mm.get(), resCheck_) ) {
delete resCheck_ ;
return false ;
} ;
///download end///
} catch(...) {
delete resCheck_ ;
return false ;
} ;
delete resCheck_ ;
return true ;
}
=============================================================
4. 파일 다운로드 함수 : AmazonS3_Download
//--------------------------------------------------------------------------
bool __fastcall TMainForm::AmazonS3_Download(String stBucketName, String stActName, String stActKey, String stGetFileName, String stSaveFileNameWithPath)
{
String stDate, stFiles ;
this->AmazonConnectionInfo1->AccountName = stActName ;
this->AmazonConnectionInfo1->AccountKey = stActKey ;
std::unique_ptr<TAmazonStorageService> s3(new TAmazonStorageService(AmazonConnectionInfo1));
TCloudResponseInfo* resDownload_(new TCloudResponseInfo());
try {
std::unique_ptr<TMemoryStream> mm(new TMemoryStream());
///download///
mm->Clear();
TAmazonGetObjectOptionals* op_(new TAmazonGetObjectOptionals());
s3->GetObject( stBucketName, stGetFileName, mm.get(), resDownload_); // bool
mm.get()->SaveToFile(stSaveFileNameWithPath);
delete op_ ;
///download end///
} catch(...) {
delete resDownload_ ;
return false ;
} ;
delete resDownload_ ;
return true ;
}
=============================================================
5. 파일 리스트 가져오는 함수 : AmazonS3_List_String ( String 으로 리턴받을 경우)
//---------------------------------------------------------------------------
/* 이용방법
String stS3List ;
stS3List = AmazonS3_List_String(stBucketName, stActName, stActKey, stl_list) ;
...
...
...
*/
//---------------------------------------------------------------------------
String __fastcall TMainForm::AmazonS3_List_String(String stBucketName, String stActName, String stActKey)
{
String stFiles ;
this->AmazonConnectionInfo1->AccountName = stActName ;
this->AmazonConnectionInfo1->AccountKey = stActKey ;
std::unique_ptr<TStringList> listView_( new TStringList() ) ;
std::unique_ptr<TAmazonStorageService> s3View(new TAmazonStorageService(AmazonConnectionInfo1));
TCloudResponseInfo* resView_(new TCloudResponseInfo());
TStringList * amzFileList = new TStringList ;
amzFileList->Clear() ;
stFiles = "" ;
TAmazonBucketResult * bs = s3View->GetBucket(stBucketName, listView_.get(), resView_);
for(int i = 0; i<bs->Objects->Count; i++) {
amzFileList->Add(bs->Objects->Items[i].Name );
} ;
stFiles = amzFileList->Text ;
delete resView_ ;
delete amzFileList ;
return stFiles ;
}
=============================================================
6. 파일 리스트 가져오는 함수 : AmazonS3_List_StringList ( StringList 를 이용할 경우)
//---------------------------------------------------------------------------
/* 이용방법
TStringList * stl_list = new TStringList ;
stl_list->Clear() ;
AmazonS3_List_StringList(stBucketName, stActName, stActKey, stl_list) ;
for (int i = 0; i < stl_list->Count; i++) {
...
...
...
} ;
delete stl_list ;
*/
//---------------------------------------------------------------------------
void __fastcall TMainForm::AmazonS3_List_StringList(String stBucketName, String stActName, String stActKey, TStringList stl_list[])
{
this->AmazonConnectionInfo1->AccountName = stActName ;
this->AmazonConnectionInfo1->AccountKey = stActKey ;
std::unique_ptr<TStringList> listView_( new TStringList() ) ;
std::unique_ptr<TAmazonStorageService> s3View(new TAmazonStorageService(AmazonConnectionInfo1));
TCloudResponseInfo* resView_(new TCloudResponseInfo());
TAmazonBucketResult * bs = s3View->GetBucket(stBucketName, listView_.get(), resView_);
for(int i = 0; i<bs->Objects->Count; i++) {
stl_list->Add(bs->Objects->Items[i].Name );
} ;
delete bs ;
delete resView_ ;
return ;
}
=============================================================
이상입니다.
|