分布式是写出来的(一)
生活随笔
收集整理的這篇文章主要介紹了
分布式是写出来的(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
分布式對象存儲筆記
實現一個單機版本的對象存儲
package mainimport ("io""log""net/http""os""strings" )func Handler(w http.ResponseWriter, r *http.Request) {m := r.Methodif m == http.MethodPut {put(w, r)return}if m == http.MethodGet {get(w, r)return}w.WriteHeader(http.StatusMethodNotAllowed) }func get(w http.ResponseWriter, r *http.Request) {f, e := os.Open("C:/Users/HodgeKou/go/src/distribute_file_system/chapter1" + "/object/" + strings.Split(r.URL.EscapedPath(), "/") [2])if e != nil {log.Println(e)w.WriteHeader(http.StatusNotFound)return}defer f.Close()io.Copy(w, f) }func put(w http.ResponseWriter, r *http.Request) {f, e := os.Create("C:/Users/HodgeKou/go/src/distribute_file_system/chapter1" + "/object/" + strings.Split(r.URL.EscapedPath(), "/") [2])if e != nil {log.Println(e)w.WriteHeader(http.StatusInternalServerError)return}defer f.Close()io.Copy(f, r.Body) }func main() {http.HandleFunc("/object/", object.Handler)log.Fatal(http.ListenAndServe("127.0.0.1:8080", nil)) }Test
Test put數據
C:\Users\HodgeKou\go\src\distribute_file_system>curl -v 127.0.0.1:8080/object/1234.txt -XPUT -d "add test" * Trying 127.0.0.1... * TCP_NODELAY set * Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0) > PUT /objects/123.txt HTTP/1.1 > Host: 127.0.0.1:8080 > User-Agent: curl/7.55.1 > Accept: */* > Content-Length: 4 > Content-Type: application/x-www-form-urlencoded > * upload completely sent off: 4 out of 4 bytes < HTTP/1.1 405 Method Not Allowed < Date: Thu, 09 Apr 2020 06:18:51 GMT < Content-Length: 0 < * Connection #0 to host 127.0.0.1 left intactTest get數據
C:\Users\HodgeKou\go\src\distribute_file_system>curl -v 127.0.0.1:8080/object/1234.txt * Trying 127.0.0.1... * TCP_NODELAY set * Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0) > GET /object/1234.txt HTTP/1.1 > Host: 127.0.0.1:8080 > User-Agent: curl/7.55.1 > Accept: */* > < HTTP/1.1 200 OK < Date: Thu, 09 Apr 2020 07:20:36 GMT < Content-Type: text/plain; charset=utf-8 < Transfer-Encoding: chunked < add test* Connection #0 to host 127.0.0.1 left intact總結
以上是生活随笔為你收集整理的分布式是写出来的(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DNF稀有装扮冥光天羽套如何获得?
- 下一篇: 分布式是写出来的(二)