edit-icon download-icon

Anti-leech

Last Updated: Nov 07, 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.

Configure a referer whitelist

We can use the following code to set a Referer whitelist:

  1. using Aliyun.OSS;
  2. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  3. var refererList = new List<string>();
  4. // Add a referer item
  5. refererList.Add("http://www.aliyun.com");
  6. refererList.Add("http://www.*.com");
  7. refererList.Add("http://www.?.aliyuncs.com");
  8. // Allow a blank referer field and configure the bucket referer list
  9. var request = new SetBucketRefererRequest(bucketName, refererList);
  10. request. AllowEmptyReferer = true;
  11. client.setBucketReferer(bucketName, br);
  12. Console.WriteLine ("The bucket {0} referer whitelist has been successfully configured", bucketName);

For complete code, see GitHub.

Note: The referer parameters support the wildcard “*” and “?”. For more information about rule configuration, see Anti-leech settings.

Retrieve a Referer whitelist

  1. using Aliyun.OSS;
  2. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  3. var rc = client.GetBucketReferer(bucketName);
  4. Console.WriteLine("allow?" + (rc.AllowEmptyReferer ? "yes" : "no"));
  5. if (rc.RefererList.Referers != null)
  6. {
  7. for (var i = 0; i < rc.RefererList.Referers.Length; i++)
  8. Console.WriteLine(rc.RefererList.Referers[i]);
  9. }
  10. else
  11. {
  12. Console.WriteLine("Empty Referer List");
  13. }

For complete code, see GitHub.

Clear a Referer whitelist

The Referer whitelist cannot be cleared directly. You can only reset it to overwrite the previous rules.

  1. using Aliyun.OSS;
  2. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  3. // Allow a blank referer field and a blank referer whitelist by default.
  4. var request = new SetBucketRefererRequest(bucketName);
  5. client.SetBucketReferer(request);
  6. Console.WriteLine ("The bucket {0} referer whitelist has been cleared successfully", bucketName);
Thank you! We've received your feedback.