edit-icon download-icon

CNAME

Last Updated: Nov 07, 2017

OSS supports binding custom domain names to OSS, so that you can seamlessly migrate data storage to OSS. For example, your domain name is my-domain.com, and all your previous image resources are in a format similar to http://img.my-domain.com/xx.jpg. After migrating the image storage to OSS, you can still access the images with the original address by binding your custom domain name to OSS:

  • Activate the OSS service and create a bucket.
  • Bind img.my-domain.com to the created bucket.
  • Upload images to the created bucket in the OSS.
  • Modify the DNS configuration, add a CNAME record, and direct img.my-domain.com to an endpoint of OSS (for example, my-bucket.oss-cn-hangzhou.aliyuncs.com).

In this way, you can access the images stored on OSS using the original address http://img.my-domain.com/x.jpg.

For more information, see Bind custom domain names (CNAME).

When using the SDK, you can use a custom domain name as the endpoint. In this case, you must set the parameter UseCname to true. For example:

Note: For the example code of CORS, see sample/cname_sample.go.

  1. import (
  2. "fmt"
  3. "io/ioutil"
  4. "strings"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret", oss.UseCname(true))
  8. if err != nil {
  9. // HandleError(err)
  10. }
  11. bucket, err := client.Bucket("my-bucket")
  12. if err != nil {
  13. // HandleError(err)
  14. }
  15. err = bucket.PutObject("my-object", strings.NewReader("MyObjectValue"))
  16. if err != nil {
  17. // HandleError(err)
  18. }
  19. body, err := bucket.GetObject("my-object")
  20. if err != nil {
  21. // HandleError(err)
  22. }
  23. data, err := ioutil.ReadAll(body)
  24. if err != nil {
  25. // HandleError(err)
  26. }
  27. body.Close()
  28. data = data // Process data
  29. lsRes, err := bucket.ListObjects()
  30. if err != nil {
  31. // HandleError(err)
  32. }
  33. fmt.Println("Objects:", lsRes.Objects)
  34. err = bucket.DeleteObject("my-object")
  35. if err != nil {
  36. // HandleError(err)
  37. }

Note: The list_buckets interface is unavailable when CNAME is used, because the custom domain name has been bound to a specific bucket.

Thank you! We've received your feedback.