メインコンテンツへスキップ
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)
}
FieldTypeDescription
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)pathcontent を書き込みます。ファイルが存在しない場合は作成し、存在する場合は上書きします。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)
}