Summary
Log Service (LOG for short) is a one-stop service for LOG data, which is tempered in Alibaba group through a large number of big data scenarios. Besides operating through the management console, LOG also provides API (Application Programming Interface) to write and query LOG data, manage their own projects and LOG libraries. In the actual development and use process, we recommend that users use the latest version of SDK provided by the official. Using the Rest API directly is relatively troublesome, especially in the process of signature generation, it is easy to make mistakes and is not easy to troubleshoot. The following mainly introduces the use examples of three APIs: CreateProject, DeleteProject and ListLogstore. JAVA language is used for relevant examples.
Dependent dependence
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4</version> </dependency>
Sample code
CreateProject
import net.sf.json.JSONObject; import org.apache.commons.codec.binary.Base64; import java.math.BigInteger; import java.net.URI; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.TimeZone; //Create project operation public class CreateProject { public static void main(String[] args){ //Get GMT English format time Date d=new Date(); DateFormat format=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",Locale.ENGLISH);//You need to bring Locale.ENGLISH to the time conversion of English format, otherwise the conversion will fail, because the default setting is localization. Unless your operating system is English, in short, the time format and mode should be consistent during the time conversion. format.setTimeZone(TimeZone.getTimeZone("GMT")); String date = format.format(d); //Parameter setting String projectName = "restapiproject";//Log service item name String projectDescription = "sample_demo";//Log service item description String AccessKeySecret = "********";//Access Key Secret String AccessKeyId = "********";//AccessKey ID JSONObject jsonBody = new JSONObject(); jsonBody.put("projectName", projectName); jsonBody.put("description", projectDescription); //Construct request body String body = jsonBody.toString(); System.out.println("jsonBody: " + jsonBody.toString()); String md5 = md5(body).toUpperCase(); md5 = md5.toUpperCase();//Lowercase to uppercase //Construct signature String String SignString = "POST\n" + md5+"\n"+ "application/json\n" + date + "\n"+ "x-log-apiversion:0.6.0\n" + "x-log-bodyrawsize:0\n" + "x-log-signaturemethod:hmac-sha1\n" + "/"; String sign = encode(AccessKeySecret,SignString); String Authorization = "LOG " + AccessKeyId + ":" + sign; HttpClient httpclient = HttpClients.createDefault(); try { URIBuilder builder = new URIBuilder("http://"+ projectName+".cn-qingdao.log.aliyuncs.com "); / / create a Project in Qingdao URI uri = builder.build(); HttpPost request = new HttpPost(uri); request.setHeader("Authorization", Authorization); request.setHeader("Date", date); request.setHeader("Host", projectName + ".cn-qingdao.log.aliyuncs.com"); request.setHeader("x-log-apiversion", "0.6.0"); request.setHeader("x-log-signaturemethod", "hmac-sha1"); request.setHeader("Content-Type","application/json"); request.setHeader("User-Agent","sls-java-sdk-v-0.6.1"); request.setHeader("x-log-bodyrawsize","0"); request.setHeader("Content-MD5", md5); // Request body StringEntity reqEntity = new StringEntity(body,"UTF-8"); request.setEntity(reqEntity); HttpResponse response = httpclient.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("Project created successfully!"); System.out.println(EntityUtils.toString(entity)); } } catch (Exception e) { System.out.println("error"); System.out.println(e.getMessage()); } } //Write an md5 encryption method public static String md5(String plainText) { //Define a byte array byte[] secretBytes = null; try { // Generate an MD5 encryption calculation summary MessageDigest md = MessageDigest.getInstance("MD5"); //Encrypt string md.update(plainText.getBytes()); //Get encrypted data secretBytes = md.digest(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("No, md5 This algorithm!"); } //Convert encrypted data to hexadecimal numbers String md5code = new BigInteger(1, secretBytes).toString(16);// Hexadecimal number // If the generated number is less than 32 bits, fill 0 in front for (int i = 0; i < 32 - md5code.length(); i++) { md5code = "0" + md5code; } return md5code; } //Computational signature public static String encode(String accessKey, String data) { try { byte[] keyBytes = accessKey.getBytes("UTF-8"); byte[] dataBytes = data.getBytes("UTF-8"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(keyBytes, "HmacSHA1")); return new String(Base64.encodeBase64(mac.doFinal(dataBytes))); } catch (UnsupportedEncodingException var5) { throw new RuntimeException("Not supported encoding method UTF-8", var5); } catch (NoSuchAlgorithmException var6) { throw new RuntimeException("Not supported signature method hmac-sha1", var6); } catch (InvalidKeyException var7) { throw new RuntimeException("Failed to calculate the signature", var7); } } }
Delete Project
import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.net.URI; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.TimeZone; //Delete project operation public class DeleProject { public static void main(String[] args){ //Get GMT English format time Date d=new Date(); DateFormat format=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",Locale.ENGLISH);//You need to bring Locale.ENGLISH to the time conversion of English format, otherwise the conversion will fail, because the default setting is localization. Unless your operating system is English, in short, the time format and mode should be consistent during the time conversion. format.setTimeZone(TimeZone.getTimeZone("GMT")); String date = format.format(d); String AccessKeySecret = "********"; String AccessKeyId = "********"; String projectName = "restapiproject";//Deleted project name String SignString = "DELETE\n" + "\n"+ "application/x-protobuf\n" + date + "\n"+ "x-log-apiversion:0.6.0\n" + "x-log-bodyrawsize:0\n" + "x-log-signaturemethod:hmac-sha1\n"+"/"; String sign = encode(AccessKeySecret,SignString); String Authorization = "LOG " + AccessKeyId + ":" + sign; HttpClient httpclient = HttpClients.createDefault(); try { URIBuilder builder = new URIBuilder("http://"+projectName+".cn-qingdao.log.aliyuncs.com/"); URI uri = builder.build(); HttpDelete request = new HttpDelete(uri); request.setHeader("Authorization", Authorization); request.setHeader("Date", date); request.setHeader("Host", projectName + ".cn-qingdao.log.aliyuncs.com"); request.setHeader("x-log-apiversion", "0.6.0"); request.setHeader("x-log-signaturemethod", "hmac-sha1"); request.setHeader("Content-Type","application/x-protobuf"); request.setHeader("User-Agent","sls-java-sdk-v-0.6.1"); request.setHeader("x-log-bodyrawsize","0"); HttpResponse response = httpclient.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } catch (Exception e) { System.out.println(e.getMessage()); } } //Write an md5 encryption method public static String md5(String plainText) { //Define a byte array byte[] secretBytes = null; try { // Generate an MD5 encryption calculation summary MessageDigest md = MessageDigest.getInstance("MD5"); //Encrypt string md.update(plainText.getBytes()); //Get encrypted data secretBytes = md.digest(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("No, md5 This algorithm!"); } //Convert encrypted data to hexadecimal numbers String md5code = new BigInteger(1, secretBytes).toString(16);// Hexadecimal number // If the generated number is less than 32 bits, fill 0 in front for (int i = 0; i < 32 - md5code.length(); i++) { md5code = "0" + md5code; } return md5code; } public static String encode(String accessKey, String data) { try { byte[] keyBytes = accessKey.getBytes("UTF-8"); byte[] dataBytes = data.getBytes("UTF-8"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(keyBytes, "HmacSHA1")); return new String(Base64.encodeBase64(mac.doFinal(dataBytes))); } catch (UnsupportedEncodingException var5) { throw new RuntimeException("Not supported encoding method UTF-8", var5); } catch (NoSuchAlgorithmException var6) { throw new RuntimeException("Not supported signature method hmac-sha1", var6); } catch (InvalidKeyException var7) { throw new RuntimeException("Failed to calculate the signature", var7); } } }
ListLogstore
import org.apache.commons.codec.binary.Base64; import java.net.URI; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.TimeZone; //List the log libraries under the specified Project public class GetProjectLogstores { public static void main(String[] args){ //Get GMT English format time Date d=new Date(); DateFormat format=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",Locale.ENGLISH);//You need to bring Locale.ENGLISH to the time conversion of English format, otherwise the conversion will fail, because the default setting is localization. Unless your operating system is English, in short, the time format and mode should be consistent during the time conversion. format.setTimeZone(TimeZone.getTimeZone("GMT")); String date = format.format(d); //Parameter setting String projectName = "taroslproject";//entry name String AccessKeySecret = "********"; String AccessKeyId = "********"; String SignString = "GET\n" + "\n" + "application/x-protobuf\n" + date + "\n"+ "x-log-apiversion:0.6.0\n" + "x-log-bodyrawsize:0\n" + "x-log-signaturemethod:hmac-sha1\n" + "/logstores?logstoreName=&offset=0&size=100"; String sign = encode(AccessKeySecret,SignString); String Authorization = "LOG " + AccessKeyId + ":" + sign; HttpClient httpclient = HttpClients.createDefault(); try { URIBuilder builder = new URIBuilder("http://"+projectName+".cn-qingdao.log.aliyuncs.com/logstores"); builder.setParameter("offset", "0"); builder.setParameter("size", "100"); builder.setParameter("logstoreName", ""); URI uri = builder.build(); HttpGet request = new HttpGet(uri); request.setHeader("Authorization", Authorization); request.setHeader("Date", date); request.setHeader("Host", projectName + ".cn-qingdao.log.aliyuncs.com"); request.setHeader("x-log-apiversion", "0.6.0"); request.setHeader("x-log-signaturemethod", "hmac-sha1"); request.setHeader("Content-Length","0"); request.setHeader("Content-Type","application/x-protobuf"); request.setHeader("User-Agent","sls-java-sdk-v-0.6.1"); request.setHeader("x-log-bodyrawsize","0"); HttpResponse response = httpclient.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } catch (Exception e) { System.out.println(e.getMessage()); } } public static String encode(String accessKey, String data) { try { byte[] keyBytes = accessKey.getBytes("UTF-8"); byte[] dataBytes = data.getBytes("UTF-8"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(keyBytes, "HmacSHA1")); return new String(Base64.encodeBase64(mac.doFinal(dataBytes))); } catch (UnsupportedEncodingException var5) { throw new RuntimeException("Not supported encoding method UTF-8", var5); } catch (NoSuchAlgorithmException var6) { throw new RuntimeException("Not supported signature method hmac-sha1", var6); } catch (InvalidKeyException var7) { throw new RuntimeException("Failed to calculate the signature", var7); } } }
proposal
In the actual use process, when you have to use the Rest API directly, if you only refer to the API documentation, it is difficult to troubleshoot when there is a problem in the test. At present, the Java language SDK implements all the APIs. It is recommended to write your own function referring to the source code of the Java SDK.