跨域资源共享(Cross-origin resource sharing,简称CORS)允许Web端的应用程序访问不属于本域的资源。UOS提供跨域资源共享接口,方便您控制跨域访问的权限。
以下代码用于设置指定存储空间的跨域资源共享规则:
from src.ossConfig import ossConfig import unicloud_oss.Oss as Oss access_key = 'youraccesskey' secret_key = 'yoursecretkey' endpoint_url = 'http://XXXXXXXXXXXXXXXXX.com' config = ossConfig(access_key,secret_key,endpoint_url) bucket_name = 'ddddd1' #get_corsConfig CORSConfiguration=Oss.get_bucket_cors(config, bucket_name) if CORSConfiguration is False: print("The specified bucket does not have CORS configured") else: print(CORSConfiguration) #put_corsConfig CORSRules = [ { 'AllowedHeaders': [ 'string111', ], 'AllowedMethods': [ 'PUT', 'POST', ], 'AllowedOrigins': [ 'string', ], 'ExposeHeaders': [ 'string', ], 'MaxAgeSeconds': 123 }, ] if Oss.put_bucket_cors(config, bucket_name, CORSRules): print("put bucket cors succes!") else: print("put bucket cors failed!") #delete_corsConfig if Oss.delete_bucket_cors(config, bucket_name): print("delete sucess!") else: print("delete failed!") |