第一個go的web程式;調用七牛雲端儲存的音頻api問題解決;條件搜尋檔案裡的內容

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   檔案   

package main


import (
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"runtime/debug"
)


const (
ListDir      = 0x0001
UPLOAD_DIR   = "./uploads"
TEMPLATE_DIR = "./views"
)


//cache storage all template
var templates = make(map[string]*template.Template)


func init() {
fileInfoArr, err := ioutil.ReadDir(TEMPLATE_DIR)
check(err)
var templateName, templatePath string
for _, fileInfo := range fileInfoArr {
templateName = fileInfo.Name()
if ext := path.Ext(templateName); ext != ".html" {
continue
}
templatePath = TEMPLATE_DIR + "/" + templateName
//log.Println(templateName)
log.Println("Loading template:", templatePath)
t := template.Must(template.ParseFiles(templatePath))
templates[templateName] = t
}


//if const template
/*for _, tmpl := range []string{"upload", "list"} {
//Must ensure in case can‘t analytic will do error operate,If the template loading is not successful, the program will exit
t := template.Must(template.ParseFiles(tmpl + ".html"))
templates[tmpl] = t
}*/
}
func check(err error) {
if err != nil {
panic(err)
}
}
func renderHtml(w http.ResponseWriter, tmpl string, locals map[string]interface{}) {
/*tt := templates[tmpl]
log.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", templates, tmpl, tt)*/
log.Println(locals)
err := templates[tmpl].Execute(w, locals)


check(err)
}
func isExists(path string) bool {
_, err := os.Stat(path)
//log.Println(err)
if err == nil {
return true
}
return os.IsExist(err)
}


//Callback method,
func uploadHandler(w http.ResponseWriter, r *http.Request) {
//log.Println(r.Method)
if r.Method == "GET" {
renderHtml(w, "upload.html", nil)
}
if r.Method == "POST" {
f, h, err := r.FormFile("image")
//log.Println(f, h, err)
check(err)
filename := h.Filename
defer f.Close()
//log.Println(UPLOAD_DIR, filename)
t, err := ioutil.TempFile(UPLOAD_DIR, filename)
//log.Println(t)
check(err)
defer t.Close()
_, err = io.Copy(t, f) //dst src
check(err)
http.Redirect(w, r, "/view?id="+filename, http.StatusFound)


}
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
imageId := r.FormValue("id")
imagePath := UPLOAD_DIR + "/" + imageId
log.Println(imageId, imagePath)
if exists := isExists(imagePath); !exists {
http.NotFound(w, r)
return
}
//log.Println("222222222222")
w.Header().Set("Content-Type", "image")
http.ServeFile(w, r, imagePath)
}
func listHandler(w http.ResponseWriter, r *http.Request) {
fileInfoArr, err := ioutil.ReadDir("./uploads")
//log.Println(fileInfoArr)
check(err)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
locals := make(map[string]interface{})
images := []string{}
for _, fileInfo := range fileInfoArr {
//log.Println(fileInfo)
images = append(images, fileInfo.Name())
}
locals["images"] = images //這裡map的key是images,對應list.html裡面的$.images
renderHtml(w, "list.html", locals)


}

//巧用閉包避免執行階段錯誤崩潰
func safeHandler(fn http.HandlerFunc) http.HandlerFunc {


return func(w http.ResponseWriter, r *http.Request) {
defer func() {
log.Println("can‘t past execute or finished execute")
if e, ok := recover().(error); ok {
log.Println("50x error")
//50x error
http.Error(w, e.Error(), http.StatusInternalServerError)
log.Println("Warn : panic in %v. - %v", fn, e)
log.Println(string(debug.Stack()))
}
}()


log.Println("if no panic then first execute")
fn(w, r)
}
}
func staticDirHandler(mux *http.ServeMux, prefix string, staticDir string, flags int) {
mux.HandleFunc(prefix, func(w http.ResponseWriter, r *http.Request) {
file := staticDir + r.URL.Path[len(prefix)-1:]
if (flags & ListDir) == 0 {
if exists := isExists(file); !exists {
http.NotFound(w, r)
return
}
}
http.ServeFile(w, r, file)
})


}
func main() {
mux := http.NewServeMux()
staticDirHandler(mux, "/assets/", "./public", 0)
mux.HandleFunc("/", safeHandler(listHandler))
mux.HandleFunc("/view", safeHandler(viewHandler))
mux.HandleFunc("/upload", safeHandler(uploadHandler))
err := http.ListenAndServe(":8080", mux)
if err != nil {
log.Fatal("ListenAndServe:", err.Error())
}

}

uoload.html

<html>


