aws s3 上傳 binary 數據 (通過stringstream)


有個需求需要將二進制istream上傳到s3上暫存,但苦於沒能直接找到接口,官方提供的設置數據塊的接口如下:

inline void SetBody(const std::shared_ptr<Aws::IOStream>& body) { m_bodyStream = body; }
這個Aws::IOStream其實就是std::iostream的封裝。
在實際尋找傳入參數的時候沒找到比較好的傳入對象:fstream意味着我需要先將數據存到磁盤再去讀取,感覺不是很好;而stringstream在裝二進制數據的時候又可能出現截斷的可能性。
最后我終於找到了 https://www.ojit.com/article/829511這個解釋,原來stringstream本身是可以存二進制數據流的,只不過這時候不能使用 <<>>函數,需要使用 std::stringstream::readstd::stringstream::write函數。
與此同時,從aws sdk的注解中我發現這個接口傳入的數據aws都會當做二進制數據格式:binary/octet-stream 進行讀取。這樣的話,往s3上面寫二進制流就沒什么問題了。
大致代碼如下:
std::shared_ptr<Aws::StringStream> input_data =
        Aws::MakeShared<Aws::StringStream>("");

    input_data->write(buffer.data(),srcSize);
    S3ClientWrapper *client = new S3ClientWrapper();
    std::string endpoint = "";
    ngmp::common::aws_client_configuration_info_struct config;
    client->Init(config);
    std::string errinfo;
    Aws::S3::S3Errors error_code;
    
    bool res = client->PutObject("bucket","object-name",input_data,errinfo, error_code,10);

 上面的代碼包含了一部分aws sdk的封裝代碼,僅供參考。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM