golang redis 隊列刪除圖片

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

   之前幾篇完成了縮圖功能,今天在常帥的指導下,完成了golang操作隊列刪除圖片功能,之前打算用lua-resty-redis弄,尼瑪,不懂怎麼觸發,SB了。然後常帥說用Golang,好吧.就有了下面的代碼。

package mainimport ("fmt""time""gopkg.in/redis.v5""strings""os""path/filepath")var client *redis.Clientfunc main() {fmt.Println("Hello, 世界")client = redis.NewClient(&redis.Options{Addr:         "ip:6379",DialTimeout:  10 * time.Second,ReadTimeout:  30 * time.Second,WriteTimeout: 30 * time.Second,PoolSize:     10,PoolTimeout:  30 * time.Second,})pong, err := client.Ping().Result()fmt.Println(pong,err)if err != nil {panic(err)}for{result, err := client.BLPop(0, "sync_img").Result()if err != nil {fmt.Println("error:  %s",err)continue}img_path := result[1]if len(img_path) < 1 {fmt.Println(" path is   null")continue}if(strings.Contains(img_path,"..")){fmt.Println(" path can't contains ..")continue}// (gif|jpg|png|jpegif(strings.HasSuffix(img_path, ".gif")  || strings.HasSuffix(img_path, ".jpg")  || strings.HasSuffix(img_path, ".png")  || strings.HasSuffix(img_path, ".jpeg")  ){full_path                 := "/tmp"+img_pathfiles, err  := filepath.Glob(full_path+"*")if err != nil{fmt.Println(err)continue}for _, f := range files{fmt.Println("rm path :%s",f)removeErr :=os.Remove(f)if removeErr != nil {        fmt.Printf("file remove Error! %s", removeErr)} else {        //如果刪除成功則輸出 file remove OK!        fmt.Print("file remove OK!") }}}else{fmt.Println(" only receive : gif | jpg |png |jpeg  ")}}}

上面的代碼東拼西湊的,對了,需要先用Go安裝: go get gopkg.in/redis.v5

然後在控制台執行運行命令: go run xxx.go  

然後常帥說在伺服器上使用root運行許可權太大了,一不小心把刪掉別的資料怎麼辦,然後找到使用nobody運行命令,修改如下

vim /etc/passwd找到nobody:x:99:99:Nobody:/:/sbin/nologin改為nobody:x:99:99:Nobody:/:/bin/bash後儲存執行su -nobody

  接著就可以執行: su -m nobody -c "go run redis_queue.go &"  

  資源: https://github.com/go-redis/redis 打完收工

  來源我的部落格:http://www.hihubs.com/article/271

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.