> ## 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)
}
```

| 字段             | 类型       | 说明                       |
| -------------- | -------- | ------------------------ |
| `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)` 将 `content` 写入 `path`，文件不存在则创建，存在则覆盖。它返回一个 `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](/zh/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](/zh/sdks/go/snapshots)，否则已删除的文件无法恢复。
</Warning>

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