我正在尝试使用此处指定的 REST API 文档将图像上传到 Azure 中的 FILE SERVICE: 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 是我要上传的图像的属性(它们是动态的)。
在这里,我面临内容长度的问题。以下是错误响应:
<?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() 以避免无效范围错误。