Last Updated: Jun 01, 2017

The OSS is a pay-as-you-go service. To prevent users’ data on the OSS from being leeched, the OSS supports anti-leech based on the field ‘referer’ in the HTTP header.

For more anti-leech information, refer to Anti-leech Configuration.

Set anti-leech

  1. # -*- coding: utf-8 -*-
  2. import oss2
  3. from oss2.models import BucketReferer
  4. auth = oss2.Auth ('Your AccessKeyID', 'Your AccessKeySecret')
  5. bucket = oss2.Bucket (auth, 'Your endpoint', 'your bucket name')
  6. bucket.put_bucket_referer(BucketReferer(True, ['', 'http://*']))

When the above code is successfully executed, the anti-leech configuration is as follows:

  • If the first parameter ‘allow_empty_referer’ is set to ‘True’, it means an empty referer is allowed. The parameter can also be set to ‘False’ based on your actual needs.
  • The referer whitelist is set to or the http://* with the wildcard ‘*’.

Get anti-leech settings

  1. config = bucket.get_bucket_referer()
  2. print('allow empty referer={0}, referers={1}'.format(config.allow_empty_referer, config.referers))

Disable anti-leech

To disable anti-leech, set the parameter allow_empty_referer to ‘True’ and clear the referer whitelist.

  1. bucket.put_bucket_referer(BucketReferer(True, []))
Thank you! We've received your feedback.