水曜日, 11月 10, 2010

rhinoから"Google Storage"にupload!

GSUtil というコマンドラインツールが用意されているものの、Python(?Rubyだっけ)なので使っていなかった、が、以下ソースでuploadに成功。
"WebOS Goodies"さんの情報を参考にしました。ありがとうございます!
importPackage(javax.crypto);
importPackage(javax.crypto.spec);
importPackage(org.apache.commons.codec.binary);

importPackage(org.apache.commons.httpclient);
importPackage(org.apache.commons.httpclient.methods);

var ACCESS_KEY = 'GOOG4C72MAOUMK7xxx3Z';
var SECRET     = 'xy4jTrxxxxuTZEtqUItfZIViVUF3xEKx4xxxp';
var HOST       = 'commondatastorage.googleapis.com';

var strDate    = getDate();
var strPath    = '/test/start.txt';

var headers = {
//  'Content-Length': '0',
'Content-Type': 'text/plain',
'Date': strDate,
    'User-Agent': 'Jakarta Commons-HttpClient/3.1',
    'Host': 'commondatastorage.googleapis.com'
};

// rhino + Java package/class
// http://d.hatena.ne.jp/terurou/20100918/1284791541

// http://www.fireproject.jp/feature/uzumi/httpclient/logging.html
java.lang.System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
java.lang.System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
java.lang.System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "debug");
java.lang.System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");

var httpclient = new HttpClient();

if(false){ 
var httpget = new GetMethod("http://commondatastorage.googleapis.com" + strPath); 
httpget.setRequestHeader(new Header("Date",strDate));
httpget.setRequestHeader(new Header("Authorization",getAuthHeader('GET', headers,strPath)));

var statusCode = httpclient.executeMethod(httpget);  
print(httpget.getStatusLine());
}else{
var httpput = new PutMethod("http://commondatastorage.googleapis.com" + '/tf0054'+strPath);
var jstrTmp = "Hello this is a test in one line that is longer 12345677890\n";

httpput.setRequestHeader(new Header("Authorization",getAuthHeader('PUT', headers, strPath)));
httpput.setRequestHeader(new Header("Date",strDate));
httpput.setRequestHeader(new Header("x-goog-acl","public-read"));
httpput.setRequestHeader(new Header("Content-Type","text/plain"));
httpput.setRequestHeader(new Header("Content-Length","" + jstrTmp.length));
httpput.setRequestBody(jstrTmp);

httpclient.executeMethod(httpput);
print(httpput.getResponseBodyAsString());
}

function getAuthHeader(strMethod, headers, strPath){

function getHmacSign(strTmp){
var strType = 'HmacSHA1';
var jstrTmp = new java.lang.String(strTmp);
var jstrSec = new java.lang.String(SECRET);
var mac = Mac.getInstance(strType);
mac.init(new SecretKeySpec(jstrSec.getBytes(), strType));
return new java.lang.String(Base64.encodeBase64(mac.doFinal(jstrTmp.getBytes())),"UTF-8");
}

var strHeaders = strMethod + "\n";
strHeaders += "\n"; // Content-MD5 value
for(var i in headers){
if(i.toLowerCase() == 'date' || i.toLowerCase() == 'content-type'){
strHeaders += headers[i] + "\n";
} else {
// strHeaders += i.toLowerCase()+':'+headers[i]+"\r\n";
}
}
strHeaders += "x-goog-acl:public-read\n";
strHeaders += '/tf0054' + strPath;
print(">"+strHeaders);
return 'GOOG1 ' + ACCESS_KEY + ':' + getHmacSign(strHeaders);
}

function getDate(){
var d = new Date(new Date().getTime());
return d.toGMTString().replace(/UTC/,'GMT');
}

適当なソースですが、ご参考までに。