阿里云函数计算
发表于 · 归类于技术 · 阅读完需 5 分钟 ·
一、方法说明
二、功能使用
全局变量
$counter = 0;
function preStop($context) {
$GLOBALS['fcLogger']->info("preStop ok");
}
function handler($event, $context) {
global $counter;
$counter += 2;
return $counter;
}
获取请求参数
$info['body'] = $request->getBody()->getContents();
$info['queries'] = $request->getQueryParams(); //数组
$info['method'] = $request->getMethod(); // `GET`
$info['headers'] = $request->getHeaders(); // "Host":["fc.fe80.cn"]
$info['path'] = $request->getAttribute('path'); // `/aa/bb/cc`
$info['requestURI'] = $request->getAttribute('requestURI'); // `/aa/bb/cc?name=bb`
$info['clientIP'] = $request->getAttribute('clientIP'); // `116.171.4.22`
无密码操作对象存储
use RingCentral\Psr7\Response;
use OSS\OssClient;
use OSS\Core\OssException;
function handler($request, $context): Response
{
$credentials = $context['credentials'];
$resbody['oss']['list'] = getObjectList($credentials);
return new Response(
200,
[
'X-Powered-By' => 'nginx/1.28.0',
'Content-Type' => 'application/json',
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Headers' => 'origin,accept,content-type',
],
json_encode($resbody,JSON_UNESCAPED_UNICODE)
);
}
function getObjectList($credentials){
$accessKeyId = $credentials['accessKeyId'];
$accessKeySecret = $credentials['accessKeySecret'];
$securityToken = $credentials['securityToken'];
$endpoint = 'http://oss-cn-chengdu.aliyuncs.com';
$bucket = 'bucket';
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, $securityToken);
$options = array(
'delimiter' => '/',
'prefix' => "folder",
'max-keys' => 100,
'marker' => '',
);
$listObjectInfo = $ossClient->listObjects($bucket, $options);
foreach ($listObject as $objectInfo) {
// TODO
}
return $listObject;
}