Skip to content

Commit

Permalink
listAllMyBuckets Complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
reyoung committed Aug 15, 2012
1 parent 61e54cc commit 65413ac
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
46 changes: 46 additions & 0 deletions QAliOSS/ossapi.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "ossapi.h"
#include "utl.h"

namespace QAliOSS{
OSSApi::OSSApi(const QString &host, const QString &access_id, const QString &secret_access_key, QObject *parent) :
QObject(parent),m_host(host),m_accessId(access_id),m_secret_access_key(secret_access_key)
Expand Down Expand Up @@ -41,3 +43,47 @@ void OSSApi::setSecretAccessKey(const QString &k)


}

Response QAliOSS::OSSApi::listAllMyBuckets() const
{
QString method = "GET";
QString url = "/";
QString resource = "/";
QLocale loc(QLocale::English,QLocale::UnitedStates);
QString date =
loc.toString(QDateTime::currentDateTime().toUTC(),
"ddd, dd MMM yyyy hh:mm:ss")+" GMT";
QMap<QString,QString> headers;
headers.insert("Date",date);
headers.insert("Host",this->getHost());
if (!this->getAccessID().isEmpty()&&!this->getSecretAccessKey().isEmpty()){
headers.insert("Authorization",this->_createSignForNormalAuth(method,headers,resource));
} else if(!this->getAccessID().isEmpty()){
headers.insert("Authorization",this->getAccessID());
}

QHttpRequestHeader req(method,url);
QMapIterator<QString,QString> it(headers);
while(it.hasNext()){
it.next();
req.addValue(it.key(),it.value());
}
SyncHttp h;
bool ok = h.setHost(this->getHost(),80);
if(!ok){
Response retv;
retv.Ok = false;
return retv;
}
return h.syncRequest(req,"");
}

QString QAliOSS::OSSApi::_createSignForNormalAuth(const QString &method, const QMap<QString, QString> &headers, const QString &resource)const
{
return QString("OSS ")+
this->getAccessID()+":"
+QAliOSS::Utl::getAuthorizationCode(this->getSecretAccessKey(),
method,
headers,
resource);
}
10 changes: 10 additions & 0 deletions QAliOSS/ossapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QObject>
#include "QAliOSS_global.h"
#include "synchttp.h"
namespace QAliOSS{

class QALIOSSSHARED_EXPORT OSSApi : public QObject
Expand Down Expand Up @@ -30,10 +31,19 @@ class QALIOSSSHARED_EXPORT OSSApi : public QObject
void setSecretAccessKey(const QString& k);


Response listAllMyBuckets()const;

signals:

public slots:

private:
QString _createSignForNormalAuth(const QString& method,
const QMap<QString,QString>& headers=QMap<QString,QString>(),
const QString& resource="/")const;



private:
QString m_host;
QString m_accessId;
Expand Down
10 changes: 10 additions & 0 deletions TstQAliOSS/tst_tstqaliosstest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private Q_SLOTS:
void testConsForOSSApi();
void initTestCase();
void testSyncHttp();
void testListAllMyBuckets();
private:
QString accessId;
QString secretAccessKey;
Expand Down Expand Up @@ -97,4 +98,13 @@ void TstQAliOSSTest::testSyncHttp()
qDebug()<<r.Header.contentLength();
}

void TstQAliOSSTest::testListAllMyBuckets()
{
QAliOSS::OSSApi api("storage.aliyun.com",this->accessId,this->secretAccessKey);
Response res = api.listAllMyBuckets();
QVERIFY(res.Ok);
QVERIFY(res.Header.statusCode()/100 == 2);
qDebug()<<res.Body;
}

#include "tst_tstqaliosstest.moc"

0 comments on commit 65413ac

Please sign in to comment.