跳转到主要内容
client := rest.NewClient(os.Getenv("SQUARECLOUD_API_KEY"))
defer client.Close()

api := rest.New(client)

列出目录中的文件

api.GetApplicationFiles(appID, path)[]squarecloud.FileInfo 返回目录中的条目。空路径列出应用根目录。
entries, err := api.GetApplicationFiles("abc123def456abc123def456", "")
if err != nil {
	panic(err)
}

for _, entry := range entries {
	fmt.Println(entry.Type, entry.Name, entry.Size, entry.LastModified)
}
字段类型说明
Typestring"file""directory"
Namestring条目名称
Sizeint大小(字节)—— 目录始终为 0
LastModifiedint64最后修改时间(Unix 毫秒)

读取文件

api.ReadApplicationFile(appID, path) 返回一个包含文件字节的 squarecloud.FileContent
content, err := api.ReadApplicationFile("abc123def456abc123def456", "main.go")
if err != nil {
	panic(err)
}

fmt.Println(string(content.Data))
API 以 Node.js 风格的 Buffer 对象({"type": "Buffer", "data": [...]})返回内容。SDK 已为你解码 —— content.Data 是一个普通的 []byteByteArray 类型),开箱即用。

创建或覆盖文件

api.PutApplicationFile(appID, path, content)content 写入 path,文件不存在则创建,存在则覆盖。它返回一个 squarecloud.FileWritten
written, err := api.PutApplicationFile(
	"abc123def456abc123def456",
	"config/settings.json",
	[]byte(`{"debug": false}`),
)
if err != nil {
	panic(err)
}

fmt.Println(written.Written) // true
内容以 UTF-8 字符串发送到 API —— 暂不支持二进制文件上传。要上传二进制文件,请改用 commit

移动或重命名文件

api.MoveApplicationFile(appID, path, to) 移动或重命名一个文件。
err := api.MoveApplicationFile("abc123def456abc123def456", "main.go", "src/main.go")
if err != nil {
	panic(err)
}

删除文件或目录

删除目录会移除其中的所有内容。除非你有 snapshot,否则已删除的文件无法恢复。
err := api.DeleteApplicationFile("abc123def456abc123def456", "src/old.go")
if err != nil {
	panic(err)
}