# 添付コマンド

### 概要

本ページでは、ボルトのレコード内のファイル添付を管理するKeeperコマンダーのコマンドをまとめます。ダウンロード、アップロード、削除ができ、バックアップ、整理、メンテナンスなどに向いています。

次のコマンドを扱います。

* [添付ファイルのアップロード](#upload-attachments-command)
* [添付ファイルのダウンロード](#download-attachment-command)
* [添付ファイルの削除](#remove-attachment-command)

### 添付ファイルのアップロード <a href="#upload-attachments-command" id="upload-attachments-command"></a>

ボルトに新しいファイルを取り込み、レコードに紐付けます。既存のレコードに文書、構成ファイル、証明書などのファイルを安全に追加します。

<details>

<summary>DotNet CLI</summary>

**コマンド:** `upload-attachment`

**パラメーター:**

| パラメーター       | 説明                  |
| ------------ | ------------------- |
| `record`     | KeeperレコードのパスまたはUID |
| `-f, --file` | アップロードするファイルのパス     |

**オプション:**

| オプション       | 説明            |
| ----------- | ------------- |
| `--help`    | ヘルプ画面を表示します   |
| `--version` | バージョン情報を表示します |

**例:**

```bash
My Vault> upload-attachment record_uid  -f <path_of_file>
```

</details>

<details>

<summary>DotNet SDK</summary>

**メソッド:** `UploadAttachment`

```csharp
Task UploadAttachment(KeeperRecord record, IAttachmentUploadTask uploadTask);
```

**パラメーター:**

* `FileName` - レコード上の添付ファイル名
* `record Uid` - このファイルをアップロードして紐付ける先のレコードのUID

**例:**

```csharp
var uploadTask = new FileAttachmentUploadTask(<FileName>);

//the vault instance has to be created before we call the next function
await vault.UploadAttachment(record, uploadTask); 
```

</details>

<details>

<summary>Power Commander</summary>

**コマンド:** `Copy-FileToKeeperRecord`

**パラメーター:**

* `-Filename` : レコードにアップロードする添付の名前
* `-Record` : レコードUID

**例:**

```bash
PS> Copy-FileToKeeperRecord -Filename <path_of_the_file> -Record record_uid 
```

</details>

<details>

<summary>Python CLI</summary>

**コマンド:** `upload-attachment` or `ua`

**パラメーター:**

* `PATH` : レコードのパスまたはUID (必須)
* `--file` : アップロードするファイル名 FILE (必須)

**例:**

```bash
My Vault> upload-attachment record_uid  --file <path_of_the_file>
```

</details>

<details>

<summary>Python SDK</summary>

**関数:** `upload_attachments`

**引数:**

* `vault` レコードを扱う `VaultOnline` オブジェクト
* `record` `PasswordRecord` または `TypesRecord` オブジェクト
* `files` `FileUploadTask` で処理したファイルのリスト
* `stop_on_error` ブール値。アップロード失敗時に停止するかどうか

**例:**

```python
upload_attachments(self, vault: vault_online.VaultOnline,
                           record: Union[vault_record.PasswordRecord, vault_record.TypedRecord],
                           files: List[ParsedFieldValue],
                           stop_on_error: bool)
```

</details>

### 添付ファイルのダウンロード <a href="#download-attachment-command" id="download-attachment-command"></a>

指定したレコードに添付されたファイルをすべてダウンロードします。再帰指定時はフォルダ単位でも取得できます。

* ボルトのレコードに添付されたファイルを取得し、指定したローカルディレクトリに保存します。
* 個別のレコードから取得するほか、フォルダとそのサブフォルダを再帰的にたどり、すべての添付を取得できます。

<details>

<summary>DotNet CLI</summary>

**コマンド** : `download-attachment`

**パラメーター:**

* `record path or uid (pos. 0`) : 必須。Keeperレコード
* `-o, --output-dir` : 出力ディレクトリ

**フラグ** :

* `-f, --file` : 添付のUID、名前、またはタイトル
* `--help` : このヘルプ画面を表示します。
* `--version` : バージョン情報を表示します。

**例:**

```bash
My Vault> download-attachment record_uid -f "custom-json.json" -o .
Downloading custom-json.json ... Done.
```

</details>

<details>

<summary>DotNet SDK</summary>

Keeperレコードの添付またはファイルをローカルにダウンロードします。通常のレコード添付には `DownloadAttachmentFile`、ファイル型レコードには `DownloadFile` を使います。

**メソッド:** `DownloadAttachmentFile` またはレコード種別に応じて `DownloadFile`

```csharp
public async Task DownloadAttachmentFile(string recordUid, AttachmentFile attachment, Stream destination)
```

```csharp
public async Task DownloadFile(FileRecord fileRecord, Stream destination)
```

**パラメーター:**

| パラメーター        | 型              | 説明                     |
| ------------- | -------------- | ---------------------- |
| `recordUid`   | string         | 添付を含むレコードのUID          |
| `attachment`  | AttachmentFile | ダウンロードする添付ファイルオブジェクト   |
| `fileRecord`  | FileRecord     | ダウンロードするファイルレコードオブジェクト |
| `destination` | Stream         | ファイルの書き込み先の出力ストリーム     |

**例:**

```csharp
# atta here is attachment
using (var stream = File.OpenWrite(Path.Combine(options.OutputDirectory, atta.Name)))
{
    switch (atta)
    {
        case AttachmentFile af:
            await context.Vault.DownloadAttachmentFile(record.Uid, af, stream);
            break;
        case FileRecord fr:
            await context.Vault.DownloadFile(fr, stream);
            break;
    }
}
```

</details>

<details>

<summary>PowerCommander</summary>

**コマンド:** `Copy-KeeperFileAttachment` または `kda`

**パラメーター:**

| パラメーター  | 必須 | 説明                     |
| ------- | -- | ---------------------- |
| `-Path` | はい | ファイルをダウンロードする出力フォルダのパス |

**フラグ:**

| オプション        | 説明                                |
| ------------ | --------------------------------- |
| `-Record`    | 添付をダウンロードする元のレコードUID              |
| `-Folder`    | 添付をダウンロードする元のフォルダUID              |
| `-Name`      | ダウンロードする特定ファイルの名前 (`-Record` と併用) |
| `-Recursive` | フォルダとサブフォルダ内のすべての添付をダウンロードします     |

**例:**

```powershell
PS> Copy-KeeperFileAttachment -Record "ABC123XYZ456" -Name "custom-json.json" -Path .
PS> kda -Record "ABC123XYZ456" -Name "config.yaml" -Path ./downloads
# Downloads all the files for the respective folder
PS> Copy-KeeperFileAttachment -Folder record_uid  -Path . -Recursive
PS> kda -Folder "FOLDER_UID_123" -Path ./backup -Recursive
```

</details>

<details>

<summary>Python CLI</summary>

**コマンド:** `download-attachment`

**エイリアス:** `da`

**パラメーター:**

* `PATH` : レコードまたはフォルダのパスまたはUID (必須)

**オプション:**

* `-r, --recursive` : サブフォルダを再帰的にたどってダウンロードします
* `--out-dir` : OUT\_DIR ダウンロード先のローカルフォルダ
* `--preserve-dir` : ボルトのフォルダ構造を保持します
* `--record-title` : 添付ファイル名にレコードタイトルを付与する。

**例:**

```python
My Vault> download-attachment --out-dir .  record_uid
Downloading 'service_config.yaml'
```

</details>

<details>

<summary>Python SDK</summary>

**関数:** `download_to_file`

```python
download_to_file(self, file_name: str) -> None
```

**例:**

```python
for atta in attachments:
    file_name = atta.title
    if title:
        file_name = f'{title}-{atta.title}'
    file_name = os.path.basename(file_name)
    name = os.path.join(subfolder_path, file_name)
    if os.path.isfile(name):
        base_name, ext = os.path.splitext(file_name)
        name = os.path.join(subfolder_path, f'{base_name}({record_uid}){ext}')
    if os.path.isfile(name):
        base_name, ext = os.path.splitext(file_name)
        name = os.path.join(subfolder_path, f'{base_name}({atta.file_id}){ext}')
    atta.download_to_file(str(name))

```

</details>

### 添付ファイルの削除 <a href="#remove-attachment-command" id="remove-attachment-command"></a>

Keeperレコードから1件以上の添付を削除します。

* レコードから添付ファイルを恒久的に取り除きます。
* **古い、または重複した添付**の整理や、ボルトの容量管理に便利です。

<details>

<summary>DotNet CLI</summary>

**コマンド:** `delete-attachment`

**フラグ:**

* `record` : レコードのパスまたはUID
* `-h, --help` このヘルプメッセージを表示して終了する
* `--file` : NAME 添付ファイル名またはID。繰り返し指定可。

**例:**

```bash
My Vault> delete-attachment record_uid --file "custom-json.json, custom-json-1.json"  
```

</details>

<details>

<summary>DotNet SDK</summary>

**メソッド:**

```csharp
Task DeleteAttachmentCommand(this VaultContext context, DeleteAttachmentOptions options)
```

**フラグ** :

* 添付ID

**例:**

```csharp
var attachments = context.Vault.RecordAttachments(record).ToArray();
await context.Vault.DeleteAttachment(record, attachmentToDelete.Id);
```

</details>

<details>

<summary>PowerCommander</summary>

**コマンド** : `Remove-KeeperFileAttachment`

**エイリアス:** `krfa`

**フラグ** :

* `-Record` : レコードUID
* `-FileName` : 削除する添付の名前
* `-Confirm` : `-Confirm: $false` のとき確認プロンプトを出さない。

**例:**

```powershell
PS> Remove-KeeperFileAttachment -Record record_uid -FileName "custom-json.json", "custom-json-1.json"
```

</details>

<details>

<summary>Python CLI</summary>

**コマンド:** `delete-attachment`

**パラメーター:**

* `RECORD` : レコードのパスまたはUID (必須)

**オプション**:

* `-h, --help` : このヘルプメッセージを表示して終了する
* `--name` : NAME 添付ファイル名またはID。繰り返し指定可 (必須)

**例:**

```bash
My Vault> delete-attachment --name "service_config.yaml" record_uid
```

</details>

<details>

<summary>Python SDK</summary>

**関数:** `update_record`

既存のレコードから添付を外すには、レコード更新関数を使います。

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.keeper.io/keeperpam/jp/commander-sdk/keeper-commander-sdks/sdk-command-reference/record-commands/attachment-commands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
