From fa8f0e40e124a2e7deb42f48cef2a1e33c338323 Mon Sep 17 00:00:00 2001 From: Lupino Date: Sun, 25 Oct 2015 11:42:38 +0800 Subject: [PATCH] Add tutorial website --- README.md | 1 + examples/website/README.md | 85 ++++++++++++++++++++++++++++++ examples/website/css/screen.css | 25 +++++++++ examples/website/error.html | 0 examples/website/index.html | 40 ++++++++++++++ examples/website/js/application.js | 1 + examples/website/main.go | 55 +++++++++++++++++++ 7 files changed, 207 insertions(+) create mode 100644 examples/website/README.md create mode 100644 examples/website/css/screen.css create mode 100644 examples/website/error.html create mode 100644 examples/website/index.html create mode 100644 examples/website/js/application.js create mode 100644 examples/website/main.go diff --git a/README.md b/README.md index fdb123a..29e977c 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ var e = oss.ParseError(err) ## Tutorial [Getting start with oss-go-sdk](examples/getting_start) +[How to create a static website on OSS](examples/website) ## API docs diff --git a/examples/website/README.md b/examples/website/README.md new file mode 100644 index 0000000..4b31d74 --- /dev/null +++ b/examples/website/README.md @@ -0,0 +1,85 @@ +# How to use OSS as a static website. + +In this tutorial will tell you use the `PutBucketWebsite` to build a static website. + +First read [getting start](../getting_start) + +## create a website able bucket + +I' ll use bucket name `ossgosdkwebsite` to build the website. + +```go +var bucket = "ossgosdkwebsite" +``` + +first create a public read write bucket. + +```go +if err = OSSAPI.PutBucket(bucket, oss.ACLPublicReadWrite, nil, nil); err != nil { + var e = oss.ParseError(err) + log.Printf("Code: %s\nMessage: %s\n", e.Code, e.Message) +} +``` + +Once create the bucket, set it is website config. + +```go +if err = OSSAPI.PutBucketWebsite(bucket, "index.html", "error.html"); err != nil { + var e = oss.ParseError(err) + log.Printf("Code: %s\nMessage: %s\n", e.Code, e.Message) +} +``` + +## create a static website + +the website file list is: +```bash +. +├── css +│   └── screen.css +├── error.html +├── index.html +└── js +    └── application.js +``` + +write the file what you want. + +## upload static website + +first create an `upload` function. + +```go +func upload(OSSAPI *oss.API, bucket, filename string) { + fp, err := os.Open(filename) + defer fp.Close() + if err != nil { + log.Fatal(err) + } + + var headers = make(map[string]string) + headers["Content-Type"] = "text/html" + + if err = OSSAPI.PutObject(bucket, filename, fp, headers); err != nil { + var e = oss.ParseError(err) + log.Printf("Code: %s\nMessage: %s\n", e.Code, e.Message) + } +} +``` + +then upload the static site + +```go +upload(OSSAPI, bucket, "index.html") +upload(OSSAPI, bucket, "error.html") +upload(OSSAPI, bucket, "css/screen.css") +upload(OSSAPI, bucket, "js/application.js") +``` + +now you visit the website on + +## The end + +there some wrong with site browser will download the file + +the source code [main.go](main.go) diff --git a/examples/website/css/screen.css b/examples/website/css/screen.css new file mode 100644 index 0000000..add76ab --- /dev/null +++ b/examples/website/css/screen.css @@ -0,0 +1,25 @@ +body, html { margin: 0; padding: 0; } + +body { font-family: sans-serif; color: black; background: white; } +a { color: black; } +a:hover { color: red; } + +#container { } + +#header { text-align: center; } +#header h1 { } +#header img { width: 600px; } + +#navigation { display: none; } + +#main { width: 500px; margin: 0 auto; margin-top: 50px; padding-bottom: 80px; } +#main h2 { } +#main p { } + +#footer { display: none; } + +#socialmedia { overflow: hidden; } +#socialmedia div { display: block; margin-top: 10px; } +#socialmedia .google { } +#socialmedia .twitter { } +#socialmedia .facebook { } diff --git a/examples/website/error.html b/examples/website/error.html new file mode 100644 index 0000000..e69de29 diff --git a/examples/website/index.html b/examples/website/index.html new file mode 100644 index 0000000..cef09a2 --- /dev/null +++ b/examples/website/index.html @@ -0,0 +1,40 @@ + + + oss-static-site -- basic HTML+CSS+JS website on Open Storage Service + + + + + + +
+ + + +
+ +
+

What's the idea?

+

Yep

+
+
+ + +
+ + + + diff --git a/examples/website/js/application.js b/examples/website/js/application.js new file mode 100644 index 0000000..adac840 --- /dev/null +++ b/examples/website/js/application.js @@ -0,0 +1 @@ +/* lol */ diff --git a/examples/website/main.go b/examples/website/main.go new file mode 100644 index 0000000..412b701 --- /dev/null +++ b/examples/website/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "github.com/Lupino/oss-go-sdk" + "log" + "os" +) + +// AccessKeyID defined Access Key ID +const AccessKeyID = "3cpfsfx4f1yqxb3zy9rx1vn4" + +// AccessKeySecret defined Access Key Secret +const AccessKeySecret = "T4Re+CRM4oXrqvoqhv0vNRZi2sQ=" + +func main() { + var APIOptions = oss.GetDefaultAPIOptioins() + APIOptions.AccessID = AccessKeyID + APIOptions.SecretAccessKey = AccessKeySecret + var OSSAPI = oss.NewAPI(APIOptions) + + var bucket = "ossgosdkwebsite" + var err error + + if err = OSSAPI.PutBucket(bucket, oss.ACLPublicReadWrite, nil, nil); err != nil { + var e = oss.ParseError(err) + log.Printf("Code: %s\nMessage: %s\n", e.Code, e.Message) + } + + if err = OSSAPI.PutBucketWebsite(bucket, "index.html", "error.html"); err != nil { + var e = oss.ParseError(err) + log.Printf("Code: %s\nMessage: %s\n", e.Code, e.Message) + } + + upload(OSSAPI, bucket, "index.html") + upload(OSSAPI, bucket, "error.html") + upload(OSSAPI, bucket, "css/screen.css") + upload(OSSAPI, bucket, "js/application.js") + log.Println("success.") +} + +func upload(OSSAPI *oss.API, bucket, filename string) { + fp, err := os.Open(filename) + defer fp.Close() + if err != nil { + log.Fatal(err) + } + + var headers = make(map[string]string) + headers["Content-Type"] = "text/html" + + if err = OSSAPI.PutObject(bucket, filename, fp, headers); err != nil { + var e = oss.ParseError(err) + log.Printf("Code: %s\nMessage: %s\n", e.Code, e.Message) + } +}