> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# ファイルの管理

> アプリケーション内のファイルを閲覧、読み取り、作成、移動、削除します。

```go theme={null}
client := rest.NewClient(os.Getenv("SQUARECLOUD_API_KEY"))
defer client.Close()

api := rest.New(client)
```

## ディレクトリ内のファイルの一覧取得

`api.GetApplicationFiles(appID, path)` は、ディレクトリのエントリを `[]squarecloud.FileInfo` として返します。パスを空にすると、アプリケーションのルートを一覧取得します。

```go theme={null}
entries, err := api.GetApplicationFiles("abc123def456abc123def456", "")
if err != nil {
	panic(err)
}

for _, entry := range entries {
	fmt.Println(entry.Type, entry.Name, entry.Size, entry.LastModified)
}
```

| Field          | Type     | Description                 |
| -------------- | -------- | --------------------------- |
| `Type`         | `string` | `"file"` または `"directory"`  |
| `Name`         | `string` | エントリ名                       |
| `Size`         | `int`    | サイズ（バイト） — ディレクトリの場合は常に `0` |
| `LastModified` | `int64`  | 最終更新日時（Unix ミリ秒）            |

## ファイルの読み取り

`api.ReadApplicationFile(appID, path)` は、ファイルのバイト列を含む `squarecloud.FileContent` を返します。

```go theme={null}
content, err := api.ReadApplicationFile("abc123def456abc123def456", "main.go")
if err != nil {
	panic(err)
}

fmt.Println(string(content.Data))
```

<Note>
  API はコンテンツを Node.js スタイルの Buffer オブジェクト（`{"type": "Buffer", "data": [...]}`）として返します。SDK がデコードを代行するため、`content.Data` はそのまま使えるプレーンな `[]byte`（`ByteArray` 型）です。
</Note>

## ファイルの作成または上書き

`api.PutApplicationFile(appID, path, content)` は `path` に `content` を書き込みます。ファイルが存在しない場合は作成し、存在する場合は上書きします。`squarecloud.FileWritten` を返します。

```go theme={null}
written, err := api.PutApplicationFile(
	"abc123def456abc123def456",
	"config/settings.json",
	[]byte(`{"debug": false}`),
)
if err != nil {
	panic(err)
}

fmt.Println(written.Written) // true
```

<Note>
  コンテンツは UTF-8 文字列として API に送信されます — バイナリファイルのアップロードはまだサポートされていません。バイナリファイルを配置するには、代わりに [commit](/ja/sdks/go/commit_and_upload) を使用してください。
</Note>

## ファイルの移動またはリネーム

`api.MoveApplicationFile(appID, path, to)` は、ファイルを移動またはリネームします。

```go theme={null}
err := api.MoveApplicationFile("abc123def456abc123def456", "main.go", "src/main.go")
if err != nil {
	panic(err)
}
```

## ファイルまたはディレクトリの削除

<Warning>
  ディレクトリを削除すると、その中のすべてが削除されます。[snapshot](/ja/sdks/go/snapshots) がない限り、削除されたファイルを復元する方法はありません。
</Warning>

```go theme={null}
err := api.DeleteApplicationFile("abc123def456abc123def456", "src/old.go")
if err != nil {
	panic(err)
}
```