<head>
<meta charset="utf-8">
<title>upload</title>
</head>
<body>
<form method="POST" action="/upload" enctype="multipart/form-data">
choosing an image to upload :<br>
<input name="image" type="file">
<input type="submit" value="Upload">
</form>
</body>
</html>

list.html

<html>
<head>
<meta charset="utf-8">
<title>list</title>
</head>
<body>
<ol>
{{range $.images}}
<li><a href="/view?id={{.|urlquery}}">{{.|html}}</a></li>
{{end}}
</ol>
</body>
</html>

注意檔案結構 

photoweb

                 -photoweb.go

                 -public

                                 -js

                                 -css

                                 -images

                  -uploads

                  -views

                                -upload.html

                                 -list.html


解決調用七牛音頻問題


   

樣本只提供代碼方式調用api,不能夠像圖片處理直接在url中添加參數進行修改

若強用url會提示要預先處理音頻轉碼,一般轉碼採用非同步方式


OK,在go的SDK代碼中進行,結果發現api給的參數只是個大概,go的api裡面的對應參數,不完全和官網貼出來的一樣

我們搜需PutPolicy


在rs/token.go 中,看到對應deadline;grep ‘xx‘ *  -R查看檔案裡內容


package main


import (
"fmt"
. "github.com/qiniu/api/conf"
"github.com/qiniu/api/fop"
"github.com/qiniu/api/io"
"github.com/qiniu/api/rs"
"log"
)


func init() {


ACCESS_KEY = "自己的ak"
SECRET_KEY = "自己sk"
}


//GET upload access token
func uptoken(bucketName string) string {
putPolicy := rs.PutPolicy{
Scope: bucketName,
//CallbackUrl: callbackUrl,
//CallbackBody:callbackBody,
//ReturnUrl: returnUrl,
//ReturnBody: returnBody,
//AsyncOps: asyncOps,
//EndUser: endUser,
//Expires: expires,
Expires:             1406555272, //截止時間戳記
PersistentOps:       "avthumb/mp3",
PersistentNotifyUrl: "http://fake.com/qiniu/notify",
}
return putPolicy.Token(nil)
}


func main() {
//上傳本地檔案
upload("a")


//5.1 擷取檔案資訊
//getFileInfo()


//6.1.1 查看映像屬性
//imageAttr()


//5.2 刪除檔案
//delFile()


}


//6.1.1 查看映像屬性
func imageAttr() {
var imageUrl = "http://needkane.qiniudn.com/kane2.jpg"
ii := fop.ImageInfo{}
infoRet, err := ii.Call(nil, imageUrl)
if err != nil {
// 產生錯誤
log.Println("fop getImageInfo failed:", err)
return
}


log.Println(infoRet.Height, infoRet.Width, infoRet.ColorModel, infoRet.Format)
}


func makeImageInfoUrl(imageUrl string) string {
ii := fop.ImageInfo{}
return ii.MakeRequest(imageUrl)
}


//5.2 刪除檔案
func delFile() {
bucket := "needkane"
key := "goupload.jpg"
var rsCli = rs.New(nil)


err := rsCli.Delete(nil, bucket, key)
if err != nil {
// 產生錯誤
log.Println("rs.Copy failed:", err)
return
}
}


//5.1 擷取檔案資訊
func getFileInfo() {
var ret rs.Entry
bucket := "needkane"
key := "kane3.jpg"
var rsCli = rs.New(nil)
var err error
ret, err = rsCli.Stat(nil, bucket, key)


if err != nil {
// 產生錯誤
log.Println("rs.Stat failed:", err)
return
}


// 處理返回值
log.Println(ret)
}


//上傳本地檔案
func upload(key string) {
uptoken := uptoken("needkane")
fmt.Printf("uptoken:%s\n", uptoken)


var err error
var ret io.PutRet
var extra = &io.PutExtra{
//Params: params,
//MimeType: mieType,
//Crc32: crc32,
//CheckCrc: CheckCrc,
}


var localFile = "/home/qboxtest/Downloads/a.wav"


// ret 變數用於存取返回的資訊,詳情見 io.PutRet
// uptoken 為商務服務器產生的上傳口令
// key 為檔案儲存體的標識
// localFile 為本地檔案名稱
// extra 為上傳檔案的額外資訊,詳情見 io.PutExtra,可選
err = io.PutFile(nil, &ret, uptoken, key, localFile, extra)


if err != nil {
//上傳產生錯誤
log.Print("io.PutFile failed:", err)
return
}


//上傳成功,處理返回值
log.Print(ret.Hash, ret.Key)


}

用 file 命令查看檔案


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.