使用URL进行授权访问

更新时间: 2019-03-14 10:05

OBS客户端支持通过访问密钥、请求方法类型、请求参数等信息生成一个在Query参数中携带鉴权信息的URL,可将该URL提供给其他用户进行临时访问。在生成URL时,您需要指定URL的有效期来限制访客用户的访问时长。

如果您想授予其他用户对桶或对象临时进行其他操作的权限(例如上传或下载对象),则需要生成带对应请求的URL后(例如使用生成PUT请求的URL上传对象),将该URL提供给其他用户。

通过该方式可支持的操作以及相关信息见下表:


操作名

HTTP请求方法

特殊操作符**(子资源)
是否需要桶名
是否需要对象名**

创建桶

PUT

N/A





获取桶列表

GET

N/A





删除桶

DELETE

N/A





列举桶内对象

GET

N/A





列举桶内多版本对象

GET

versions





列举分段上传任务

GET

uploads





获取桶元数据

HEAD

N/A





获取桶区域位置

GET

location





获取桶存量信息

GET

storageinfo





设置桶配额

PUT

quota





获取桶配额

GET

quota





设置桶存储类型

PUT

storagePolicy





获取桶存储类型

GET

storagePolicy





设置桶访问权限

PUT

acl





获取桶访问权限

GET

acl





开启/关闭桶日志

PUT

logging





查看桶日志

GET

logging





设置桶策略

PUT

policy





查看桶策略

GET

policy





删除桶策略

DELETE

policy





设置生命周期规则

PUT

lifecycle





查看生命周期规则

GET

lifecycle





删除生命周期规则

DELETE

lifecycle





设置托管配置

PUT

website





查看托管配置

GET

website





清除托管配置

DELETE

website





设置桶多版本状态

PUT

versioning





查看桶多版本状态

GET

versioning





设置跨域规则

PUT

cors





查看跨域规则

GET

cors





删除跨域规则

DELETE

cors





设置/关闭事件通知

PUT

notification





查看事件通知

GET

notification





OPTIONS桶

OPTIONS

N/A





设置桶标签

PUT

tagging





查看桶标签

GET

tagging





删除桶标签

DELETE

tagging





上传对象

PUT

N/A





下载对象

GET

N/A





复制对象

PUT

N/A





删除对象

DELETE

N/A





批量删除对象

POST

delete





获取对象属性

HEAD

N/A





设置对象访问权限

PUT

acl





查看对象访问权限

GET

acl





初始化分段上传任务

POST

uploads





上传段

PUT

N/A





复制段

PUT

N/A





列举已上传的段

GET

N/A





合并段

POST

N/A





取消分段上传任务

DELETE

N/A





OPTIONS对象

OPTIONS

N/A





取回归档存储对象

POST

restore




通过OBS Go SDK实现URL授权访问的步骤如下:

  • 通过ObsClient.CreateSignedUrl生成带签名信息的URL。
  • 使用任意HTTP库发送HTTP/HTTPS请求,访问OBS服务。
    以下代码展示了如何使用URL进行授权访问,包括:创建桶、上传对象、下载对象、列举对象、删除对象。

创建桶

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. "strings"
  6. )
  7.  
  8. var ak = "*** Provide your Access Key ***"
  9. var sk = "*** Provide your Secret Key ***"
  10. var endpoint = "https://your-endpoint"
  11.  
  12. // 创建ObsClient结构体
  13. var obsClient, _ = obs.New(ak, sk, endpoint)
  14.  
  15. func main() {
  16. input := &obs.CreateSignedUrlInput{}
  17. input.Expires = 3600
  18. // 创建桶
  19. input.Method = obs.HttpMethodPut
  20. input.Bucket = "bucketname"
  21. output, _ := obsClient.CreateSignedUrl(input)
  22. fmt.Printf("SignedUrl:%s\n", output.SignedUrl)
  23. fmt.Printf("ActualSignedRequestHeaders:%v\n", output.ActualSignedRequestHeaders)
  24.  
  25. createBucketOutput, err := obsClient.CreateBucketWithSignedUrl(output.SignedUrl, output.ActualSignedRequestHeaders, strings.NewReader("<CreateBucketConfiguration><LocationConstraint>your-location</LocationConstraint></CreateBucketConfiguration>"))
  26. if err == nil {
  27. fmt.Printf("RequestId:%s\n", createBucketOutput.RequestId)
  28. } else if obsError, ok := err.(obs.ObsError); ok {
  29. fmt.Println("Code:%s\n", obsError.Code)
  30. fmt.Println("Message:%s\n", obsError.Message)
  31. }
  32.  
  33. // 上传对象
  34. input.Method = obs.HttpMethodPut
  35. input.Bucket = "bucketname"
  36. input.Key = "objectkey"
  37. output, _ = obsClient.CreateSignedUrl(input)
  38. fmt.Printf("SignedUrl:%s\n", output.SignedUrl)
  39. fmt.Printf("ActualSignedRequestHeaders:%v\n", output.ActualSignedRequestHeaders)
  40.  
  41. putObjectOutput, err := obsClient.PutObjectWithSignedUrl(output.SignedUrl, output.ActualSignedRequestHeaders, strings.NewReader("Hello OBS"))
  42. if err == nil {
  43. fmt.Printf("ETag:%s\n", putObjectOutput.ETag)
  44. } else if obsError, ok := err.(obs.ObsError); ok {
  45. fmt.Println("Code:%s\n", obsError.Code)
  46. fmt.Println("Message:%s\n", obsError.Message)
  47. }
  48.  
  49. // 下载对象
  50. input.Method = obs.HttpMethodGet
  51. input.Bucket = "bucketname"
  52. input.Key = "objectkey"
  53. output, _ = obsClient.CreateSignedUrl(input)
  54. fmt.Printf("SignedUrl:%s\n", output.SignedUrl)
  55. fmt.Printf("ActualSignedRequestHeaders:%v\n", output.ActualSignedRequestHeaders)
  56. getObjectOutput, err := obsClient.GetObjectWithSignedUrl(output.SignedUrl, output.ActualSignedRequestHeaders)
  57. if err == nil {
  58. defer getObjectOutput.Body.Close()
  59. fmt.Printf("ETag:%s\n", getObjectOutput.ETag)
  60. } else if obsError, ok := err.(obs.ObsError); ok {
  61. fmt.Println("Code:%s\n", obsError.Code)
  62. fmt.Println("Message:%s\n", obsError.Message)
  63. }
  64.  
  65. // 列举对象
  66. input.Method = obs.HttpMethodGet
  67. input.Bucket = "bucketname"
  68. input.Key = ""
  69. output, _ = obsClient.CreateSignedUrl(input)
  70. fmt.Printf("SignedUrl:%s\n", output.SignedUrl)
  71. fmt.Printf("ActualSignedRequestHeaders:%v\n", output.ActualSignedRequestHeaders)
  72.  
  73. listObjectsOutput, err := obsClient.ListObjectsWithSignedUrl(output.SignedUrl, output.ActualSignedRequestHeaders)
  74. if err == nil {
  75. for index, val := range listObjectsOutput.Contents {
  76. fmt.Printf("Content[%d]-OwnerId:%s, OwnerName:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
  77. index, val.Owner.ID, val.Owner.DisplayName, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
  78. }
  79. } else if obsError, ok := err.(obs.ObsError); ok {
  80. fmt.Println("Code:%s\n", obsError.Code)
  81. fmt.Println("Message:%s\n", obsError.Message)
  82. }
  83. // 删除对象
  84. input.Method = obs.HttpMethodDelete
  85. input.Bucket = "bucketname"
  86. input.Key = "objectkey"
  87. output, _ = obsClient.CreateSignedUrl(input)
  88. fmt.Printf("SignedUrl:%s\n", output.SignedUrl)
  89. fmt.Printf("ActualSignedRequestHeaders:%v\n", output.ActualSignedRequestHeaders)
  90. deleteObjectOutput, err := obsClient.DeleteObjectWithSignedUrl(output.SignedUrl, output.ActualSignedRequestHeaders)
  91. if err == nil {
  92. fmt.Printf("RequestId:%s\n", deleteObjectOutput.RequestId)
  93. } else if obsError, ok := err.(obs.ObsError); ok {
  94. fmt.Println("Code:%s\n", obsError.Code)
  95. fmt.Println("Message:%s\n", obsError.Message)
  96. }
  97. }

上传对象

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. "strings"
  6. )
  7.  
  8. var ak = "*** Provide your Access Key ***"
  9. var sk = "*** Provide your Secret Key ***"
  10. var endpoint = "https://your-endpoint"
  11.  
  12. // 创建ObsClient结构体
  13. var obsClient, _ = obs.New(ak, sk, endpoint)
  14.  
  15. func main() {
  16. input := &obs.CreateSignedUrlInput{}
  17. input.Expires = 3600
  18.  
  19. // 上传对象
  20. input.Method = obs.HttpMethodPut
  21. input.Bucket = "bucketname"
  22. input.Key = "objectkey"
  23. output, _ = obsClient.CreateSignedUrl(input)
  24. fmt.Printf("SignedUrl:%s\n", output.SignedUrl)
  25. fmt.Printf("ActualSignedRequestHeaders:%v\n", output.ActualSignedRequestHeaders)
  26.  
  27. putObjectOutput, err := obsClient.PutObjectWithSignedUrl(output.SignedUrl, output.ActualSignedRequestHeaders, strings.NewReader("Hello OBS"))
  28. if err == nil {
  29. fmt.Printf("ETag:%s\n", putObjectOutput.ETag)
  30. } else if obsError, ok := err.(obs.ObsError); ok {
  31. fmt.Println("Code:%s\n", obsError.Code)
  32. fmt.Println("Message:%s\n", obsError.Message)
  33. }
  34. }

下载对象

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. "strings"
  6. )
  7.  
  8. var ak = "*** Provide your Access Key ***"
  9. var sk = "*** Provide your Secret Key ***"
  10. var endpoint = "https://your-endpoint"
  11.  
  12. // 创建ObsClient结构体
  13. var obsClient, _ = obs.New(ak, sk, endpoint)
  14.  
  15. func main() {
  16. input := &obs.CreateSignedUrlInput{}
  17. input.Expires = 3600
  18.  
  19. // 下载对象
  20. input.Method = obs.HttpMethodGet
  21. input.Bucket = "bucketname"
  22. input.Key = "objectkey"
  23. output, _ = obsClient.CreateSignedUrl(input)
  24. fmt.Printf("SignedUrl:%s\n", output.SignedUrl)
  25. fmt.Printf("ActualSignedRequestHeaders:%v\n", output.ActualSignedRequestHeaders)
  26. getObjectOutput, err := obsClient.GetObjectWithSignedUrl(output.SignedUrl, output.ActualSignedRequestHeaders)
  27. if err == nil {
  28. defer getObjectOutput.Body.Close()
  29. fmt.Printf("ETag:%s\n", getObjectOutput.ETag)
  30. } else if obsError, ok := err.(obs.ObsError); ok {
  31. fmt.Println("Code:%s\n", obsError.Code)
  32. fmt.Println("Message:%s\n", obsError.Message)
  33. }
  34. }

列举对象

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. "strings"
  6. )
  7.  
  8. var ak = "*** Provide your Access Key ***"
  9. var sk = "*** Provide your Secret Key ***"
  10. var endpoint = "https://your-endpoint"
  11.  
  12. // 创建ObsClient结构体
  13. var obsClient, _ = obs.New(ak, sk, endpoint)
  14.  
  15. func main() {
  16. input := &obs.CreateSignedUrlInput{}
  17. input.Expires = 3600
  18.  
  19. // 列举对象
  20. input.Method = obs.HttpMethodGet
  21. input.Bucket = "bucketname"
  22. output, _ = obsClient.CreateSignedUrl(input)
  23. fmt.Printf("SignedUrl:%s\n", output.SignedUrl)
  24. fmt.Printf("ActualSignedRequestHeaders:%v\n", output.ActualSignedRequestHeaders)
  25.  
  26. listObjectsOutput, err := obsClient.ListObjectsWithSignedUrl(output.SignedUrl, output.ActualSignedRequestHeaders)
  27. if err == nil {
  28. for index, val := range listObjectsOutput.Contents {
  29. fmt.Printf("Content[%d]-OwnerId:%s, OwnerName:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d, StorageClass:%s\n",
  30. index, val.Owner.ID, val.Owner.DisplayName, val.ETag, val.Key, val.LastModified, val.Size, val.StorageClass)
  31. }
  32. } else if obsError, ok := err.(obs.ObsError); ok {
  33. fmt.Println("Code:%s\n", obsError.Code)
  34. fmt.Println("Message:%s\n", obsError.Message)
  35. }
  36.  
  37. }

删除对象

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. "strings"
  6. )
  7.  
  8. var ak = "*** Provide your Access Key ***"
  9. var sk = "*** Provide your Secret Key ***"
  10. var endpoint = "https://your-endpoint"
  11.  
  12. // 创建ObsClient结构体
  13. var obsClient, _ = obs.New(ak, sk, endpoint)
  14.  
  15. func main() {
  16. input := &obs.CreateSignedUrlInput{}
  17. input.Expires = 3600
  18.  
  19. // 删除对象
  20. input.Method = obs.HttpMethodDelete
  21. input.Bucket = "bucketname"
  22. input.Key = "objectkey"
  23. output, _ = obsClient.CreateSignedUrl(input)
  24. fmt.Printf("SignedUrl:%s\n", output.SignedUrl)
  25. fmt.Printf("ActualSignedRequestHeaders:%v\n", output.ActualSignedRequestHeaders)
  26. deleteObjectOutput, err := obsClient.DeleteObjectWithSignedUrl(output.SignedUrl, output.ActualSignedRequestHeaders)
  27. if err == nil {
  28. fmt.Printf("RequestId:%s\n", deleteObjectOutput.RequestId)
  29. } else if obsError, ok := err.(obs.ObsError); ok {
  30. fmt.Println("Code:%s\n", obsError.Code)
  31. fmt.Println("Message:%s\n", obsError.Message)
  32. }
  33. }

父主题:授权访问