AWS C ++ SDK UploadPart超时

时间:2016-02-10 17:32:14

标签: c++ amazon-web-services amazon-s3 aws-sdk

我尝试使用AWS C ++ SDK将文件上传到Amazon S3。

对CreateMultipartUpload的调用成功返回,但以下对UploadPart的调用超时,出现以下错误:

(Aws::String) m_message = "Unable to parse ExceptionName: RequestTimeout Message: Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed."

我不明白为什么发起呼叫有效,但不理解部分上传呼叫。显然没有任何网络问题。

这是我的代码:

bool FileUploader::uploadChunk() {

    Aws::S3::Model::UploadPartRequest request;
    request.SetBucket("video");
    request.SetKey(_key);
    request.SetUploadId(_file->uploadId);
    request.SetPartNumber(_file->chunksUploaded + 1);

    long file_pos = _file->chunksUploaded * CHUNK_SIZE;
    _input_file.seekg(file_pos, std::ios::beg);

    _input_file.read(_file_buf, CHUNK_SIZE);
    long n_bytes = _input_file.gcount();

    if(n_bytes > 0) {

        request.SetContentLength(n_bytes);

        char_array_buffer buf2(_file_buf, _file_buf + n_bytes);
        std::iostream *chunk_stream = new std::iostream(&buf2);

        request.SetBody(std::shared_ptr<std::iostream>(chunk_stream));

        Aws::S3::Model::UploadPartOutcome response = _client->UploadPart(request);
        if(response.IsSuccess()) {
            _file->chunksUploaded++;
            _uploader->updateUploadStatus(_file);
        }

        return response.IsSuccess();

    }
    else {
        return false;
    }

}

2 个答案:

答案 0 :(得分:2)

问题是我获取SetBody流的方法。我转而使用升级库而不是自己开发的方法。

    typedef boost::iostreams::basic_array_source<char> Device;
    boost::iostreams::stream_buffer<Device> stmbuf(_file_buf, n_bytes);

    std::iostream *stm = new std::iostream(&stmbuf);

    request.SetBody(std::shared_ptr<Aws::IOStream>(stm));

这很有效。

我还需要跟踪我上传的用于调用CompleteMultipartUpload的部分,如下所示:

    Aws::S3::Model::CompletedPart part;
    part.SetPartNumber(request.GetPartNumber());
    part.SetETag(response.GetResult().GetETag());
    _uploadedParts.AddParts(part);

答案 1 :(得分:0)

或者,您可以使用TransferManager界面为您执行此操作。它有一个IOStream接口。另外,我们为iostream提供了预分配的缓冲区实现:

https://github.com/aws/aws-sdk-cpp/blob/master/aws-cpp-sdk-core/include/aws/core/utils/stream/PreallocatedStreamBuf.h