阿里云OSS表单上传签名


/**
 *  阿里云 OSS 密钥
 */
define('AccessKeyId', 'ID');
define('AccessKeySecret', 'SS');
define('OssHost', 'https://bucket.oss-cn-beijing.aliyuncs.com');
define('UploadPrefix', 'files/');

function ossUploadSign($expire = 60) {
    $sign = [];
    $sign['accessid'] = AccessKeyId;
    $sign['host'] = OssHost;
    $sign['expire'] = time() + $expire;
    $sign['policy'] = base64_encode(json_encode(['expiration' => date('Y-m-d\TH:i:s\Z', $sign['expire']), 'conditions' => [['content-length-range', 0, 100 * 1024 * 1024], ['starts-with', '$key', UploadPrefix]]]));
    $sign['signature'] = base64_encode(hash_hmac('sha1', $sign['policy'], AccessKeySecret, true));
    $sign['dir'] = UploadPrefix;
    return json_encode($sign);
}

echo ossUploadSign();