-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
© 2015 No Rights Reserved. | ||
|
||
</div> | ||
</div> | ||
|
||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* lol */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |