當前OSS支持用戶使用HTTPS/HTTP協議訪問Bucket。但因爲HTTP存在安全漏洞。大型企業客戶都要求使用HTTPS方式訪問OSS,而且拒絕HTTP訪問請求。python
目前OSS能夠經過RAM policy方式實現:限制某個用戶、角色拒絕經過HTTP協議訪問指定的Bucket和對象。可是RAM Policy是一種基於用戶的受權方式,沒法針對資源進行受權。也就是說沒法針對Bucket或者對象級別,拒絕全部用戶的HTTP請求。目前咱們正在基於Bucket Policy開發該功能,後續用戶能夠直接經過Bucket Policy設置HTTPS訪問策略。linux
阿里雲RAM Policy有豐富的Condition參數,能夠限制對資源的訪問。這裏咱們利用"Secure Transport"條件參數生成RAM Policy,以實現拒絕指定的用戶經過HTTP方式訪問Bucket。api
Condition | 功能 | 合法取值 |
---|---|---|
acs:SecureTransport | 是不是https協議 | 「true」或者」false」 |
{ "Version": "1", "Statement": [ { "Effect": "Deny", "Action": [ "oss:*" ], "Resource": [ "acs:oss:*:*:*" ], "Condition": { "Bool": { "acs:SecureTransport": [ "false" ] } } } ] }
說明::如上Policy可以拒絕該用戶經過HTTP方式訪問OSS資源;安全
說明:app
# -*- coding: utf-8 -*- import oss2 # 阿里雲主帳號AccessKey擁有全部API的訪問權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或平常運維,請登陸 https://ram.console.aliyun.com 建立RAM帳號。 auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') # Endpoint以杭州爲例,其它Region請按實際狀況填寫。 bucket = oss2.Bucket(auth, 'https://oss-cn-beijing.aliyuncs.com', 'test-beijing-2018') # <yourLocalFile>由本地文件路徑加文件名包括後綴組成,例如/users/local/myfile.txt bucket.put_object_from_file('02.txt', '002.txt')
root@shanghai-02:~/figo# python putobject.py 2019-01-10 20:55:37,003 oss2.api [INFO] 140496922879744 : Init oss bucket, endpoint: https://oss-cn-beijing.aliyuncs.com, isCname: False, connect_timeout: None, app_name: , enabled_crc: True 2019-01-10 20:55:37,008 oss2.api [INFO] 140496922879744 : Put object from file, bucket: test-beijing-2018, key: 02.txt, file path: 002.txt 2019-01-10 20:55:37,009 oss2.api [INFO] 140496922879744 : Start to put object, bucket: test-beijing-2018, key: 02.txt, headers: {'Content-Type': 'text/plain'} 2019-01-10 20:55:37,212 oss2.api [INFO] 140496922879744 : Put object done, req_id: 5C3740C952FF5BAFB298BDDA, status_code: 200
說明:如上執行結果,代表文件已經上傳成功;運維
說明:以下咱們在腳本中指定訪問路徑爲「http://oss-cn-beijing.aliyuncs.com」測試
# -*- coding: utf-8 -*- import oss2 # 阿里雲主帳號AccessKey擁有全部API的訪問權限,風險很高。強烈建議您建立並使用RAM帳號進行API訪問或平常運維,請登陸 https://ram.console.aliyun.com 建立RAM帳號。 auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') # Endpoint以杭州爲例,其它Region請按實際狀況填寫。 bucket = oss2.Bucket(auth, 'http://oss-cn-beijing.aliyuncs.com', 'test-beijing-2018') # <yourLocalFile>由本地文件路徑加文件名包括後綴組成,例如/users/local/myfile.txt bucket.put_object_from_file('02.txt', '002.txt')
root@shanghai-02:~/figo# python putobject.py 2019-01-10 21:14:37,499 oss2.api [INFO] 140697781880576 : Init oss bucket, endpoint: http://oss-cn-beijing.aliyuncs.com, isCname: False, connect_timeout: None, app_name: , enabled_crc: True 2019-01-10 21:14:37,501 oss2.api [INFO] 140697781880576 : Put object from file, bucket: test-beijing-2018, key: 02.txt, file path: 002.txt 2019-01-10 21:14:37,503 oss2.api [INFO] 140697781880576 : Start to put object, bucket: test-beijing-2018, key: 02.txt, headers: {'Content-Type': 'text/plain'} 2019-01-10 21:14:37,585 oss2.api [ERROR] 140697781880576 : Exception: {'status': 403, 'x-oss-request-id': '5C37453DDF97EBEDF4BDA095', 'details': {'HostId': 'test-beijing-2018.oss-cn-beijing.aliyuncs.com', 'Message': 'You have no right to access this object because of bucket acl.', 'Code': 'AccessDenied', 'RequestId': '5C37453DDF97EBEDF4BDA095'}} Traceback (most recent call last): File "putobject.py", line 10, in <module> bucket.put_object_from_file('02.txt', '002.txt') File "build/bdist.linux-x86_64/egg/oss2/api.py", line 481, in put_object_from_file File "build/bdist.linux-x86_64/egg/oss2/api.py", line 453, in put_object File "build/bdist.linux-x86_64/egg/oss2/api.py", line 1579, in __do_object File "build/bdist.linux-x86_64/egg/oss2/api.py", line 210, in _do oss2.exceptions.AccessDenied: {'status': 403, 'x-oss-request-id': '5C37453DDF97EBEDF4BDA095', 'details': {'HostId': 'test-beijing-2018.oss-cn-beijing.aliyuncs.com', 'Message': 'You have no right to access this object because of bucket acl.', 'Code': 'AccessDenied', 'RequestId': '5C37453DDF97EBEDF4BDA095'}}
說明:ui