This article introduces how to download and upload attachment files using cli-kintone. For those new to cli-kintone, refer to the following previous tutorials first:
How to Download Files from Attachment Fields
Attachment files can be downloaded using the following command.
The -b option must be used to specify the directory to save the file in. If the specified directory does not exist, it will be created as a new directory. The API token specified with the -t option is sufficient with just the "view" permission. Refer to Generating an API Token in the Kintone Help for more information about API tokens.
Structure of the Download Directories
Downloaded files are stored in directories with the following structure:
- Files in a regular Attachment field
- Files in an Attachment field inside a table
How to Upload Files to Attachment Fields
Check the format of the Attachment field by exporting a CSV file
The following command lets the user check the output format of attachment files being exported with the -b option. This exported CSV file can then be used as a template to prepare a CSV file for uploading other attachment files.
Since the purpose is to confirm the output format only, the -c option is used to limit the number of fields to be output to make the returned information easier to understand.
The following CSV file is exported when the field code of the attachment file is attachments and the field code of the record number field is Record_number.
In this example, 1-1.png and 1-2.png exist in record number 1, and 2-1.png exists in record number 2.
The paths to the exported attachment files are noted inside of the CSV file relative to the directory that was specified with the -b option. If multiple attachment files exist in a single record, the paths will be separated with line breaks in the CSV file.
Retrieve the files in an Attachment field inside a table by specifying the field code of the table with the other field codes.
Add Records with Attachment Files
Use the following example to add a new record to the Expense Report App that has also been used for the past cli-kintone tutorials. Refer to the article Creating an App from the Marketplace from the Kintone Help for information on adding sample Apps.
STEP 1 Prepare a CSV file for adding records
First, create a local directory called "Attachments" that will contain files for the attachment file uploads. Place a file named "receipt.pdf" into the directory.
Next, create a CSV file with the data shown below:
Feel free to work on an exported CSV file first to create the template for inserting the data. To add multiple attachment files, enter in the paths to the attachment files separated by line breaks.
For example, adding a file called sample.xlsx to the record as well, would look like the following.
Finally, save the file as "add_record_with_attachments.csv".
Notes on creating CSV files
When creating a CSV file by reusing and adding additional information to a file exported with cli-kintone, make sure to format the CSV file such that the line break character codes are consistent.
When handling multiple attachment files, importing a CSV file created with more than one type of line break character code may fail when importing.
When exporting, cli-kintone saves the CSV file so that the line break codes are in CRLF format. However, if the CSV file is exported using the redirection operator, the line break portion of the attachment file may be changed to LF. Be sure to check the format of the line breaks in the file before importing.
STEP 2 Import the CSV file
Once the attachment and CSV files are ready, the record can be registered by running the following command:
The registered record should look like the image below.
For those who have followed the cli-kintone tutorials up until now, the "Expense Code" field (field code "expense_code") in the Expense Report App likely has the "Prohibit duplicate values" option checked. To test out the samples in this article, make sure to change the value of the "Expense Code" field so an error is not returned because of duplicated Expense Code values.
Update Records with an Attachment File
In this section, the record that was registered with attachment fields in the previous section will be updated.
STEP 1 Prepare a CSV file for updating records
Refer to Cli-kintone Tutorial 3: Update Records to prepare a CSV file for updating records.
Any attachment files that are already inside of the record but are not specified in the CSV file to update the record will be deleted. Because of this, it is recommended to keep backup of the files stored in the records in advance. Additionally, saving the CSV file to use as a template at the same time will save time by eliminating the need to run two commands.
Specifying ExpenseReport as the directory to store the attachment files will give the following results.
The directory will be created and the attachment files that were registered to the record are saved.
The existing attachment files are saved in the path, <destination directory>\attachments-10. Therefore, to add a new attachment file called sample2.docx to the record, add the path attachments-10\sample2.docx to the CSV file.
The completed CSV file is shown below, saved as update_record_with_attachments.csv.
STEP 2 Update the record
Now that sample2.docx was added to the CSV file, add the sample2.docx file to <destination directory>\attachments-10 and run the command.
If the update was successful, it should look like the following.
The working directory should look like this.
This concludes cli-kintone tutorial 5, which covered attachment files.
The next cli-kintone tutorial will cover practical uses.