文档首页/ OBS PHP SDK/ API参考/ 其他接口/ 生成带授权信息的URL
更新时间:2021-03-18 GMT+08:00
分享

生成带授权信息的URL

功能说明

通过访问密钥、请求方法类型、请求参数等信息生成一个在Query参数中携带鉴权信息的URL,以对OBS服务进行特定操作。

方法定义

ObsClient->createSignedUrl(array $parameter)

请求参数

字段名

类型

约束

说明

Method

string

必选

HTTP方法类型,支持的值:

  • GET
  • POST
  • PUT
  • DELETE
  • HEAD

Bucket

string

可选

桶名。

Key

string

可选

对象名。

SpecialParam

string

可选

特殊操作符,代表要操作的子资源,支持的值:

  • versions
  • uploads
  • location
  • storageinfo
  • quota
  • storagePolicy
  • acl
  • logging
  • policy
  • lifecycle
  • website
  • versioning
  • cors
  • notification
  • tagging
  • delete
  • restore

Expires

integer

可选

带授权信息的URL的过期时间(单位:秒),默认值:300。

Headers

associative array

可选

请求中携带的头域。

QueryParams

associative array

可选

请求中携带的查询参数。

返回结果

字段名

类型

说明

SignedUrl

string

带授权信息的URL。

ActualSignedRequestHeaders

associative array

通过带授权信息的URL发起请求时实际应携带的头域。

代码样例

try {
       // 生成创建桶的带授权信息的URL
       $resp = $obsClient->createSignedUrl( [ 
              'Method' => 'PUT',
              'Bucket' => 'bucketname',
              'Expires' => 3600,
       ] );
       printf ( "SignedUrl:%s\n", $resp ['SignedUrl'] );
       printf ( "ActualSignedRequestHeaders:%s\n", print_r($resp ['ActualSignedRequestHeaders'], true) );
       
       // 生成上传对象的带授权信息的URL
       $resp = $obsClient->createSignedUrl( [
                     'Method' => 'PUT',
                     'Bucket' => 'bucketname',
                     'Key' => 'objectkey',
                     'Expires' => 3600,
                     'Headers' => ['content-type' => 'text/plain']
       ] );
       printf ( "SignedUrl:%s\n", $resp ['SignedUrl'] );
       printf ( "ActualSignedRequestHeaders:%s\n", print_r($resp ['ActualSignedRequestHeaders'], true) );

       // 生成设置对象ACL的带授权信息的URL
       $resp = $obsClient->createSignedUrl( [
                     'Method' => 'PUT',
                     'Bucket' => 'bucketname',
                     'Key' => 'objectkey',
                     'Expires' => 3600,
                     'SpecialParam' => 'acl',
                     'Headers' => ['x-obs-acl' => 'public-read']
       ] );
       printf ( "SignedUrl:%s\n", $resp ['SignedUrl'] );
       printf ( "ActualSignedRequestHeaders:%s\n", print_r($resp ['ActualSignedRequestHeaders'], true) );
       
       // 生成下载对象的带授权信息的URL
       $resp = $obsClient->createSignedUrl( [
                     'Method' => 'GET',
                     'Bucket' => 'bucketname',
                     'Key' => 'objectkey',
                     'Expires' => 3600
       ] );
       printf ( "SignedUrl:%s\n", $resp ['SignedUrl'] );
       printf ( "ActualSignedRequestHeaders:%s\n", print_r($resp ['ActualSignedRequestHeaders'], true) );

       // 生成删除对象的带授权信息的URL
       $resp = $obsClient->createSignedUrl( [
                     'Method' => 'DELETE',
                     'Bucket' => 'bucketname',
                     'Key' => 'objectkey',
                     'Expires' => 3600
       ] );
       printf ( "SignedUrl:%s\n", $resp ['SignedUrl'] );
       printf ( "ActualSignedRequestHeaders:%s\n", print_r($resp ['ActualSignedRequestHeaders'], true) );
       
       // 生成删除桶的带授权信息的URL
       $resp = $obsClient->createSignedUrl( [
                     'Method' => 'DELETE',
                     'Bucket' => 'bucketname',
                     'Expires' => 3600
       ] );
       printf ( "SignedUrl:%s\n", $resp ['SignedUrl'] );
       printf ( "ActualSignedRequestHeaders:%s\n", print_r($resp ['ActualSignedRequestHeaders'], true) );
} catch ( Obs\Common\ObsException $obsException ) {
       printf ( "ExceptionCode:%s\n", $obsException->getExceptionCode () );
       printf ( "ExceptionMessage:%s\n", $obsException->getExceptionMessage () );
}

相关文档