edit-icon download-icon

Anti-leech

Last Updated: Oct 24, 2017

OSS is a Pay-As-You-Go service. To prevent users’ data on OSS from being leeched, OSS supports anti-leech based on the field referer in the HTTP header.

For more anti-leech information, see 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://aliyun.com', 'http://*.aliyuncs.com']))

Once the preceding code is successfully executed, the anti-leech is configured 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 requirements.

  • The referer whitelist is set to http://aliyun.com or the http://*.aliyuncs.com 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 as ‘True’ and clear the referer whitelist.

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