shrimpy's profileShrimpy`s spacePhotosBlogListsMore ![]() | Help |
|
||||||||||||||
|
July 03 What a amazing benifit.....Back then, when we were still a course work student.....the printing quota was around 1K every semester......we`ve already thought that was a lot.... however...after been a research assistance........what the hell.........unbelieveable... 10G internet quota....100K black and whit printing quota and 200 colour printing quota..... man....now..i felt a little bit regret...i should ask for a research master instead of course work at the very beginning... damn it...howcome there are so much different.....we pay for exact the same amount of tuition fee... ![]() June 22 A Mistake capture from MSN ah~~~~~~~~~~~~~~~~ look at the place the read arrow is pointing..... it should be some image AD...however.....MSN failed to parse it....must be the wrong script send from Microsoft..... ----------------------------------------------------------------------------------------------------------------------- Ok...i have to go back to study......fighting for my last exam.... June 12 终于。。拿回了点学费。。。June 03 曾经的大学, 我们也疯狂过。。。毕业的晚会。。。全班的cosplay。。。最多的掌声。。。还有最多的欢呼。。。 很快。。。我又要毕业了。。。。 (Part 1) http://video.qq.com/res/qqplayerout.swf?vid=1vrM0B70QVg (Part 2) http://video.qq.com/res/qqplayerout.swf?vid=1SCJz21YGtW May 16 Access Azure Blob Storage in Java Microsoft is not that friendly to other language`s programer... it is hard to find out resource how to access Azure storage in anohter language besides C# This article is show how to access Azure Blob Storage in java, although Steve Marx has already post a artiles on his blog months ago, but it is still hard to use for beginner. What i am going to do in this article is that: i have already got an container call "cantainer" under my account, i am going to put a blob call "abc" into the container. Let`s begin: Azure Storage is a REST base database, so i choose to use httpclient from apache, it is not a must that u have to use the same lib as mine... <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.0-beta2</version> </dependency> the hard thing when comunicate with azure storage is that, we have to sign our request for authorization There is a article from microsoft : http://msdn.microsoft.com/en-us/library/dd179428.aspx list what we should do to sign the request, e.g blob storage : StringToSign = VERB + "\n" +In Java : public static String ContentType = "Content-Type"; public static String X_MS_Date = "x-ms-date"; public static synchronized void Sign(HttpRequestBase request, String account, String key) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); fmt.setTimeZone(TimeZone.getTimeZone("GMT")); request.setHeader(X_MS_Date, fmt.format(Calendar.getInstance().getTime()) + " GMT"); StringBuilder sb = new StringBuilder(); sb.append(request.getMethod().toUpperCase() + "\n\n"); sb.append(request.getFirstHeader(ContentType).getValue() + "\n\n"); sb.append(X_MS_Date + ":" + request.getFirstHeader("x-ms-date").getValue() + "\n"); sb.append("/" + account + request.getURI().getPath()); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(Base64Coder.decode(key), "HmacSHA256")); String finalKey = new String(Base64Coder.encode(mac.doFinal(sb.toString().getBytes("UTF-8")))); request.setHeader("Authorization", "SharedKey " + account + ":" + finalKey); } HOWTO use it: public static void main(String args[]) throws Exception { HttpClient httpclient = new DefaultHttpClient(); HttpPut put = new HttpPut("http://" + YOUR_ACCOUNT + ".blob.core.windows.net/container/abc"); put.addHeader(PutBlob.ContentType, "text/plain"); // by default is binary, you should specify a qualify MINE TYPE put.setEntity(new StringEntity("Hello world!!", "UTF-8")); Sign(put, YOUR_ACCOUNT, YOUR_KEY); log.debug(EntityUtils.toString(httpclient.execute(put).getEntity())); } ====== Full Source Code Below ====== package org.unsw.eva.rest.azure; import java.util.Calendar; import java.util.TimeZone; import java.text.SimpleDateFormat; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.http.client.methods.HttpRequestBase; import org.unsw.eva.utils.Base64Coder; /** * * @author shrimpy */ public abstract class AbstractAzureStorageRestAction { public enum CONTENT_TYPE { TEXT_PLAIN("text/plain; charset=UTF-8"); private String value; private CONTENT_TYPE(String value) { this.value = value; } public String getValue() { return value; } } public static String ACCOUNT = "YOUR ACCOUNT HERE"; public static String KEY = "YOUR KEY HERE"; public static String REQUEST_URI = "http://" + ACCOUNT + ".blob.core.windows.net/container/abc"; public static String ContentType = "Content-Type"; public static String X_MS_Date = "x-ms-date"; public static synchronized void Sign(HttpRequestBase request, String account, String key) throws Exception { SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss"); fmt.setTimeZone(TimeZone.getTimeZone("GMT")); request.setHeader(X_MS_Date, fmt.format(Calendar.getInstance().getTime()) + " GMT"); StringBuilder sb = new StringBuilder(); sb.append(request.getMethod().toUpperCase() + "\n\n"); sb.append(request.getFirstHeader(ContentType).getValue() + "\n\n"); sb.append(X_MS_Date + ":" + request.getFirstHeader("x-ms-date").getValue() + "\n"); sb.append("/" + account + request.getURI().getPath()); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(Base64Coder.decode(key), "HmacSHA256")); String finalKey = new String(Base64Coder.encode(mac.doFinal(sb.toString().getBytes("UTF-8")))); request.setHeader("Authorization", "SharedKey " + account + ":" + finalKey); } } package org.unsw.eva.rest.azure; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author shrimpy */ public class PutBlob extends AbstractAzureStorageRestAction { private static final Logger log = LoggerFactory.getLogger(PutBlob.class); public static void main(String args[]) throws Exception { HttpClient httpclient = new DefaultHttpClient(); HttpPut put = new HttpPut("http://" + PutBlob.ACCOUNT + ".blob.core.windows.net/container/abc"); put.addHeader(PutBlob.ContentType, PutBlob.CONTENT_TYPE.TEXT_PLAIN.getValue()); put.setEntity(new StringEntity("Hello world!!---", "UTF-8")); Sign(put, PutBlob.ACCOUNT, PutBlob.KEY); log.debug(EntityUtils.toString(httpclient.execute(put).getEntity())); } } Reference : http://blog.smarx.com/posts/programming-language-interoperability-in-windows-azure |
Shrimpy`s spaceLife for rent ... 走过路过,记得留下点口水。。。呵呵
|
|||||||||||||
|
|