Skip to content

[Feature][#13] 分片上传新增接口,支持一次上传自定义的一段buffer #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 91 additions & 2 deletions ucloud/api/mput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ UFileMput::UFileMput()

UFileMput::~UFileMput() {

if (m_file_stream && m_file_stream->is_open())
m_file_stream->close();
if (m_file_stream) {
if (m_file_stream->is_open()) {
m_file_stream->close();
}
delete m_file_stream;
}
}

void UFileMput::SetResource(const std::string &bucket, const std::string &key) {
Expand Down Expand Up @@ -144,6 +148,12 @@ int UFileMput::MUpload(ssize_t blk_idx) {
if (ret)
return ret;

if (!m_is) {
UFILE_SET_ERROR2(ERR_CPPSDK_INVALID_PARAM,
"stream has not been set up yet");
return ERR_CPPSDK_INVALID_PARAM;
}

if (blk_idx != -1)
m_blk_idx = blk_idx;

Expand Down Expand Up @@ -226,6 +236,85 @@ int UFileMput::MUpload(ssize_t blk_idx) {
return ret;
}

int UFileMput::MUpload(ssize_t blk_idx, const char *data, size_t len) {
int64_t ret = InitGlobalConfig();
if (ret)
return ret;

m_blk_idx = blk_idx;

if (m_mimetype == "") {
ret = MimeType(m_filename, m_mimetype);
if (ret)
return ret;
}

UCloudStreamBuf sbuf(data, len);
std::istream iss(&sbuf);
if (!iss) {
UFILE_SET_ERROR(ERR_CPPSDK_FILE_READ);
return ERR_CPPSDK_FILE_READ;
}

//开始上传数据
std::string signature("");
//构建 HTTP 头部
m_http->Reset();
m_http->SetVerb("PUT");
m_http->AddHeader("Content-Type", m_mimetype);
m_http->AddHeader("Content-Length", SIZET2STR(len));
m_http->AddHeader("User-Agent", USERAGENT);
m_http->SetURL(MUploadURL());

//使用 HTTP 信息构建签名
UFileDigest digestor;
ret = digestor.SignWithRequest(m_http, HEAD_FIELD_CHECK, m_bucket, m_key, "",
signature);
if (ret) {
return ret;
}
m_http->AddHeader("Authorization", digestor.Token(signature));

//设置输出
std::ostringstream oss, hss;
UCloudOStream data_stream(&oss);
UCloudOStream header_stream(&hss);
UCloudHTTPReadParam rp =
{f : NULL, is : iss, fsize : len, need_total_n : len};
UCloudHTTPWriteParam wp = {f : NULL, os : &data_stream};
UCloudHTTPHeaderParam hp = {f : NULL, os : &header_stream};
ret = m_http->RoundTrip(&rp, &wp, &hp);
if (ret) {
UFILE_SET_ERROR2(ERR_CPPSDK_SEND_HTTP, UFILE_LAST_ERRMSG());
return ERR_CPPSDK_SEND_HTTP;
}

//解析回应
long code = 200;
ret = m_http->ResponseCode(&code);
if (ret) {
UFILE_SET_ERROR(ERR_CPPSDK_CURL);
return ERR_CPPSDK_CURL;
}

std::string errmsg;
if (code != 200) {
int parse_ret = UFileErrorRsp(oss.str().c_str(), &ret, errmsg);
if (parse_ret) {
UFILE_SET_ERROR(ERR_CPPSDK_CLIENT_INTERNAL);
return ERR_CPPSDK_CLIENT_INTERNAL;
}
UFILE_SET_ERROR2(ret, errmsg);
} else {
ret = ParseMuploadResult(oss.str(), hss.str());
if (ret) {
return ret;
}
m_uploaded_size += len;
}
return ret;
}

int UFileMput::MUploadCopyPart(ssize_t blk_idx, std::string src_bucket_name,
std::string src_object, size_t offset,
size_t length, std::string mimetype) {
Expand Down
2 changes: 1 addition & 1 deletion ucloud/include/ufile-cppsdk/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extern std::string UCLOUD_HOST_SUFFIX;

int InitGlobalConfig();

#define USERAGENT ("UFile C++SDK/1.1.1")
#define USERAGENT ("UFile C++SDK/1.1.2")

/*
* 动态修改配置
Expand Down
9 changes: 9 additions & 0 deletions ucloud/include/ufile-cppsdk/mput.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ class UFileMput : public APIBase {
*/
int MUpload(ssize_t blk_idx = -1);

/*
* @brief: 上传分片
* @blk_idx: 分片编号
* @data: 要上传的分片数据
* @len: 分片数据的长度
* @return: 0=成功,非0=失败
*/
int MUpload(ssize_t blk_idx, const char *data, size_t len);

/*
* @brief: copy文件分片
* @blk_idx: 分片编号
Expand Down
10 changes: 10 additions & 0 deletions ucloud/include/ufile-cppsdk/stream_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ostream>
#include <string>
#include <vector>
#include <streambuf>

namespace ucloud {
namespace cppsdk {
Expand Down Expand Up @@ -123,6 +124,15 @@ class MultiWriteStream : public UCloudOStream {
std::vector<UCloudOStream *> m_osv;
};

class UCloudStreamBuf : public std::streambuf {
public:
UCloudStreamBuf(const char *data, size_t size) {
char *base = const_cast<char *>(data);
// 设置左右边界以及读指针位置
setg(base, base, base + size);
}
};

} // namespace utils
} // namespace cppsdk
} // namespace ucloud
Expand Down