正在使用 REST API-invalid header 值 'Content-Length' 更新文件 service/Azure 中的图像
Updating images in File service/Azure using REST API-invalid header value 'Content-Length'
我正在尝试使用此处指定的 REST API 文档将图像上传到 Azure 中的文件服务:
https://docs.microsoft.com/en-us/rest/api/storageservices/put-range
视觉力页面:
<apex:page controller="azure_cls_cpy">
<apex:form >
<apex:inputFile value="{!img}" filename="{!fileName}" fileSize="{!fileSize}" ></apex:inputFile>
<apex:commandButton value="Click" action="{!createImage}"/><br/>
<apex:messages />
</apex:form>
</apex:page>
这是我的代码,它不起作用:
public void createImage(){
string storageKey = 'xxxxStorageKeyxxxx';
string storageName = '<storageName>';
Datetime dt = Datetime.now();
string formattedDate = dt.formatGMT('EEE, dd MMM yyyy HH:mm:ss')+ ' GMT';
string CanonicalizedHeaders = 'x-ms-date:'+formattedDate+'\nx-ms-range:bytes=0-'+string.valueOf(fileSize)+'\nx-ms-version:2016-05-31\nx-ms-write:update';
string CanonicalizedResource = '/' + storageName + '/<shareName>/<DirectoryName>/'+fileName+'\ncomp:range\ntimeout:20';
string StringToSign = 'PUT\n\n\nstring.valueOf(fileSize)\n\n\n\n\n\n\n\n\n' + CanonicalizedHeaders+'\n'+CanonicalizedResource;
system.debug('StringToSign--'+StringToSign);
Blob temp = EncodingUtil.base64Decode(storageKey);
Blob hmac = Crypto.generateMac('HmacSHA256',Blob.valueOf(StringToSign),temp ); //StringToSign
system.debug('oo-'+EncodingUtil.base64Encode(hmac));
HttpRequest req = new HttpRequest();
req.setMethod('PUT');
req.setHeader('x-ms-version','2016-05-31' );
req.setHeader('x-ms-date', formattedDate);
req.setHeader('Content-Length',string.valueOf(fileSize);
req.setHeader('x-ms-range','bytes=0-'+string.valueOf(fileSize));
req.setHeader('x-ms-write','update');
string signature = EncodingUtil.base64Encode(hmac);
string authHeader = 'SharedKey biznussoftfiles'+':'+signature;
req.setHeader('Authorization',authHeader);
req.setEndpoint('https://<storageName>.file.core.windows.net/<shareName>/<directoryName>/'+fileName+'?comp=range&timeout=20');
req.setBodyAsBlob(img);
Http http = new Http();
HTTPResponse res;
res = http.send(req);
}
在我的代码中,fileName、fileSize、img 是我要上传的图像的属性(它们是动态的)。
这里,我遇到了 Content-Length 的问题。以下是错误响应:
<?xml version="1.0" encoding="utf-8"?>
<Error><Code>InvalidHeaderValue</Code>
<Message>The value for one of the HTTP headers is not in the correct format.
RequestId:0996d8a7-001a-0060-7376-2ce658000000
Time:2017-09-13T09:53:06.4734435Z</Message>
<HeaderName>Content-Length</HeaderName>
<HeaderValue>197844</HeaderValue>
</Error>
更新说明:将范围恢复为 0-filesize() 以避免出现无效范围错误。
您收到的错误指出 Content-Length 值是意外的。您发送的负载可能与 2224 字节不匹配。检查setBodyAsBlob是否真的发送了2224字节的数据。你可以在WireShark之类的工具上验证这个抓包。
我应该早点看到这个问题:)。本质上,问题出在您的 x-ms-range
header 上。因为它从0
开始,结束值应该是length of the image - 1
。完成后,代码应该可以正常工作。
请看下面的代码:
public class azure_cls_cpy {
public string fileName {get; set;}
public integer fileSize {get; set;}
public blob img {get; set;}
public void createImage(){
string storageKey = 'account-key';
string storageName = 'account-name';
string shareName = 'share-name';
Datetime dt = Datetime.now();
string formattedDate = dt.formatGMT('EEE, dd MMM yyyy HH:mm:ss')+ ' GMT';
system.debug('formattedDate--'+formattedDate);
system.debug('fileSize--'+fileSize);
string CanonicalizedHeaders = 'x-ms-date:'+formattedDate+'\nx-ms-range:bytes=0-'+string.valueOf(fileSize-1)+'\nx-ms-version:2016-05-31\nx-ms-write:update';
string CanonicalizedResource = '/' + storageName + '/' + shareName + '/' +fileName + '\ncomp:range';
string StringToSign = 'PUT\n\n\n' + string.valueOf(fileSize) + '\n\n\n\n\n\n\n\n\n' + CanonicalizedHeaders+'\n'+CanonicalizedResource;
system.debug('StringToSign--'+StringToSign);
system.debug('StringToSign--'+StringToSign);
Blob temp = EncodingUtil.base64Decode(storageKey);
Blob hmac = Crypto.generateMac('HmacSHA256',Blob.valueOf(StringToSign),temp );
HttpRequest req = new HttpRequest();
req.setMethod('PUT');
req.setHeader('x-ms-version','2016-05-31' );
req.setHeader('x-ms-date', formattedDate);
req.setHeader('x-ms-range','bytes=0-'+string.valueOf(fileSize-1));
req.setHeader('x-ms-write','update');
req.setHeader('Content-Length', string.valueOf(fileSize));
string signature = EncodingUtil.base64Encode(hmac);
string authHeader = 'SharedKey ' + storageName +':'+signature;
req.setHeader('Authorization',authHeader);
req.setEndpoint('https://' + storageName + '.file.core.windows.net/' + shareName + '/' + fileName + '?comp=range');
req.setBodyAsBlob(img);
Http http = new Http();
HTTPResponse res;
res = http.send(req);
string responseBody = res.getBody();
system.debug('responseBody--'+responseBody);
}
}
我正在尝试使用此处指定的 REST API 文档将图像上传到 Azure 中的文件服务: https://docs.microsoft.com/en-us/rest/api/storageservices/put-range
视觉力页面:
<apex:page controller="azure_cls_cpy">
<apex:form >
<apex:inputFile value="{!img}" filename="{!fileName}" fileSize="{!fileSize}" ></apex:inputFile>
<apex:commandButton value="Click" action="{!createImage}"/><br/>
<apex:messages />
</apex:form>
</apex:page>
这是我的代码,它不起作用:
public void createImage(){
string storageKey = 'xxxxStorageKeyxxxx';
string storageName = '<storageName>';
Datetime dt = Datetime.now();
string formattedDate = dt.formatGMT('EEE, dd MMM yyyy HH:mm:ss')+ ' GMT';
string CanonicalizedHeaders = 'x-ms-date:'+formattedDate+'\nx-ms-range:bytes=0-'+string.valueOf(fileSize)+'\nx-ms-version:2016-05-31\nx-ms-write:update';
string CanonicalizedResource = '/' + storageName + '/<shareName>/<DirectoryName>/'+fileName+'\ncomp:range\ntimeout:20';
string StringToSign = 'PUT\n\n\nstring.valueOf(fileSize)\n\n\n\n\n\n\n\n\n' + CanonicalizedHeaders+'\n'+CanonicalizedResource;
system.debug('StringToSign--'+StringToSign);
Blob temp = EncodingUtil.base64Decode(storageKey);
Blob hmac = Crypto.generateMac('HmacSHA256',Blob.valueOf(StringToSign),temp ); //StringToSign
system.debug('oo-'+EncodingUtil.base64Encode(hmac));
HttpRequest req = new HttpRequest();
req.setMethod('PUT');
req.setHeader('x-ms-version','2016-05-31' );
req.setHeader('x-ms-date', formattedDate);
req.setHeader('Content-Length',string.valueOf(fileSize);
req.setHeader('x-ms-range','bytes=0-'+string.valueOf(fileSize));
req.setHeader('x-ms-write','update');
string signature = EncodingUtil.base64Encode(hmac);
string authHeader = 'SharedKey biznussoftfiles'+':'+signature;
req.setHeader('Authorization',authHeader);
req.setEndpoint('https://<storageName>.file.core.windows.net/<shareName>/<directoryName>/'+fileName+'?comp=range&timeout=20');
req.setBodyAsBlob(img);
Http http = new Http();
HTTPResponse res;
res = http.send(req);
}
在我的代码中,fileName、fileSize、img 是我要上传的图像的属性(它们是动态的)。
这里,我遇到了 Content-Length 的问题。以下是错误响应:
<?xml version="1.0" encoding="utf-8"?>
<Error><Code>InvalidHeaderValue</Code>
<Message>The value for one of the HTTP headers is not in the correct format.
RequestId:0996d8a7-001a-0060-7376-2ce658000000
Time:2017-09-13T09:53:06.4734435Z</Message>
<HeaderName>Content-Length</HeaderName>
<HeaderValue>197844</HeaderValue>
</Error>
更新说明:将范围恢复为 0-filesize() 以避免出现无效范围错误。
您收到的错误指出 Content-Length 值是意外的。您发送的负载可能与 2224 字节不匹配。检查setBodyAsBlob是否真的发送了2224字节的数据。你可以在WireShark之类的工具上验证这个抓包。
我应该早点看到这个问题:)。本质上,问题出在您的 x-ms-range
header 上。因为它从0
开始,结束值应该是length of the image - 1
。完成后,代码应该可以正常工作。
请看下面的代码:
public class azure_cls_cpy {
public string fileName {get; set;}
public integer fileSize {get; set;}
public blob img {get; set;}
public void createImage(){
string storageKey = 'account-key';
string storageName = 'account-name';
string shareName = 'share-name';
Datetime dt = Datetime.now();
string formattedDate = dt.formatGMT('EEE, dd MMM yyyy HH:mm:ss')+ ' GMT';
system.debug('formattedDate--'+formattedDate);
system.debug('fileSize--'+fileSize);
string CanonicalizedHeaders = 'x-ms-date:'+formattedDate+'\nx-ms-range:bytes=0-'+string.valueOf(fileSize-1)+'\nx-ms-version:2016-05-31\nx-ms-write:update';
string CanonicalizedResource = '/' + storageName + '/' + shareName + '/' +fileName + '\ncomp:range';
string StringToSign = 'PUT\n\n\n' + string.valueOf(fileSize) + '\n\n\n\n\n\n\n\n\n' + CanonicalizedHeaders+'\n'+CanonicalizedResource;
system.debug('StringToSign--'+StringToSign);
system.debug('StringToSign--'+StringToSign);
Blob temp = EncodingUtil.base64Decode(storageKey);
Blob hmac = Crypto.generateMac('HmacSHA256',Blob.valueOf(StringToSign),temp );
HttpRequest req = new HttpRequest();
req.setMethod('PUT');
req.setHeader('x-ms-version','2016-05-31' );
req.setHeader('x-ms-date', formattedDate);
req.setHeader('x-ms-range','bytes=0-'+string.valueOf(fileSize-1));
req.setHeader('x-ms-write','update');
req.setHeader('Content-Length', string.valueOf(fileSize));
string signature = EncodingUtil.base64Encode(hmac);
string authHeader = 'SharedKey ' + storageName +':'+signature;
req.setHeader('Authorization',authHeader);
req.setEndpoint('https://' + storageName + '.file.core.windows.net/' + shareName + '/' + fileName + '?comp=range');
req.setBodyAsBlob(img);
Http http = new Http();
HTTPResponse res;
res = http.send(req);
string responseBody = res.getBody();
system.debug('responseBody--'+responseBody);
}
}