加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang redis 队列删除图片

发布时间:2020-12-16 19:13:16 所属栏目:大数据 来源:网络整理
导读:之前几篇完成了缩图功能,今天在常帅的指导下,完成了golang操作队列删除图片功能,之前打算用lua-resty-redis弄,尼玛,不懂怎么触发,SB了。然后常帅说用Golang,好吧.就有了下面的代码。 package mainimport ("fmt""time""gopkg.in/redis.v5""strings""os""

之前几篇完成了缩图功能,今天在常帅的指导下,完成了golang操作队列删除图片功能,之前打算用lua-resty-redis弄,尼玛,不懂怎么触发,SB了。然后常帅说用Golang,好吧.就有了下面的代码。

package main
import (
	"fmt"
	"time"
	"gopkg.in/redis.v5"
	"strings"
	"os"
	"path/filepath"
)
var client *redis.Client
func 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|jpeg
		if(strings.HasSuffix(img_path,".gif")  || strings.HasSuffix(img_path,".jpg")  || strings.HasSuffix(img_path,".png")  || strings.HasSuffix(img_path,".jpeg")  ){
			full_path                 := "/tmp"+img_path
			files,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读