2010年7月22日 星期四

[Note] Google Storage API 開發二三事

Google Storage API
GS 提供了RESTful的介面供開發者使用

GET Service—lists all of the buckets that you own.
PUT Bucket—creates a bucket and changes the permissions on a bucket.
GET Bucket—lists the contents of a bucket or retrieves the ACLs that are applied to a bucket.
DELETE Bucket—deletes an empty bucket.
GET Object—downloads an object or retrieves the ACLs that are applied to an object.
PUT Object—uploads an object or applies new ACLs to an object.
DELETE Object—deletes an object.
HEAD Object—lists the metadata for an object.
POST Object—uploads an object by using HTML forms.

而目前網路上提供的資源如下:

Boto: Python interface to GS and S3
CloudBerry: Windows的檔案管理工具
Cyberduck : Mac上的browser
Dasein Cloud: open source Java API(如JDBC)
Data Nucleus : JDO/JPA plug in
Gladinet: maps network drives and provides AFS suppor
gstorea :Ruby client library
SharpGsa C# library

時間格式的要求上 google storage API要求的時間格式為UTC,時間格式處理如下:

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ssz", new Locale("US"));
Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));

format.setCalendar(cal);
return format.format(new Date());


而Google Storage在每個request都需使用auth
Signature = Base64-Encoding-Of(HMAC-SHA1(UTF-8-Encoding-Of(YourGoogleStorageSecretKey, MessageToBeSigned)))

public static String makeHMACSHA(String msg) {
byte[] hmac = new byte[msg.length()];
SecureRandom sr = new SecureRandom();
byte[] keyBytes = new byte[20];
sr.nextBytes(keyBytes);

try {
//將google提供的Secret key以UTF-8的編碼取出
SecretKey sk = new SecretKeySpec(GS_SK.getBytes("utf-8"),
"HmacSHA1");
//使用HMAC-SHA1
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(sk);
//傳入UTF-8編碼的訊息
mac.update(msg.getBytes("utf-8"));
hmac = mac.doFinal();

} catch (Exception e) {
e.printStackTrace();
} finally {
//回傳Base64編碼過得HMAC-SHA1值
return new String(Base64.encodeBase64(hmac));

}

}

沒有留言: