应用 - 文件管理器
创建/修改文件
在你的应用中创建文件。
PUT
/
v2
/
apps
/
{app_id}
/
files
创建/修改文件
curl --request PUT \
--url https://api.squarecloud.app/v2/apps/{app_id}/files \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"path": "<string>",
"content": "<string>"
}
'import requests
url = "https://api.squarecloud.app/v2/apps/{app_id}/files"
payload = {
"path": "<string>",
"content": "<string>"
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.put(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'PUT',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({path: '<string>', content: '<string>'})
};
fetch('https://api.squarecloud.app/v2/apps/{app_id}/files', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.squarecloud.app/v2/apps/{app_id}/files",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => json_encode([
'path' => '<string>',
'content' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.squarecloud.app/v2/apps/{app_id}/files"
payload := strings.NewReader("{\n \"path\": \"<string>\",\n \"content\": \"<string>\"\n}")
req, _ := http.NewRequest("PUT", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.put("https://api.squarecloud.app/v2/apps/{app_id}/files")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"path\": \"<string>\",\n \"content\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.squarecloud.app/v2/apps/{app_id}/files")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Put.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"path\": \"<string>\",\n \"content\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"status": "success"
}
创建/修改文件会将
content 写入应用内正在运行的 path,如果文件不存在则创建它,存在则覆盖它。content 既可以是文本文件的 UTF-8 字符串,也可以是用于二进制内容的 { type: "Buffer", data: number[] } 字节数组。可用它进行小范围、有针对性的编辑,例如调整某个配置值或放入一个生成的文件,而无需重新上传整个应用;如需批量更改或上传 .zip,请改用提交应用。
写入 squarecloud.app 或 squarecloud.config 会被特殊处理:平台会将其解析为应用的配置(显示名称、描述、内存、autorestart、子域名)并校验每个字段,非法值会返回具体的错误代码(例如 INVALID_MEMORY 或 INVALID_SUBDOMAIN),而不会写入文件。这类配置重写的速率限制为每个用户每 15 秒 1 次请求。
在工作区共享的应用上,调用者需要具有 Maintainer 或 Administrator 角色,没有受限文件权限的成员会被阻止写入 .env 等排除路径。与其他文件管理器路由一样,此操作不会重启应用,需要新进程才能生效的更改请调用重启应用。
参数
string
必填
应用的 ID。你可以在应用控制面板的 URL 中找到它。
string
必填
文件应被创建的路径。示例:/test.txt
string
必填
待创建文件的内容。
响应
string
指示调用是否成功。成功时为
success,否则为 error。{
"status": "success"
}
常见错误
| 代码 | HTTP | 说明 |
|---|---|---|
INVALID_PATH | 400 | path 字段未通过校验。 |
INVALID_CONTENT | 400 | content 缺失或为空。 |
INVALID_DISPLAY_NAME / INVALID_DESCRIPTION / INVALID_MEMORY / INVALID_AUTORESTART / INVALID_SUBDOMAIN | 400 | 从 squarecloud.app/squarecloud.config 写入中解析出的某个字段未通过校验。 |
CANNOT_SET_SUBDOMAIN | 400 | 解析出的配置尝试设置一个当前套餐或账户无法使用的子域名。 |
BLOCKED_PATH | 403 | 调用者缺少写入受限文件的工作区权限。 |
SAVE_FAILED | 500 | 解析出的配置更新无法持久化。 |
⌘I
创建/修改文件
curl --request PUT \
--url https://api.squarecloud.app/v2/apps/{app_id}/files \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"path": "<string>",
"content": "<string>"
}
'import requests
url = "https://api.squarecloud.app/v2/apps/{app_id}/files"
payload = {
"path": "<string>",
"content": "<string>"
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.put(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'PUT',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({path: '<string>', content: '<string>'})
};
fetch('https://api.squarecloud.app/v2/apps/{app_id}/files', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.squarecloud.app/v2/apps/{app_id}/files",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => json_encode([
'path' => '<string>',
'content' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.squarecloud.app/v2/apps/{app_id}/files"
payload := strings.NewReader("{\n \"path\": \"<string>\",\n \"content\": \"<string>\"\n}")
req, _ := http.NewRequest("PUT", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.put("https://api.squarecloud.app/v2/apps/{app_id}/files")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"path\": \"<string>\",\n \"content\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.squarecloud.app/v2/apps/{app_id}/files")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Put.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"path\": \"<string>\",\n \"content\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"status": "success"
}

