---
title: "Merging Attachments"
canonical: "https://docs.perspectium.com/space/Iodine/1704973/Merging%20Attachments"
format: markdown
---
Attachments are stored in ServiceNow in two separate tables,  sys_attachment  and  sys_attachment_doc . The  sys_attachment  record holds information about the attachment (such as the file name and file size), while the  sys_attachment_doc  table contains the actual byte data of the attachment separated into different chunks so as to not have one record take up too much space in a table row. Thus, sending attachment(s) to your database with the DataSync Agent will store the attachment(s) in the two tables just as they are stored in ServiceNow.  Merging Attachments in the Database For   edge encrypted   attachments, ServiceNow will encrypt the attachment byte data when the attachment is in one piece and then split up the encrypted content into chunks for saving in the  sys_attachment_doc  table. As a result, this content is not usable since each  sys_attachment_doc  record is only one portion of the encrypted content.  By including a  SysAttachmentHandler  in the  DataSync Agent  configuration , the Agent will query the  sys_attachment  and  sys_attachment_doc  tables and merge the attachment records back together into one record . The records are removed from these two tables and the merged attachment records can then be stored in whole in the database (in a new table called  attachments ) or shared out as messages back to the Integration Mesh.   NOTE :  Storing the attachments as complete records in the database will lead to large table rows. Ensure your database has the necessary storage space to save these large records. When saving attachments into the database, an  attachments  table gets created to store the fully built attachments and has the following structure: Column SQL Data Type* Description sys_id varchar(40) sys_id of the sys_attachment record table_name varchar(128) Name of the table that the file is attached to file_name varchar(128) Name of the file content_type varchar(128) Type of file table_sys_id varchar(32) sys_id of the record the file is attached to position bigint Will have a value of  0  to represent a full attachment starting at the beginning (0) position attachment varchar(max) Attachment's complete byte data. This column's data can be used to get the whole attachment as one viewable file. *Data types shown are for MySQL/SQL Server. Other databases will create columns in their equivalent data types (for example, Oracle will create the position field as a number field data type, its equivalent of bigint). ↑ Go to top of page Saving Locally or to a Cloud By including a  SysAttachmentHandler  in the DataSync Agent configuration, the Agent will query the  sys_attachment  and  sys_attachment_doc  tables and construct the full attachment to be saved locally or to a cloud storage service such as Amazon S3, Azure Blob Storage, or Google Cloud Storage. The records are then removed from these two tables. When adding a  SysAttachmentHandler , all database configurations must match the database configurations from the subscribe task to ensure the correct sys_attachment and sys_attachment_doc tables are being queried and processed Saving Locally  When saving locally, you will need the following directives  under  <share><task> :  Directive Description Required? <destination> Final location of the attachment (local, S3, GCS, ABS) yes <directory> Where the attachment will be saved locally yes <?xml version="1.0" encoding="UTF-8"?>
<config>
   <agent>
      <max_reads_per_connect>1</max_reads_per_connect>
      <polling_interval>5</polling_interval>
      <skip_message_set_processing />
      <test_mode />
      <subscribe>
         <task>
            <task_name>oracle_subscriber_automated_test</task_name>
            <handler>com.perspectium.replicator.sql.SQLSubscriber</handler>
            <decryption_key>The cow jumped over the moon</decryption_key>
            <message_connection user="demo" password="example_pass" queue="psp.out.replicator.agent_example">https://example.perspectium.net</message_connection>
            <use_cache />
            <instance_connection user="admin" password="example_pass">https://example.service-now.com</instance_connection>
            <database_server />
            <database_user>psptest</database_user>
            <database_password />
            <database_port>1433</database_port>
            <database_type>sqlserver</database_type>
            <database>psp_repl</database>
            <skip_columns_log_interval>200</skip_columns_log_interval>
         </task>
      </subscribe>
      <share>
         <task>
            <task_name>attachment_processor</task_name>
            <handler>com.perspectium.replicator.sql.subscriber.edge.SysAttachmentHandler</handler>
            <encryption_key>The cow jumped over the moon twice in 2021</encryption_key>
            <message_connection user="demo" password="example_pass" queue="psp.out.replicator.agent_attachments">https://example.perspectium.net</message_connection>
            <polling_interval>60</polling_interval>
            <max_writes_per_connect>1</max_writes_per_connect>
            <skip_queue />
            <skip_report />
            <topic>test</topic>
            <type>test</type>
            <key>mac</key>
            <name />
            <database_server />
            <database_user>psptest</database_user>
            <database_password />
            <database_port>1433</database_port>
            <database_type>sqlserver</database_type>
            <database>psp_repl</database>
            <attachment_storage>
               <directory>/Users/demo/Desktop/TestAttachments</directory>
               <destination>local</destination>
            </attachment_storage>
         </task>
      </share>
   </agent>
