阿里雲團隊努力不懈,力求將最新的技術內容更快地以您最熟悉的語言呈現。本文由簡體中文內容自動轉碼而成,過程無人工干預。阿里雲不保證此自動轉碼的準確性、完整性及時效性。因轉碼造成的任何內容錯誤及因此可能帶來的損失,阿里雲概不負責,敬請見諒。本文内容請以簡體中文版本為準。
全部產品
Search
文件中心

設定防盜鏈

更新時間: Oct 30, 2018

OSS是按使用收費的服務,為了防止使用者在OSS上的資料被其他人盜鏈,OSS支援基於HTTP header中表頭欄位referer的防盜鏈方法。更多OSS防盜鏈請參考:OSS防盜鏈

設定Referer白名單

通過putBucketReferer設定Referer白名單:

  1. let OSS = require('ali-oss')
  2. let client = new OSS({
  3. region: '<Your region>'
  4. accessKeyId: '<Your AccessKeyId>',
  5. accessKeySecret: '<Your AccessKeySecret>',
  6. bucket: '<Your bucket name>'
  7. });
  8. async function putBucketReferer () {
  9. try {
  10. let result = await client.putBucketReferer('bucket-name', 'region', true, [
  11. 'my-domain.com',
  12. '*.example.com'
  13. ]);
  14. console.log(result);
  15. } catch (e) {
  16. console.log(e);
  17. }
  18. }
  19. putBucketReferer();

查看Referer白名單

通過getBucketReferer查看Referer白名單:

  1. let OSS = require('ali-oss')
  2. let client = new OSS({
  3. region: '<Your region>'
  4. accessKeyId: '<Your AccessKeyId>',
  5. accessKeySecret: '<Your AccessKeySecret>',
  6. bucket: '<Your bucket name>'
  7. });
  8. async function getBucketReferer () {
  9. try {
  10. let result = await client.getBucketReferer('bucket-name', 'region');
  11. console.log(result);
  12. } catch (e) {
  13. console.log(e);
  14. }
  15. }
  16. getBucketReferer();

清空Referer白名單

通過deleteBucketReferer設定清空Referer白名單:

  1. let OSS = require('ali-oss')
  2. let client = new OSS({
  3. region: '<Your region>'
  4. accessKeyId: '<Your AccessKeyId>',
  5. accessKeySecret: '<Your AccessKeySecret>',
  6. bucket: '<Your bucket name>'
  7. });
  8. async function deleteBucketReferer () {
  9. try {
  10. let result = await client.deleteBucketReferer('bucket-name', 'region');
  11. console.log(result);
  12. } catch (e) {
  13. console.log(e);
  14. }
  15. }
  16. deleteBucketReferer();