最近更新时间:2022-07-25
本文介绍如何使用防盗链。
为了防止您在UOS上的数据被其他人盗链而产生额外费用,您可以设置防盗链功能,包括以下参数:
Referer白名单。仅允许指定的域名访问UOS资源。
是否允许空Referer。如果不允许空Referer,则只有HTTP或HTTPS header中包含Referer字段的请求才能访问UOS资源。
以下代码用于设置防盗链:
public void testBucketSetReferer(){ String bucketRefererPolicy = "{\n" + " \"Id\":\"http referer policy\"," + " \"Version\":\"2012-10-17\"," + " \"Statement\":[" + " {\n" + " \"Sid\":\"RefererAllow\"," + " \"Effect\":\"Allow\"," + " \"Principal\":{\"AWS\":[ \"*\"]}," + " \"Action\":[\"s3:GetObject\"]," + " \"Resource\":[\"arn:aws:s3:::javatest/*\"]," + " \"Condition\":{\n" + " \"StringLike\":{" + " \"aws:Referer\":[" + " \"*://*test.com\"," + " \"*://*test.com/*\"" + " ]" + " }" + " }" + " }" + " ]" + "}"; try{ s3.setBucketPolicy(bucketName,bucketRefererPolicy); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } System.out.println("done!"); } |
以下代码用于获取防盗链信息:
public void testBucketGetReferer() { try { BucketPolicy policy = s3.getBucketPolicy(bucketName); System.out.println(policy.getPolicyText()); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } System.out.println("done!"); } |
以下代码用于清空防盗链:
public void testBucketDelReferer() { try{ s3.deleteBucketPolicy(bucketName); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } System.out.println("done!"); } |