想要对特定路径下的所有对象设置公开访问权限,并使新写入对象继承该权限,应使用 put_bucket_policy 而不是 put_bucket_acl。
bucketpolicy 示例
参考阿里云官方文档中的 bucketpolicy 示例,可以自行编写一个针对特定路径的 bucketpolicy:https://help.aliyun.com/document_detail/266627.htm
示例代码
以下代码示例展示了如何在 sample/video 路径下设置公开访问权限:
from oss2 import Bucket from oss2.fields import Policy bucket = Bucket(oss2.Auth(), "your-endpoint", "your-bucket-name") policy = Policy() policy.rules = [ { "effect": "Allow", "principal": "*", "action": ["GetObject"], "resource": ["acs:oss://your-bucket-name/sample/video/*"], } ] bucket.put_bucket_policy(policy)