介紹如何使用 C++ AWS SDK 中的 S3 TransferManager
開發 object 下載程式,以串流寫入檔案。
S3 TransferManager
串流下載
以下是以 TransferManager
來自動處理 S3 下載流程的範例程式。
#include <aws/s3/S3Client.h> #include <aws/core/Aws.h> #include <aws/core/auth/AWSCredentialsProvider.h> #include <aws/transfer/TransferManager.h> #include <iostream> #include <fstream> static const char* const ALLOCATION_TAG = "S3_DOWNLOAD"; int main() { Aws::SDKOptions options; Aws::InitAPI(options); { // 自訂 S3 End Point const Aws::String endPoint = "YOUR.END.POINT"; // S3 Bucket 名稱 const Aws::String bucketName = "YOUR_BUCKET"; // Object 名稱 const Aws::String objectName = "CentOS-7-x86_64-Everything-1810.iso"; // 檔案名稱 Aws::String downloadFileName = objectName; // 認證資訊 const Aws::String accessKey = "ASFHDGN345JGS436FG53"; const Aws::String secretKey = "45G54d4cbGDF56bnFsfdgh489dfGHDdfgDFGHs4e"; // 建立 S3 Client Aws::Client::ClientConfiguration config; config.verifySSL = false; auto s3Client = Aws::MakeShared<Aws::S3::S3Client>(ALLOCATION_TAG, Aws::Auth::AWSCredentials(accessKey, secretKey), config); // 設定 S3 End Point s3Client->OverrideEndpoint(endPoint); // 建立 TransferManagerConfiguration auto sdk_client_executor = Aws::MakeShared<Aws::Utils::Threading::DefaultExecutor>(ALLOCATION_TAG); Aws::Transfer::TransferManagerConfiguration transferConfig(sdk_client_executor.get()); transferConfig.s3Client = s3Client; // 下載進度回呼函數 transferConfig.downloadProgressCallback = [](const Aws::Transfer::TransferManager*, const std::shared_ptr<const Aws::Transfer::TransferHandle>& handle) { std::cout << "Download Progress: " << handle->GetBytesTransferred() << " of " << handle->GetBytesTotalSize() << " bytes"; }; // 建立 TransferManager auto transferManager = Aws::Transfer::TransferManager::Create(transferConfig); // 下載 Object auto transferHandle = transferManager->DownloadFile(bucketName, objectName, [=](){ return Aws::New<Aws::FStream>(ALLOCATION_TAG, downloadFileName.c_str(), std::ios_base::out | std::ios_base::binary); }); // 等待下載 transferHandle->WaitUntilFinished(); } Aws::ShutdownAPI(options); return 0; }
此程式碼要配合以下 CMakeLists.txt
設定,以 CMake 來編譯:
cmake_minimum_required(VERSION 3.8) # 使用 Shared 的 AWS C++ SDK 函式庫(預設狀況) option(BUILD_SHARED_LIBS "Build shared libraries" ON) # 設定專案名稱 project(Hello) # 尋找並引入 AWS SDK for C++ 函式庫套件 find_package(AWSSDK REQUIRED COMPONENTS s3 transfer) # 增加一個執行檔 add_executable(hello hello.cpp) # 以 C++11 標準編譯 target_compile_features(hello PUBLIC cxx_std_11) # 定義執行檔連結方式 target_link_libraries(hello ${AWSSDK_LINK_LIBRARIES})