-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'QSyncUi/main'
- Loading branch information
Showing
4 changed files
with
271 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
#include "cosclient.h" | ||
#include "qeventloop.h" | ||
#include <QUrlQuery> | ||
#include <QNetworkReply> | ||
COSClient::COSClient(QObject *parent) | ||
{ | ||
return ; | ||
} | ||
|
||
COSClient::COSClient(QString bucketName, QString appId, QString region, QString secretId, QString secretKey, QString token, QDateTime expiredTime, QObject *parent) | ||
{ | ||
this->bucketName = bucketName; | ||
this->appId = appId; | ||
this->region = region; | ||
this->secretId = secretId; | ||
this->secretKey = secretKey; | ||
this->token = token; | ||
this->expiredTime = expiredTime; | ||
this->endpoint = bucketName + "-" + appId + ".cos." + region + ".myqcloud.com"; | ||
this->generalApiUrl = "https://" + endpoint; | ||
this->signHelper = new SignHelper(appId, secretId, secretKey); | ||
manager = new QNetworkAccessManager(this); | ||
} | ||
|
||
QString COSClient::listObjects(const QString &prefix, const QString &delimiter) | ||
{ | ||
QMap<QString, QString> queryParams; | ||
queryParams.insert("prefix", prefix); | ||
queryParams.insert("delimiter", delimiter); | ||
return invokeGetRequest("/", queryParams); | ||
} | ||
|
||
QString COSClient::invokeGetRequest(const QString &path, const QMap<QString, QString> queryParams) | ||
{ | ||
QNetworkRequest request = buildGetRequest(path, queryParams); | ||
qDebug()<<request.url(); | ||
QNetworkReply *reply = manager->get(request); | ||
QEventLoop loop; | ||
connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | ||
loop.exec(); | ||
return reply->readAll(); | ||
} | ||
|
||
bool COSClient::invokePutRequest(const QString &path, const QMap<QString, QString> queryParams, const QByteArray &data) | ||
{ | ||
QNetworkRequest request = buildPutRequest(path, queryParams,data); | ||
QNetworkReply *reply = manager->put(request, data); | ||
QEventLoop loop; | ||
connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | ||
loop.exec(); | ||
return reply->error()==QNetworkReply::NoError; | ||
} | ||
|
||
bool COSClient::invokeHeadRequest(const QString &path, const QMap<QString, QString> queryParams) | ||
{ | ||
QNetworkRequest request = buildHeadRequest(path, queryParams); | ||
QNetworkReply *reply = manager->head(request); | ||
QEventLoop loop; | ||
connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | ||
loop.exec(); | ||
return reply->error()==QNetworkReply::NoError; | ||
} | ||
|
||
bool COSClient::invokeDeleteRequest(const QString &path, const QMap<QString, QString> queryParams) | ||
{ | ||
QNetworkRequest request = buildDeleteRequest(path, queryParams); | ||
QNetworkReply *reply = manager->deleteResource(request); | ||
QEventLoop loop; | ||
connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | ||
loop.exec(); | ||
return reply->error()==QNetworkReply::NoError; | ||
} | ||
|
||
bool COSClient::preCheckSession() | ||
{ | ||
if (token == "") | ||
{ | ||
return true; | ||
} | ||
if (expiredTime < QDateTime::currentDateTime()) | ||
{ | ||
qDebug() << expiredTime; | ||
qDebug() << QDateTime::currentDateTime(); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
QNetworkRequest COSClient::buildGetRequest(const QString &path, const QMap<QString, QString> queryParams) | ||
{ | ||
if (!preCheckSession()) | ||
{ | ||
return QNetworkRequest(); | ||
} | ||
QUrl url; | ||
QUrlQuery query; | ||
url=QUrl(generalApiUrl+path); | ||
for(auto it=queryParams.begin();it!=queryParams.end();it++) | ||
{ | ||
query.addQueryItem(it.key(),it.value()); | ||
} | ||
qDebug()<<url; | ||
url.setQuery(query); | ||
qDebug()<<url; | ||
QNetworkRequest request(url); | ||
qDebug()<<request.url(); | ||
request.setRawHeader("Host",endpoint.toUtf8()); | ||
//设置请求方法 | ||
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"GET"); | ||
QString sig=this->signHelper->generateSignature(request,300); | ||
request.setRawHeader("Authorization",sig.toUtf8()); | ||
if(token!="") | ||
{ | ||
request.setRawHeader("x-cos-security-token",token.toUtf8()); | ||
} | ||
return request; | ||
|
||
} | ||
|
||
QNetworkRequest COSClient::buildPutRequest(const QString &path, const QMap<QString, QString> queryParams, const QByteArray &data) | ||
{ | ||
if (!preCheckSession()) | ||
{ | ||
return QNetworkRequest(); | ||
} | ||
QUrl url; | ||
QUrlQuery query; | ||
url=QUrl(generalApiUrl+path); | ||
for(auto it=queryParams.begin();it!=queryParams.end();it++) | ||
{ | ||
query.addQueryItem(it.key(),it.value()); | ||
} | ||
url.setQuery(query); | ||
QNetworkRequest request(url); | ||
request.setRawHeader("Host",endpoint.toUtf8()); | ||
//设置请求方法 | ||
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"PUT"); | ||
QString sig=this->signHelper->generateSignature(request,300); | ||
request.setRawHeader("Authorization",sig.toUtf8()); | ||
if(token!="") | ||
{ | ||
request.setRawHeader("x-cos-security-token",token.toUtf8()); | ||
} | ||
return request; | ||
} | ||
|
||
QNetworkRequest COSClient::buildHeadRequest(const QString &path, const QMap<QString, QString> queryParams) | ||
{ | ||
if (!preCheckSession()) | ||
{ | ||
return QNetworkRequest(); | ||
} | ||
QUrl url; | ||
QUrlQuery query; | ||
url=QUrl(generalApiUrl+path); | ||
for(auto it=queryParams.begin();it!=queryParams.end();it++) | ||
{ | ||
query.addQueryItem(it.key(),it.value()); | ||
} | ||
url.setQuery(query); | ||
QNetworkRequest request(url); | ||
request.setRawHeader("Host",endpoint.toUtf8()); | ||
//设置请求方法 | ||
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"HEAD"); | ||
QString sig=this->signHelper->generateSignature(request,300); | ||
request.setRawHeader("Authorization",sig.toUtf8()); | ||
if(token!="") | ||
{ | ||
request.setRawHeader("x-cos-security-token",token.toUtf8()); | ||
} | ||
return request; | ||
} | ||
|
||
QNetworkRequest COSClient::buildDeleteRequest(const QString &path, const QMap<QString, QString> queryParams) | ||
{ | ||
if (!preCheckSession()) | ||
{ | ||
return QNetworkRequest(); | ||
} | ||
QUrl url; | ||
QUrlQuery query; | ||
url=QUrl(generalApiUrl+path); | ||
for(auto it=queryParams.begin();it!=queryParams.end();it++) | ||
{ | ||
query.addQueryItem(it.key(),it.value()); | ||
} | ||
url.setQuery(query); | ||
QNetworkRequest request(url); | ||
request.setRawHeader("Host",endpoint.toUtf8()); | ||
//设置请求方法 | ||
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"DELETE"); | ||
QString sig=this->signHelper->generateSignature(request,300); | ||
request.setRawHeader("Authorization",sig.toUtf8()); | ||
if(token!="") | ||
{ | ||
request.setRawHeader("x-cos-security-token",token.toUtf8()); | ||
} | ||
return request; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#ifndef COSCLIENT_H | ||
#define COSCLIENT_H | ||
#include "qdatetime.h" | ||
#include "signhelper.h" | ||
#include <QString> | ||
#include <QObject> | ||
#include <QtNetwork/QNetworkAccessManager> | ||
#include <QtNetwork/QNetworkRequest> | ||
#include <QtNetwork/QNetworkReply> | ||
#include <QUrl> | ||
class COSClient : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
COSClient(QObject *parent = nullptr); | ||
COSClient(QString bucketName, QString appId, QString region, QString secretId, QString secretKey, QString token, QDateTime expiredTime,QObject *parent = nullptr); | ||
QString getBucketName() const { return bucketName; } | ||
QString getAppId() const { return appId; } | ||
QString getRegion() const { return region; } | ||
QString getSecretId() const { return secretId; } | ||
QString getSecretKey() const { return secretKey; } | ||
QString getEndpoint() const { return endpoint; } | ||
QString getGeneralApiUrl() const { return generalApiUrl; } | ||
QString getToken() const { return token; } | ||
QDateTime getExpiredTime() const { return expiredTime; } | ||
void setBucketName(const QString &value) { bucketName = value; } | ||
void setAppId(const QString &value) { appId = value; } | ||
void setRegion(const QString &value) { region = value; } | ||
void setNewSession(QString secretId, QString secretKey, QString token, QDateTime expiredTime){ | ||
this->signHelper = new SignHelper(appId, secretId, secretKey); | ||
this->secretId = secretId; | ||
this->secretKey = secretKey; | ||
this->token = token; | ||
this->expiredTime = expiredTime; | ||
} | ||
QString listObjects(const QString &prefix, const QString &delimiter); | ||
|
||
private: | ||
QString bucketName; | ||
QString appId; | ||
QString region; | ||
QString secretId; | ||
QString secretKey; | ||
QString endpoint; | ||
QString generalApiUrl; | ||
QString token; | ||
QDateTime expiredTime; | ||
SignHelper* signHelper; | ||
QNetworkAccessManager* manager; | ||
bool preCheckSession(); | ||
QNetworkRequest buildGetRequest(const QString& path,const QMap<QString, QString> queryParams); | ||
QNetworkRequest buildPutRequest(const QString& path,const QMap<QString, QString> queryParams, const QByteArray& data); | ||
QNetworkRequest buildHeadRequest(const QString& path,const QMap<QString, QString> queryParams); | ||
QNetworkRequest buildDeleteRequest(const QString& path,const QMap<QString, QString> queryParams); | ||
QString invokeGetRequest(const QString& path,const QMap<QString, QString> queryParams); | ||
bool invokePutRequest(const QString& path,const QMap<QString, QString> queryParams, const QByteArray& data); | ||
bool invokeHeadRequest(const QString& path,const QMap<QString, QString> queryParams); | ||
bool invokeDeleteRequest(const QString& path,const QMap<QString, QString> queryParams); | ||
}; | ||
#endif // COSCLIENT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters