Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

ServiceNow to AWS s3 Attachment Post Script

BatuhanP
Tera Contributor

Hello. I want to send attachments from ServiceNow incident_task to an AWS S3 server provided by a third-party software. I managed to upload something and received a 201 status code. However, when downloading the file from S3, the file structure is corrupted. No matter what I try, the file structure on the other side always seems corrupted. What could be causing this issue? Please help, I've been struggling with this for two weeks.

Code :

Please help me guys

 

 var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_sys_id', current.sys_id);
    attachmentGR.query();
    if (attachmentGR.next()) {
        var attachmentContent = new GlideSysAttachment();
        var fileContent = attachmentContent.getBytes(attachmentGR);
        var fileContentBase64 = GlideStringUtil.base64Encode(fileContent); // fileContent'i base64'e dönüştür
		var fileContentBase64Decode = GlideStringUtil.base64Decode(fileContent);
        var contentTypee = attachmentGR.content_type.toString(); // Dosya tipini alın
        var filename = attachmentGR.file_name.toString();
        var keyAndName = fileKey.replace('${filename}', filename); // keyAndName'i oluşturun
		}


var httpClient = new sn_ws.RESTMessageV2();
httpClient.setHttpMethod('POST');
httpClient.setEndpoint(uploadUri); // AWS S3 endpoint'i

var boundary = '----WebKitFormBoundary' + new Date().getTime();

var requestBody = '';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="Content-Type"\r\n\r\n';
requestBody += 'image/png\r\n';  // Dosya türünü burada belirtin

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="acl"\r\n\r\n';
requestBody += acl + '\r\n';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="key"\r\n\r\n';
requestBody += keyAndName + '\r\n';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="policy"\r\n\r\n';
requestBody += policy + '\r\n';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="success_action_status"\r\n\r\n';
requestBody += success_action_status + '\r\n';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="x-amz-algorithm"\r\n\r\n';
requestBody += amz_algorithm + '\r\n';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="x-amz-credential"\r\n\r\n';
requestBody += x_amz_credential + '\r\n';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="x-amz-date"\r\n\r\n';
requestBody += x_amz_date + '\r\n';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="x-amz-server-side-encryption"\r\n\r\n';
requestBody += x_amz_server_side_encryption + '\r\n';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="x-amz-signature"\r\n\r\n';
requestBody += x_amz_signature + '\r\n';

requestBody += '--' + boundary + '\r\n';
requestBody += 'Content-Disposition: form-data; name="file"; filename="' + filename + '"\r\n';
requestBody += 'Content-Type: image/png\r\n\r\n';  // Dosyanın türünü burada belirtin
requestBody += fileContent+ '\r\n';  // Dosya içeriği burada

requestBody += '--' + boundary + '--\r\n';

httpClient.setRequestBody(requestBody);

httpClient.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);

var response0 = httpClient.execute();

var responseBody = response0.getBody();
var httpStatus = response0.getStatusCode();
gs.info('AWS S3 yanıtı: ' + responseBody);
gs.log(requestBody);
gs.log(fileContentBase64Decode);
0 REPLIES 0