开发者社区> 问答> 正文

Python-SDK之如何实现跨域资源共享?

CORS允许web端的应用程序访问不属于本域的资源。OSS提供接口方便开发者控制跨域访问的权限。关于CORS的更多内容,请参考



设定CORS规则


下面的代码设置了一条CORS规则:
  1. # -*- coding: utf-8 -*-
  2. import oss2
  3. from oss2.models import BucketCors, CorsRule
  4. auth = oss2.Auth('您的AccessKeyId', '您的AccessKeySecret')
  5. bucket = oss2.Bucket(auth, '您的Endpoint', '您的Bucket名')
  6. rule = CorsRule(allowed_origins=['*'],
  7.                 allowed_methods=['GET', 'HEAD'],
  8.                 allowed_headers=['*'],
  9.                 max_age_seconds=1000)
  10. bucket.put_bucket_cors(BucketCors([rule]))


获取CORS规则

  1. try:
  2.     cors = bucket.get_bucket_cors()
  3. except oss2.exceptions.NoSuchCors:
  4.     print('cors is not set')
  5. else:
  6.     for rule in cors.rules:
  7.         print('AllowedOrigins={0}'.format(rule.allowed_origins))
  8.         print('AllowedMethods={0}'.format(rule.allowed_methods))
  9.         print('AllowedHeaders={0}'.format(rule.allowed_headers))
  10.         print('ExposeHeaders={0}'.format(rule.expose_headers))
  11.         print('MaxAgeSeconds={0}'.format(rule.max_age_seconds))


删除CORS规则

  1. bucket.delete_bucket_cors()

展开
收起
青衫无名 2017-10-18 11:42:04 1374 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
一个跨平台的云服务SDK需要什么 立即下载
Data Pre-Processing in Python: 立即下载