</config> Saving to a Cloud When saving to a cloud service, you will need the following directives  under  <share><task> :  Amazon S3 Directive Description Required? <destination> Final location of the attachment (local, S3, GCS, ABS) Yes <directory> Where the attachment will be saved locally Yes <access_key> Access Key associated with your AWS account Yes,  ONLY  when the DataSync Agent is not installed on an EC2 instance.  <region> Region that your AWS S3 bucket resides in NOTE : If a region is specified for an Agent on an EC2 instance, then the region must match the region of the EC2 instance and the region of the S3 bucket No <bucket_name> Name of your AWS S3 bucket, including subdirectories if desired, to specify where the records will be uploaded e.g. bucketName/folder1/folder2.   For example, with <bucket_name>psp-bucket</bucket_name> will save records into the psp-bucket S3 bucket. Yes <secret_access_key> Secret Access Key associated with your AWS account Yes,  ONLY  when the DataSync Agent is not installed on an EC2 instance.  <?xml version="1.0" encoding="UTF-8"?>
<config>
   <agent>
      <max_reads_per_connect>1</max_reads_per_connect>
      <polling_interval>5</polling_interval>
      <skip_message_set_processing />
      <test_mode />
      <subscribe>
         <task>
            <task_name>oracle_subscriber_automated_test</task_name>
            <handler>com.perspectium.replicator.sql.SQLSubscriber</handler>
            <decryption_key>The cow jumped over the moon</decryption_key>
            <message_connection user="demo" password="example_pass" queue="psp.out.replicator.agent_example">https://example.perspectium.net</message_connection>
            <use_cache />
            <instance_connection user="admin" password="example_pass">https://example.service-now.com</instance_connection>
            <database_server />
            <database_user>psptest</database_user>
            <database_password />
            <database_port>1433</database_port>
            <database_type>sqlserver</database_type>
            <database>psp_repl</database>
            <skip_columns_log_interval>200</skip_columns_log_interval>
         </task>
      </subscribe>
      <share>
         <task>
            <task_name>attachment_processor</task_name>
            <handler>com.perspectium.replicator.sql.subscriber.edge.SysAttachmentHandler</handler>
            <encryption_key>The cow jumped over the moon twice in 2021</encryption_key>
            <message_connection user="demo" password="example_pass" queue="psp.out.replicator.agent_attachments">https://example.perspectium.net</message_connection>
            <polling_interval>60</polling_interval>
            <max_writes_per_connect>1</max_writes_per_connect>
            <skip_queue />
            <skip_report />
            <topic>test</topic>
            <type>test</type>
            <key>mac</key>
            <name />
            <database_server />
            <database_user>psptest</database_user>
            <database_password />
            <database_port>1433</database_port>
            <database_type>sqlserver</database_type>
            <database>psp_repl</database>             
			<attachment_storage>
   				<directory>/Users/demo/Desktop/TestAttachments</directory>
   				<destination>S3</destination>
   				<access_key>EXAMPLEACCESSKEY</access_key>
   				<secret_access_key>EXAMPLESECRETACCESSKEY</secret_access_key>
  				<region>us-east-2</region>
   				<bucket_name>examplebucket</bucket_name>
   			</attachment_storage>
          </task>
      </share>
   </agent>
