Download and Upload Attachments with the Command Line Tool
Overview
This article introduces how to download and upload attachments from Kintone records using cli-kintone .
This tutorial uses Kintone Apps that are available in the Kintone Marketplace .
Initial Setup
This section introduces the required Kintone Apps and settings.
Create a Sample App
-
First, create a new Kintone App. From the Kintone Marketplace choose Project Management Pack.
-
Add a few records that include attachments inside.
Generate an API Token
Follow the steps in the Generating API Tokens article to generate an API token for both apps. Make sure to check the View Records, Add Records, and Edit Records* option, and take note of the API token, as it is needed in the later steps.
Download Attachments
To download attachment files from records, use the record export
command. In addition, the --attachments-dir
option along with the directory to download to must be specified.
The directory file path can be relative or absolute to the directory where cli-kintone is run.
|
|
--base-url
: The Kintone subdomain name (e.g. https://example.kintone.com)--app
: The App ID to retrieve attachments from--api-token
: API token copied from Initial Setup--attachments-dir
: The directory to which the attachment will be downloaded to
When run, the attachments will be downloaded to the specified directory.
For every record in the output, a directory with the name structure of FieldCode-RecordNumber
will be created, with the files included inside.
Example
|
|
Example Directory Structure
|
|
Download Attachments from Table Data
When downloading attachments from Table data, a directory with the name structure of FieldCode-RecordNumber-RowNumber
will be created, with the files included inside. The row number starts with 0.
Example Directory Structure
|
|
Upload Attachments to New Records
To upload and attach files, use the record import
command. In addition, pass the --attachments-dir
option along with the relative directory path containing the files to be attached.
The directory file path can be relative or absolute to the directory where cli-kintone is run.
In the CSV file containing record data, specify the path to the attachment file. The path is relative to the specified --attachments-dir
path. If one attachment field will have multiple uploads, separate the file names with new lines.
Example CSV file
|
|
Example Directory Structure
|
|
When the command is run, the total number of added records will be output in the command prompt.
Example
|
|
If successful, the record will be added with the files displayed in the Attachments field.
Update Attachments of Existing Records
To update attachments, use the record import
command with the --attachments-dir
option again. In addition, specify the records to update with the --update-key
option.
--update-key
: The field code of the field used as the key for updating.
For more information about updating records, refer to the Import Data with the Command Line Tool article.
In this example, the attachments uploaded in the previous section are updated. Replace the "kdp_02.png" value in the CSV file with the "kdp_03.png" value and keep the "kdp_01.png" value. If not, the file will be deleted.
Example CSV file
|
|
Example Directory Structure
|
|
When the command is run, the total number of updated records will be output in the command prompt.
Example
|
|
If successful, the "kdp_02.png" file will be deleted from the Attachments field. The "kdp_01.png" and "kdp_03.png" files will be uploaded instead.
Delete Attachments
To delete attachments, use the record import
command with the --attachments-dir
option again and leave the attachment field value blank in the CSV file. In addition, specify the records to update with the --update-key
option.
Example CSV
|
|
Specify --attachments-dir
even if all attachments will be deleted.
When the command is run, the total number of added and updated records will be output in the command prompt.
Example
|
|