我已成功创建容器,但未能使用Rest API将图像/pdf上载到我的blob容器



我正在使用PUT Blob Rest API上传img/pdf-PUT Blob引用。但它显示-服务器无法对请求进行身份验证。确保Authorization标头的值正确形成,包括签名错误(403响应代码)。

下面是要签名的字符串:

"PUT \n\n\n41676\n\nmage/png\n\n\n\n\n\n\nx ms blob类型:BlockBlob\nx ms日期:2021年2月17日星期三10:24:02 GMT\nx ms版本:2020-02-10\n/acc_name/container_name/12362_50800_13_s_1.png">

以下是标题详细信息:

{"x-ms-blob-type":"BlockBlob","x-ms-date":"2021年2月17日星期三10:24:02 GMT","x-ms-version":"2020-02-10","Authorization":"SharedKey acc_name:XXXQR728D7aqyJzFi/HXk+qT9rpjYbplUG9KXXXX","Content type":"图像/png","内容长度":"41676"}

以下是URL详细信息:

https://acc_name.blob.core.windows.net/container_name/12362_50800_13_s_1.png

private BasicDBObject createAzureBlob(String storageAPIVersion, String storageAccURL, String storageAccName, String storageAcckey, String storageAccConString,
String containerName, BasicDBObject clientReqObj, File outputfile) throws Exception
{
log.info("Entry createAzureBlob in the GenericServicesDeligator");
BasicDBObject clientResponse = null;
String date = clientReqObj.remove(Constants.F_CREATED_DATE).toString();
String filePath = clientReqObj.remove(Constants.F_PATH).toString();
String contentLength = clientReqObj.remove("Content-Length").toString();
String contentType = clientReqObj.remove("Content-Type").toString();

String hostMethod = "PUT";
StringBuilder authorization = new StringBuilder("SharedKey ").append(storageAccName).append(":").append("sharedKeySign");
StringBuilder canonicalizedResource = new StringBuilder("/").append(storageAccName).append("/").append(containerName).append("/").append(filePath);

BasicDBObject clientReqHeader = new BasicDBObject();
// Host URL
String hostName = clientReqHeader.size()>0l?new StringBuilder("/").append(filePath).append(prepareHostInfo(clientReqHeader)).toString():new StringBuilder("/").
append(filePath).toString();
String hostURL = new StringBuilder(storageAccURL).append("/").append(containerName).append(hostName).toString().trim();

// canonicalizedResource should always be in alphabetical order
canonicalizedResource.append(prepareCanonicalizedResource(clientReqHeader));
//canonicalizedHeader should always be in alphabetical order and include only that begin with x-ms-
clientReqHeader.clear();
clientReqHeader.put("x-ms-blob-type", "BlockBlob");               
clientReqHeader.put("x-ms-date", date);
clientReqHeader.put("x-ms-version", storageAPIVersion);
String canonicalizedHeader = prepareCanonicalizedHeader(clientReqHeader);
// string to sign
String stringToSign = prepareStringToSign(hostMethod, contentLength, contentType, canonicalizedHeader, canonicalizedResource.toString());

// encode the string by using the HMAC-SHA256 algorithm over the UTF-8-encoded signature string
String signature = encodeSharedKeySignature(stringToSign, storageAcckey);
clientReqHeader.put("Authorization", authorization.toString().replace("sharedKeySign", signature));
clientReqHeader.put("Content-Length", contentLength);
clientReqHeader.put("Content-Type", contentType);

clientResponse = sendAzureClientRequest(hostMethod, hostURL, clientReqHeader, clientReqObj, outputfile);
log.info("Exit createAzureBlob in the GenericServicesDeligator");
return clientResponse;

}