</config> Azure Blob Storage Directive Description Required? <destination> Final location of the attachment (local, S3, GCS, ABS) Yes <directory> Where the attachment will be saved locally Yes <connection_string> String provided by Azure for connecting and uploading to the blob storage Yes <bucket_name> Name of your Azure Blob Storage container, including subdirectories if desired, to specify where the records will be uploaded e.g. bucketName/folder1/folder2.   For example, with <bucket_name>psp-bucket</bucket_name> will save records into the psp-bucket Azure Blob Storage container. Yes <?xml version="1.0" encoding="UTF-8"?>
<config>
   <agent>
      <max_reads_per_connect>1</max_reads_per_connect>
      <polling_interval>5</polling_interval>
      <skip_message_set_processing />
      <test_mode />
      <subscribe>
         <task>
            <task_name>oracle_subscriber_automated_test</task_name>
            <handler>com.perspectium.replicator.sql.SQLSubscriber</handler>
            <decryption_key>The cow jumped over the moon</decryption_key>
            <message_connection user="demo" password="example_pass" queue="psp.out.replicator.agent_example">https://example.perspectium.net</message_connection>
            <use_cache />
            <instance_connection user="admin" password="example_pass">https://example.service-now.com</instance_connection>
            <database_server />
            <database_user>psptest</database_user>
            <database_password />
            <database_port>1433</database_port>
            <database_type>sqlserver</database_type>
            <database>psp_repl</database>
            <skip_columns_log_interval>200</skip_columns_log_interval>
         </task>
      </subscribe>
      <share>
         <task>
            <task_name>attachment_processor</task_name>
            <handler>com.perspectium.replicator.sql.subscriber.edge.SysAttachmentHandler</handler>
            <encryption_key>The cow jumped over the moon twice in 2021</encryption_key>
            <message_connection user="demo" password="example_pass" queue="psp.out.replicator.agent_attachments">https://example.perspectium.net</message_connection>
            <polling_interval>60</polling_interval>
            <max_writes_per_connect>1</max_writes_per_connect>
            <skip_queue />
            <skip_report />
            <topic>test</topic>
            <type>test</type>
            <key>mac</key>
            <name />
            <database_server />
            <database_user>psptest</database_user>
            <database_password />
            <database_port>1433</database_port>
            <database_type>sqlserver</database_type>
            <database>psp_repl</database>             
			<attachment_storage>
       			<directory>/Users/demo/Desktop/TestAttachments</directory>
   				<destination>ABS</destination>
   				<bucket_name>examplebucket</bucket_name><connection_string>DefaultEndpointsProtocol=https;AccountName=pspuploadtest;AccountKey=Ju6z+w5TZDQ6xGR9SlMnGfAPSKR41C2g/YD23u+EmiTDe77qU20NOGjXnE9wTgoI095BU97QW3p3iJC5kpvlkA==;EndpointSuffix=core.windows.net</connection_string>
   			</attachment_storage>
          </task>
      </share>
   </agent>
</config> Google Cloud Storage Directive Description Required? <destination> Final location of the attachment (local, S3, GCS, ABS) Yes <directory> Where the attachment will be saved locally Yes <key_path> JSON file provided by Googlefor connecting and uploading to the cloud Yes <project_id> Identifier of the Google project containing the bucket Yes <bucket_name> Name of your Google Cloud Storage bucket, including subdirectories if desired, to specify where the records will be uploaded e.g. bucketName/folder1/folder2.   For example, with <bucket_name>psp-bucket</bucket_name> will save records into the psp-bucket Google Cloud Storage bucket Yes <?xml version="1.0" encoding="UTF-8"?>
<config>
   <agent>
      <max_reads_per_connect>1</max_reads_per_connect>
      <polling_interval>5</polling_interval>
      <skip_message_set_processing />
      <test_mode />
      <subscribe>
         <task>
            <task_name>oracle_subscriber_automated_test</task_name>
            <handler>com.perspectium.replicator.sql.SQLSubscriber</handler>
            <decryption_key>The cow jumped over the moon</decryption_key>
            <message_connection user="demo" password="example_pass" queue="psp.out.replicator.agent_example">https://example.perspectium.net</message_connection>
            <use_cache />
            <instance_connection user="admin" password="example_pass">https://example.service-now.com</instance_connection>
            <database_server />
            <database_user>psptest</database_user>
            <database_password />
            <database_port>1433</database_port>
            <database_type>sqlserver</database_type>
            <database>psp_repl</database>
            <skip_columns_log_interval>200</skip_columns_log_interval>
         </task>
      </subscribe>
      <share>
         <task>
            <task_name>attachment_processor</task_name>
            <handler>com.perspectium.replicator.sql.subscriber.edge.SysAttachmentHandler</handler>
            <encryption_key>The cow jumped over the moon twice in 2021</encryption_key>
            <message_connection user="demo" password="example_pass" queue="psp.out.replicator.agent_attachments">https://example.perspectium.net</message_connection>
            <polling_interval>60</polling_interval>
            <max_writes_per_connect>1</max_writes_per_connect>
            <skip_queue />
            <skip_report />
            <topic>test</topic>
            <type>test</type>
            <key>mac</key>
            <name />
            <database_server />
            <database_user>psptest</database_user>
            <database_password />
            <database_port>1433</database_port>
            <database_type>sqlserver</database_type>
            <database>psp_repl</database>             
			<attachment_storage>
          		<directory>/Users/demo/Desktop/TestAttachments</directory>
   				<destination>GCS</destination>
   				<key_path>/Users/demo/Downloads/gcs-credentials.json</key_path>
   				<project_id>development-174017</project_id>
   				<bucket_name>examplebucket</bucket_name>
			</attachment_storage>
          </task>
      </share>
   </agent>
</config> ↑ Go to top of page