I am trying to upload a PDF file with Https request to File Service (not a blob) in Azure.
I have created a file on the File share, but I have a problem with the content of the file. I don't know in which format should I upload the content.
I tried to use the PUT RANGE operation and send the body as BASE64. The operation was successful, but the PDF is not readable. I also tried to send the content as binary, but with the same result.
function uploadFileContentOnAzure(strFileUrl, objInvoicePDF, dtCurrentDate, strGUID, strBinaryPDF){
var TITLE = 'uploadFileContentOnAzure';
var intPDFSize = strBinaryPDF.length;
var intRangeSize = intPDFSize - 1;
var strRangeToSign = "PUT\n"
+ "\n" // content encoding
+ "\n" // content language
+ intPDFSize+"\n" // content length
+ "\n" // content md5
+ "application/pdf\n" // content type
+ "\n" // date
+ "\n" // if modified since
+ "\n" // if match
+ "\n" // if none match
+ "\n" // if unmodified since
+ "\n" // range
+ "x-ms-date:" + dtCurrentDate + "\nx-ms-range:bytes=0-"+intRangeSize+"\nx-ms-version:2017-07-29\nx-ms-write:update\n" // headers
+ "/" + THIS_SCRIPT.CONSTANTS.AZURE_Account_Name + "/test/"+objInvoicePDF.name+"\ncomp:range"; // resources
var objSecretKey = null;
objSecretKey = crypto.createSecretKey({
guid: strGUID,
encoding: encode.Encoding.BASE_64
});
var objHmac = null;
objHmac = crypto.createHmac({
algorithm: crypto.HashAlg.SHA256,
key: objSecretKey
});
objHmac.update({
input: strRangeToSign,
inputEncoding: encode.Encoding.UTF_8
});
var strSignature = null;
strSignature = objHmac.digest({
outputEncoding: encode.Encoding.BASE_64
});
logMessage('DEBUG', 'signature: ', strSignature);
logMessage('DEBUG', 'strFileContent length: ', intPDFSize);
var objHeaders = {};
objHeaders['Content-Type'] = 'application/pdf';
objHeaders['Authorization'] = 'SharedKey ' + THIS_SCRIPT.CONSTANTS.AZURE_Account_Name + ':' + strSignature;
objHeaders['x-ms-date'] = dtCurrentDate;
objHeaders['x-ms-version'] = '2017-07-29';
objHeaders['x-ms-range'] = 'bytes=0-'+intRangeSize;
objHeaders['Content-Length'] = ''+intPDFSize;
objHeaders['x-ms-write'] = 'update';
logMessage('DEBUG', 'headers: ', objHeaders);
var objResponse = https.put({
url: strFileUrl,
headers: objHeaders,
body: strBinaryPDF
});
logMessage('DEBUG',TITLE, 'response code: ' + objResponse.code);
logMessage('DEBUG',TITLE, 'response body: ' + objResponse.body);
return objResponse.code;
}
The header for CREATE FILE operation:
{
"Content-Type": "application/pdf",
"x-ms-content-length": "23509",
"x-ms-date": "Thu, 20 Feb 2020 07:15:07 GMT",
"x-ms-type": "file",
"x-ms-version": "2017-07-29",
Authorization: "SharedKey test:1qM/i3u3A22QmL3vGwmYDqHcZOw3GSl3HdDvo8dz7rE="
}
THE header for PUT RANGE operation:
{
"Content-Type": "application/pdf",
Authorization: "SharedKey test:SY9Wk4kRsMSBO/MeqJ4mOItKNBk4rN22etm/LDkH0Wk=",
"x-ms-date": "Thu, 20 Feb 2020 07:15:07 GMT",
"x-ms-version": "2017-07-29",
"x-ms-range": "bytes=0-23508",
"Content-Length": "23509",
"x-ms-write": "update"
}
I am using SuiteScript (Javascript).
For text/plain files everything works correctly.
Could you advise how to upload a PDF file through REST API?