接入教程
1. 访问Pantry官网并注册账户
2. 创建一个新的存储桶(Pantry)
3. 获取API密钥用于身份验证
4. 使用HTTP请求存储或检索JSON数据
5. 在项目中集成Pantry的API端点
Python示例:存储和获取数据
import requests
import json
# 存储数据
base_url = 'https://getpantry.cloud'
headers = {
'Content-Type': 'application/json',
'Pantry-Id': 'YOUR_API_KEY'
}
# 创建篮子(存储桶)
basket_name = 'myBasket'
data_to_store = {'key': 'value', 'items': [1, 2, 3]}
response = requests.post(
f'{base_url}/api/pantry/YOUR_API_KEY/basket/{basket_name}',
headers=headers,
data=json.dumps(data_to_store)
)
print(f'存储状态: {response.status_code}')
# 获取篮子数据
retrieve_response = requests.get(
f'{base_url}/api/pantry/YOUR_API_KEY/basket/{basket_name}',
headers=headers
)
if retrieve_response.status_code == 200:
retrieved_data = retrieve_response.json()
print(f'获取的数据: {retrieved_data}')
PHP示例:使用cURL存储JSON数据
<?php
$baseUrl = 'https://getpantry.cloud';
$apiKey = 'YOUR_API_KEY';
$basketName = 'myBasket';
// 准备要存储的数据
$data = array(
'project' => 'demo',
'timestamp' => time(),
'settings' => array('theme' => 'dark')
);
$jsonData = json_encode($data);
// 存储数据
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$baseUrl/api/pantry/$apiKey/basket/$basketName");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Pantry-Id: ' . $apiKey
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "存储响应代码: $httpCode\n";
// 获取数据
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$baseUrl/api/pantry/$apiKey/basket/$basketName");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Pantry-Id: ' . $apiKey));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$retrievedData = curl_exec($ch);
curl_close($ch);
echo "获取的数据: $retrievedData\n";
?>
JavaScript示例:使用Fetch API
const baseUrl = 'https://getpantry.cloud';
const apiKey = 'YOUR_API_KEY';
const basketName = 'myBasket';
// 存储数据
async function storeData() {
const data = {
user: 'demoUser',
preferences: {
language: 'zh-CN',
notifications: true
},
lastUpdated: new Date().toISOString()
};
const response = await fetch(
`${baseUrl}/api/pantry/${apiKey}/basket/${basketName}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Pantry-Id': apiKey
},
body: JSON.stringify(data)
}
);
console.log(`存储状态: ${response.status}`);
return response.ok;
}
// 获取数据
async function retrieveData() {
const response = await fetch(
`${baseUrl}/api/pantry/${apiKey}/basket/${basketName}`,
{
headers: {
'Pantry-Id': apiKey
}
}
);
if (response.ok) {
const data = await response.json();
console.log('获取的数据:', data);
return data;
} else {
console.error('获取数据失败');
return null;
}
}
// 使用示例
storeData().then(() => {
retrieveData();
});
常见问题
Pantry是免费的吗?有什么使用限制?
Pantry完全免费使用,主要限制包括:每个API密钥最多创建100个篮子(存储桶),每个篮子最大存储限制为100KB,请求频率限制为每分钟60次。这些限制对于小型项目和原型开发通常足够。
如何保护我的数据安全?
Pantry通过API密钥进行访问控制。每个篮子通过唯一的篮子名称和API密钥组合进行保护。建议不要在前端代码中暴露API密钥,而是通过后端服务器中转请求。数据以明文JSON形式存储,不适合存储敏感个人信息。
Pantry支持哪些数据格式和操作?
Pantry专门设计用于存储和检索JSON数据。支持完整的CRUD操作:创建(POST)、读取(GET)、更新(PUT/POST)和删除(DELETE)篮子。所有数据必须以有效的JSON格式发送,响应也始终是JSON格式。
Aitishiku.com