Bir paketi statik web sitesi barındırma için yapılandırabilir ve yansıtma tabanlı kökene erişim için yönlendirme kuralları (RoutingRule) belirtebilirsiniz. Statik web sitesi barındırmayı etkinleştirdikten sonra, web sitesine yapılan istekler pakete yapılan isteklere eşdeğerdir. Ayrıca, belirtilen bir dizin sayfasına ve hata sayfasına otomatik yönlendirmeler de yapılandırabilirsiniz. Yansıtma tabanlı kökene erişim yönlendirme kuralları, verileri Object Storage Service (OSS) hizmetine sorunsuz bir şekilde geçirmenize yardımcı olur.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek istiyorsanız dahili bir uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınmaktadır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma.
Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneğinin oluşturulmasını göstermektedir. Özel bir etki alanı kullanmak veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama yapmak gibi alternatif yapılandırmalar için bkz. İstemciyi yapılandırma (Go SDK V1).
Statik web sitesi barındırma veya yansıtma tabanlı kökene erişimi yapılandırmak için
oss:PutBucketWebsiteiznine sahip olmanız gerekir. Statik web sitesi barındırma veya yansıtma tabanlı kökene erişim yapılandırmasını almak içinoss:GetBucketWebsiteiznine sahip olmanız gerekir. Statik web sitesi barındırma veya yansıtma tabanlı kökene erişim yapılandırmasını silmek içinoss:DeleteBucketWebsiteiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. RAM kullanıcılarına özel erişim ilkeleri atama.
Statik web sitesi barındırma
Statik web siteleri, tüm web sayfalarının yalnızca istemcide çalıştırılabilen JavaScript kodu gibi betikleri içeren statik içerikten oluştuğu web siteleridir. Statik web sitesi barındırma özelliğini kullanarak statik web sitenizi bir OSS paketinde barındırabilir ve web sitesine erişmek için paketin etki alanı adını kullanabilirsiniz.
-
Statik web sitesi barındırmayı yapılandırma
Aşağıdaki örnek kod, statik web sitesi barındırmanın nasıl yapılandırılacağını göstermektedir:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Specify the version of the signature algorithm. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. Example: examplebucket. bucketName := "examplebucket" // Set the default homepage to index.html and the default 404 page to error.html for the static website. err = client.SetBucketWebsite(bucketName, "index.html", "error.html") if err != nil { fmt.Println("Error:", err) os.Exit(-1) } } -
Statik web sitesi barındırma yapılandırmalarını sorgulama
Aşağıdaki örnek kod, statik web sitesi barındırma yapılandırmalarının nasıl sorgulanacağını göstermektedir:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Specify the version of the signature algorithm. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. Example: examplebucket. bucketName := "examplebucket" // Query static website hosting configurations. wsRes, err := client.GetBucketWebsite(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } fmt.Println("indexWebsite: ", wsRes.IndexDocument.Suffix) fmt.Println("errorWebsite: ", wsRes.ErrorDocument.Key) } -
Statik web sitesi barındırma yapılandırmalarını silme
Aşağıdaki örnek kod, statik web sitesi barındırma yapılandırmalarının nasıl silineceğini göstermektedir:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Specify the version of the signature algorithm. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. Example: examplebucket. bucketName := "examplebucket" // Delete static website hosting configurations. err = client.DeleteBucketWebsite(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } }
Yansıtma tabanlı kökene erişim
Yansıtma tabanlı kökene erişim, öncelikle verilerin OSS hizmetine sorunsuz bir şekilde geçirilmesi için kullanılır. Örneğin, hizmetiniz kendi kök sunucunuzda veya başka bir cloud ürününde çalışıyor olabilir. Hizmeti iş geliştirme amacıyla OSS hizmetine geçirmek istiyorsanız, geçiş sırasında hizmetin çalışmaya devam ettiğinden emin olmanız gerekir. Geçiş sırasında, OSS hizmetine henüz geçirilmemiş verileri almak için yansıtma tabanlı kökene erişim kurallarını kullanabilirsiniz. Bu, hizmetinizin beklendiği gibi çalışmasını sağlar.
-
Yansıtma tabanlı kökene erişim kurallarını yapılandırma
Bir istekte bulunan kişi belirtilen paketteki bir nesneye erişmeye çalıştığında ve nesne mevcut değilse, kökteki nesnenin URL'sini ve kökene erişim koşullarını belirterek istekte bulunan kişinin nesneyi kökten almasını sağlayabilirsiniz. Örneğin, Çin (Hangzhou) bölgesinde examplebucket adlı bir paket bulunmaktadır. Bir istekte bulunan kişi, paketin kök dizinindeki examplefolder dizinindeki bir nesneye erişmeye çalıştığında ve nesne mevcut olmadığında, istekte bulunan kişi kökteki examplefolder dizininde depolanan nesneye erişmek üzere www.example.com adresine yönlendirilir.
Aşağıdaki örnek kod, önceki senaryo için yansıtma tabanlı kökene erişim kurallarının nasıl yapılandırılacağını göstermektedir:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Specify the version of the signature algorithm. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. Example: examplebucket. bucketName := "examplebucket" var indexWebsite = "myindex.html" var errorWebsite = "myerror.html" btrue := true bfalse := false // Set the back-to-origin type to mirroring-based back-to-origin. ruleOk := oss.RoutingRule{ RuleNumber: 1, Condition: oss.Condition{ KeyPrefixEquals: "", // Set the back-to-origin condition to HTTP status code 404. HTTPErrorCodeReturnedEquals: 404, }, Redirect: oss.Redirect{ RedirectType: "Mirror", // PassQueryString: &btrue, // Specify the back-to-origin URL for a mirroring-based back-to-origin rule. MirrorURL: "http://www.test.com/", // MirrorPassQueryString:&btrue, // MirrorFollowRedirect:&bfalse, // MirrorCheckMd5:&bfalse, MirrorHeaders: oss.MirrorHeaders{ // PassAll:&bfalse, // Transmit specific HTTP headers. Pass: []string{"myheader-key1", "myheader-key2"}, // Prohibit the transmission of specific HTTP headers. Remove: []string{"myheader-key3", "myheader-key4"}, Set: []oss.MirrorHeaderSet{ { Key: "myheader-key5", Value: "myheader-value5", }, }, }, }, } // Set the back-to-origin type to redirection-based back-to-origin. ruleArrOk := []oss.RoutingRule{ { RuleNumber: 2, Condition: oss.Condition{ // Set the back-to-origin condition to HTTP status code 404 and the prefix in object names to abc/. KeyPrefixEquals: "abc/", HTTPErrorCodeReturnedEquals: 404, IncludeHeader: []oss.IncludeHeader{ { Key: "host", Equals: "test.oss-cn-beijing-internal.aliyuncs.com", }, }, }, Redirect: oss.Redirect{ RedirectType: "AliCDN", Protocol: "http", HostName: "www.test.com", PassQueryString: &bfalse, ReplaceKeyWith: "prefix/${key}.suffix", HttpRedirectCode: 301, }, }, // Set the back-to-origin type to mirroring-based back-to-origin. { RuleNumber: 3, Condition: oss.Condition{ KeyPrefixEquals: "", HTTPErrorCodeReturnedEquals: 404, }, Redirect: oss.Redirect{ RedirectType: "Mirror", PassQueryString: &btrue, MirrorURL: "http://www.test.com/", MirrorPassQueryString: &btrue, MirrorFollowRedirect: &bfalse, MirrorCheckMd5: &bfalse, MirrorHeaders: oss.MirrorHeaders{ PassAll: &btrue, Pass: []string{"myheader-key1", "myheader-key2"}, Remove: []string{"myheader-key3", "myheader-key4"}, Set: []oss.MirrorHeaderSet{ { Key: "myheader-key5", Value: "myheader-value5", }, }, }, }, }, } wxmlOne := oss.WebsiteXML{ IndexDocument: oss.IndexDocument{ Suffix: indexWebsite, }, ErrorDocument: oss.ErrorDocument{ Key: errorWebsite, }, } wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleOk) wxmlOne.RoutingRules = append(wxmlOne.RoutingRules, ruleArrOk...) err = client.SetBucketWebsiteDetail(bucketName, wxmlOne) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } } -
Yansıtma tabanlı kökene erişim yapılandırmalarını sorgulama
Aşağıdaki örnek kod, yansıtma tabanlı kökene erişim yapılandırmalarının nasıl sorgulanacağını göstermektedir:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Specify the version of the signature algorithm. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. Example: examplebucket. bucketName := "examplebucket" // Query mirroring-based back-to-origin configurations. data,err := client.GetBucketWebsiteXml(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } fmt.Println(data) } -
Yansıtma tabanlı kökene erişim yapılandırmalarını silme
Aşağıdaki örnek kod, yansıtma tabanlı kökene erişim yapılandırmalarının nasıl silineceğini göstermektedir:
package main import ( "fmt" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" ) func main() { // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. provider, err := oss.NewEnvironmentVariableCredentialsProvider() if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Create an OSSClient instance. // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region. clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)} clientOptions = append(clientOptions, oss.Region("yourRegion")) // Specify the version of the signature algorithm. clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4)) client, err := oss.New("yourEndpoint", "", "", clientOptions...) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } // Specify the name of the bucket. Example: examplebucket. bucketName := "examplebucket" // Delete mirroring-based back-to-origin configurations. err = client.DeleteBucketWebsite(bucketName) if err != nil { fmt.Println("Error:", err) os.Exit(-1) } }
İlgili Konular
Statik web sitesi barındırma ve yansıtma tabanlı kökene erişimi yönetmek için kullanılan tam örnek kod için GitHub adresini ziyaret edin.
Statik web sitesi barındırma veya yansıtma tabanlı kökene erişimi yapılandırmak için kullanılan API işlemi hakkında daha fazla bilgi için bkz. PutBucketWebsite.
Statik web sitesi barındırma veya yansıtma tabanlı kökene erişim yapılandırmasını almak için kullanılan API işlemi hakkında daha fazla bilgi için bkz. GetBucketWebsite.
Statik web sitesi barındırma veya yansıtma tabanlı kökene erişim yapılandırmasını silmek için kullanılan API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketWebsite.