我尝试使用此处指定的REST API(CREATE FILE)文档将图像上传到Azure中的FILE SERVICE:https://docs.microsoft.com/en-us/rest/api/storageservices/create-file
这是我的代码,它不起作用:
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);
integer imageSize = img.size();
system.debug('imageSize--'+imageSize);
string CanonicalizedHeaders = 'x-ms-content-length:'+string.valueOf(fileSize)+'\nx-ms-date:'+formattedDate+'\nx-ms-type:file\nx-ms-version:2016-05-31';
string CanonicalizedResource = '/' + storageName + '/' + shareName + '/' +fileName;
string StringToSign = 'PUT\n\n\n\n\nimage/png\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 );
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-content-length',string.valueOf(fileSize));
req.setHeader('x-ms-type','file');
req.setHeader('Content-Type','image/png');
req.setHeader('Content-Length', '0');
string signature = EncodingUtil.base64Encode(hmac);
string authHeader = 'SharedKey ' + storageName +':'+signature;
req.setHeader('Authorization',authHeader);
req.setEndpoint('https://' + storageName + '.file.core.windows.net/' + shareName + '/'+fileName);
Http http = new Http();
HTTPResponse res;
res = http.send(req);
string responseBody = res.getBody();
system.debug('responseBody--'+responseBody);
createImage_Put();
}
}
//Put Range Operation
public void createImage_Put(){
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>/'+fileName+'\ncomp:range';
string StringToSign = 'PUT\n\n\n'+string.valueOf(fileSize)+'\n\nimage/png\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 );
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('Content-Type','image/png');
req.setHeader('x-ms-range','bytes=0-'+string.valueOf(fileSize));
req.setHeader('x-ms-write','update');
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);
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info,'responseBody of PutRange operation--'+res.getBody()));
}
}
在我的代码中,fileName,fileSize,img是我试图上传的图像的属性(动态)。
在这里,我面临来自createImage_Put(PUT RANGE Operation)的问题。以下是错误回复:
<?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 :(得分:2)
代码存在一些问题。
StringToSign
中指定文件的内容长度。它应该是空的。基于这些,请参阅下面的代码。我能够运行此代码并成功创建了一个空文件。
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);
integer imageSize = img.size();
system.debug('imageSize--'+imageSize);
string CanonicalizedHeaders = 'x-ms-content-length:'+string.valueOf(fileSize)+'\nx-ms-date:'+formattedDate+'\nx-ms-type:file\nx-ms-version:2016-05-31';
string CanonicalizedResource = '/' + storageName + '/' + shareName + '/' +fileName;
string StringToSign = 'PUT\n\n\n\n\nimage/png\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 );
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-content-length',string.valueOf(fileSize));
req.setHeader('x-ms-type','file');
req.setHeader('Content-Type','image/png');
req.setHeader('Content-Length', '0');
string signature = EncodingUtil.base64Encode(hmac);
string authHeader = 'SharedKey ' + storageName +':'+signature;
req.setHeader('Authorization',authHeader);
req.setEndpoint('https://' + storageName + '.file.core.windows.net/' + shareName + '/'+fileName);
Http http = new Http();
HTTPResponse res;
res = http.send(req);
string responseBody = res.getBody();
system.debug('responseBody--'+responseBody);
}
}