Facing issue while consuming Azure Storage Table REST Service . Even Resource is present in my storage account while on consuming service (Insert Entity) facing error "The specified resource does not exist" with 400 Response status.
I tried with Jersy restful API for consuming AZURE Storage Service API. As per microsoft documnetation i tried to consume the service. Doc Link : https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key
2) I tried for GET operation too...in that case Response return 400 : reason=The value for one of the HTTP headers is not in the correct format.
3) Same tried with POSTMAN too by that time also facing the same response as above said.
import java.net.*;
import java.util.*;
import java.text.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.codec.binary.Base64;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class InsertEntity_new {
private static Base64 base64 = new Base64();
public static void signRequestSK(WebTarget target, String account, String key, Client client) throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = fmt.format(Calendar.getInstance().getTime()) + " GMT";
StringBuilder sb = new StringBuilder();
sb.append("POST\n"); // method
sb.append('\n'); // md5 (optional)
sb.append('\n'); // content type
sb.append('\n'); // legacy date
sb.append("x-ms-date:" + date + '\n'); // headers
sb.append("x-ms-version:2009-09-19\n");
sb.append("/" + account + request.getURL().getPath()); //CanonicalizedResource
// System.out.println(sb.toString());
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(base64.decode(key), "HmacSHA256"));
String authKey = new String(base64.encode(mac.doFinal(sb.toString().getBytes("UTF-8"))));
String auth = "SharedKeyLite " + account + ":" + authKey;
client.property("x-ms-date", date);
client.property("Authorization", auth);
client.property("x-ms-version", "2009-09-19");
String jsonInString = "{\"PartitionKey\":\"mypartitionkey\",\"RowKey\":\"001\",\"Email\":\"[email protected]\",\"PhoneNumber\":\"908265370\"}";
Response resp = target.request(MediaType.APPLICATION_JSON).post(Entity.entity(jsonInString, MediaType.APPLICATION_JSON));
System.out.println("Response" + resp);
}
public static void main(String args[]) throws Exception {
String account = "storageacctname";
String key = "storageaccountkey";
Client client = ClientBuilder.newClient();
WebTarget target = client.target("https://" + account + ".table.core.windows.net/mytable");
signRequestSK(target, account, key,client);
}
}
I expect my program needs to insert entity to my storage table.