OSS支持用户将自定义的域名(CNAME)绑定到OSS的Bucket上,这样能够支持用户无缝地将存储迁移到OSS上。例如用户的域名是my-domain.com,之前用户的所有图片资源都是形如
的格式,用户将图片存储迁移到OSS之后,通过绑定自定义域名,仍可以使用原来的地址访问到图片。使用步骤如下:
- 开通OSS服务并创建Bucket
- 修改域名的DNS配置,增加一个CNAME记录,将img.my-domain.com指向OSS服务的endpoint(如my-bucket.oss-cn-hangzhou.aliyuncs.com)
- 在官网控制台或者使用SDK将img.my-domain.com与创建的Bucket绑定
- 将图片上传到OSS的这个Bucket中
这样就可以通过原地址
访问到存储在OSS上的图片。
绑定自定义域名请参考
自定义域名绑定
增加一个CNAME
通过addBucketCname接口为Bucket增加一个CNAME绑定:
- [backcolor=transparent]<?[backcolor=transparent]php
- [backcolor=transparent]use[backcolor=transparent] OSS\OssClient[backcolor=transparent];
- [backcolor=transparent]$client [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OssClient[backcolor=transparent](
- [backcolor=transparent] [backcolor=transparent]'<Your AccessKeyId>'[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]'<Your AccessKeySecret>'[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]'<Your Endpoint>'[backcolor=transparent]);
- [backcolor=transparent]$client[backcolor=transparent]->[backcolor=transparent]addBucketCname[backcolor=transparent]([backcolor=transparent]'bucket name'[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]'img.my-domain.com'[backcolor=transparent]);
删除一个CNAME
通过deleteBucketCname接口删除一个CNAME绑定:
- [backcolor=transparent]<?[backcolor=transparent]php
- [backcolor=transparent]use[backcolor=transparent] OSS\OssClient[backcolor=transparent];
- [backcolor=transparent]$client [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OssClient[backcolor=transparent](
- [backcolor=transparent] [backcolor=transparent]'<Your AccessKeyId>'[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]'<Your AccessKeySecret>'[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]'<Your Endpoint>'[backcolor=transparent]);
- [backcolor=transparent]$client[backcolor=transparent]->[backcolor=transparent]deleteBucketCname[backcolor=transparent]([backcolor=transparent]'bucket name'[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]'img.my-domain.com'[backcolor=transparent]);
获取已绑定的CNAME
通过getBucketCname接口获取Bucket已绑定的CNAME列表:
- [backcolor=transparent]<?[backcolor=transparent]php
- [backcolor=transparent]use[backcolor=transparent] OSS\OssClient[backcolor=transparent];
- [backcolor=transparent]$client [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OssClient[backcolor=transparent](
- [backcolor=transparent] [backcolor=transparent]'<Your AccessKeyId>'[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]'<Your AccessKeySecret>'[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]'<Your Endpoint>'[backcolor=transparent]);
- [backcolor=transparent]$cnameConfig [backcolor=transparent]=[backcolor=transparent] $client[backcolor=transparent]->[backcolor=transparent]getBucketCname[backcolor=transparent]([backcolor=transparent]'bucket name'[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]'img.my-domain.com'[backcolor=transparent]);
- [backcolor=transparent]var_dump[backcolor=transparent]([backcolor=transparent]$cnameConfig[backcolor=transparent]);