Skip to content

Commit

Permalink
Add tutorial website
Browse files Browse the repository at this point in the history
  • Loading branch information
Lupino committed Oct 25, 2015
1 parent dd9fd09 commit fa8f0e4
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
85 changes: 85 additions & 0 deletions examples/website/README.md
Original file line number Diff line number Diff line change
@@ -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 <http://ossgosdkwebsite.oss-cn-hangzhou.aliyuncs.com>

## The end

there some wrong with site browser will download the file

the source code [main.go](main.go)
25 changes: 25 additions & 0 deletions examples/website/css/screen.css
Original file line number Diff line number Diff line change
@@ -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 { }
Empty file added examples/website/error.html
Empty file.
40 changes: 40 additions & 0 deletions examples/website/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<html>
<head>
<title>oss-static-site -- basic HTML+CSS+JS website on Open Storage Service</title>
<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen" charset="utf-8" />
<script src="js/application.js" type="text/javascript" charset="utf-8"></script>

</head>
<body>

<div id="container">
<div id="header">
<h1><a href="/">oss-static-site</a></h1>

<div id="navigation">
<ul>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
</div>
</div>


<div id="main">

<div id="description">
<h2>What's the idea?</h2>
<p>Yep</p>
</div>
</div>

<div id="footer">
&copy; 2015 No Rights Reserved.

</div>
</div>


</body>
</html>
1 change: 1 addition & 0 deletions examples/website/js/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* lol */
55 changes: 55 additions & 0 deletions examples/website/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit fa8f0e4

Please sign in to comment.