天翼云对象存储(经典版)I型API参考-使用HttpURLConnection开发 |
产品推荐: 1、安全稳定的云服务器租用,2核/2G/5M仅37元,点击抢购>>>; 2、高防物理服务器20核/16G/50M/500G防御仅350元,点击抢购>>> 3、百度智能建站(五合一网站)仅880元/年,点击抢购>>> 模板建站(PC+手机站)仅480元/年,点击抢购>>> 4、阿里云服务器2核2G3M仅99元/年、2核4G5M仅199元/年,新老同享,点击抢购>>> 5、腾讯云服务器2核2G4M仅99元/年、新老同享,点击抢购>>> 点击这里注册天翼云特邀VIP帐号,立即体验天翼云对象存储>>> 天翼云对象存储(经典版)I型API参考-使用HttpURLConnection开发 package cn.ctyun.oos.sample; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Random; import java.util.TimeZone; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64;
public class HttpUrlConnectionSample { private static final int CONN_TIMEOUT = 10000; private static final int READ_TIMEOUT = 30000; private static final String DATE_STR = "EEE, d MMM yyyy HH:mm:ss 'GMT'"; private static final SimpleDateFormat DATE_FMT = new SimpleDateFormat(DATE_STR, Locale.ENGLISH); static { TimeZone gmt = TimeZone.getTimeZone("GMT"); DATE_FMT.setTimeZone(gmt); }
private final String host; private final int port; private final String ak; private final String sk;
public HttpUrlConnectionSample(String host, int port, String ak, String sk) { this.host = host; this.port = port; this.ak = ak; this.sk = sk; }
private String authorize(String httpVerb, String date, String bucket, String objectName) throws Exception { String stringToSign = httpVerb + "\n\n\n" + date + "\n/" + bucket + "/" + objectName; Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(sk.getBytes("UTF-8"), "HmacSHA1")); byte[] macResult = mac.doFinal(stringToSign.getBytes("UTF-8")); String signature = new String(Base64.encodeBase64(macResult), "UTF-8"); String authorization = "AWS " + ak + ":" + signature; return authorization; }
public void put(String bucket, String objName, byte[] data) throws Exception { String date = DATE_FMT.format(new Date()); String authorization = authorize("PUT", date, bucket, objName); URL url = new URL("http", host, port, "/" + bucket + "/" + objName); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(data.length); conn.setRequestProperty("Date", date); conn.setRequestProperty("Authorization", authorization); conn.setConnectTimeout(CONN_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); conn.setDoOutput(true); conn.setRequestMethod("PUT"); conn.connect(); try (OutputStream out = conn.getOutputStream()) { out.write(data); out.flush(); } int code = conn.getResponseCode(); System.out.println("code=" + code); }
public void get(String bucket, String objName) throws Exception { String date = DATE_FMT.format(new Date()); String authorization = authorize("GET", date, bucket, objName); URL url = new URL("http", host, port, "/" + bucket + "/" + objName); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setRequestProperty("Date", date); conn.setRequestProperty("Authorization", authorization); conn.setConnectTimeout(CONN_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); conn.setDoInput(true); conn.setRequestMethod("GET"); conn.connect(); int code = conn.getResponseCode(); System.out.println("code=" + code); try (InputStream in = conn.getInputStream()) { byte[] buffer = new byte[1024 * 8]; while (in.read(buffer) != -1) { } } }
public void delete(String bucket, String objName) throws Exception { String date = DATE_FMT.format(new Date()); String authorization = authorize("DELETE", date, bucket, objName); URL url = new URL("http", host, port, "/" + bucket + "/" + objName); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Date", date); conn.setRequestProperty("Authorization", authorization); conn.setConnectTimeout(CONN_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); conn.setRequestMethod("DELETE"); conn.connect(); int code = conn.getResponseCode(); System.out.println("code=" + code); }
public static void main(String[] args) throws Exception { String host = "oos-nm2.ctyunapi.cn"; int port = 80; String ak = "your_ak"; String sk = "your_sk"; String bucket = "bucket_name"; String objName = "object_name"; byte[] data = new byte[1024 * 1024 * 10]; Random rand = new Random(); rand.nextBytes(data); HttpUrlConnectionSample s = new HttpUrlConnectionSample(host, port, ak, sk); s.put(bucket, objName, data); s.get(bucket, objName); s.delete(bucket, objName); } } |