private String prepareHostInfo(BasicDBObject hostNameObject) throws Exception
{
StringBuilder hostName = new StringBuilder();

for(String key: hostNameObject.keySet())
{
if(hostName.length() == 0l)
hostName.append("?").append(key).append("=").append(hostNameObject.get(key));
else
hostName.append("&").append(key).append("=").append(hostNameObject.get(key));
}

return hostName.toString().trim();
}
private String prepareCanonicalizedResource(BasicDBObject hostNameObject) throws Exception
{
StringBuilder canonicalizedResource = new StringBuilder();

Map<String, String> canonicalizedMap = new TreeMap<String, String>();

canonicalizedMap.putAll(hostNameObject.toMap());
for(String key: canonicalizedMap.keySet())
{
canonicalizedResource.append("n").append(key).append(":").append(canonicalizedMap.get(key));
}

return canonicalizedResource.toString();
}
private String prepareCanonicalizedHeader(BasicDBObject hostNameObject) throws Exception
{
StringBuilder canonicalizedHeader = new StringBuilder();

Map<String, String> canonicalizedMap = new TreeMap<String, String>();

canonicalizedMap.putAll(hostNameObject.toMap());
for(String key: canonicalizedMap.keySet())
{
canonicalizedHeader.append(key).append(":").append(canonicalizedMap.get(key).toString().trim()).append("n");
}

return canonicalizedHeader.toString();
}
private String prepareStringToSign(String hostMethod, String contentLength, String contentType, String canonicalizedHeader, String canonicalizedResource) 
{
String input = "";

input =  new StringBuilder(hostMethod).append("n").
append("n").                                                           // Content-Encoding + "n" +
append("n").                                                           // Content-Language + "n" +
append(contentLength!=null?contentLength:"").append("n").
append("n").                                                           // Content-MD5 + "n" +
append(contentType!=null?contentType:"").append("n"). 
append("n").                                                           // Date
append("n").                                                           // If-Modified-Since + "n" +
append("n").                                                           // If-Match + "n" +
append("n").                                                           // If-None-Match + "n" +
append("n").                                                           // If-Unmodified-Since + "n" +
append("n").                                                           // Range + "n" +
append(canonicalizedHeader).
append(canonicalizedResource).toString().trim();

return input;
}
public String getUTCDate(Date date) throws Exception
{
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

return (sdf.format(date) + " GMT").trim();
}
public static String getFileContentType(String fileType)
{
String contentType = "";
switch (fileType)
{
case "pdf":
contentType = "application/pdf";
break;
case "txt":
contentType = "text/plain";
break;
case "bmp":
contentType = "image/bmp";
break;
case "gif":
contentType = "image/gif";
break;
case "png":
contentType = "image/png";
break;
case "jpg":
contentType = "image/jpeg";
break;
case "jpeg":
contentType = "image/jpeg";
break;
case "xls":
contentType = "application/vnd.ms-excel";
break;
case "xlsx":
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case "csv":
contentType = "text/csv";
break;
case "html":
contentType = "text/html";
break;
case "xml":
contentType = "text/xml";
break;
case "zip":
contentType = "application/zip";
break;
default:
contentType = "application/octet-stream";
break;
}
return contentType.trim();
}

private String encodeSharedKeySignature(String inputVal, String storageAcckey) throws Exception
{
SecretKeySpec secretkey = new SecretKeySpec(new BASE64Decoder().decodeBuffer(storageAcckey), "HmacSHA256");

Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretkey);
String hash = Base64.encodeAsString(mac.doFinal(inputVal.getBytes("UTF-8")));

return hash.trim();
}

请分享解决方案,以便我上传图片/pdf。提前感谢

private BasicDBObject createAzureBlob()方法中,在以下部分中:

//encode the string by using the HMAC-SHA256 algorithm over the UTF-8-encoded signature string
String signature = encodeSharedKeySignature(stringToSign, storageAcckey);
clientReqHeader.put("Authorization", authorization.toString().replace("sharedKeySign", storageAcckey));
clientReqHeader.put("Content-Length", contentLength);
clientReqHeader.put("Content-Type", contentType);

您应该使用signature而不是storageAcckey作为Authorization标头,所以请更改这行代码

clientReqHeader.put("Authorization", authorization.toString().replace("sharedKeySign", storageAcckey));

clientReqHeader.put("Authorization", authorization.toString().replace("sharedKeySign", signature));

最新更新