当前位置:网站首页>Paste board based on curl and COS
Paste board based on curl and COS
2022-06-24 03:34:00 【Wang Lei -ai Foundation】
background
A lot of times , We need a temporary pasteboard , Sometimes we can use the chat tool as a pasteboard , Or find a similar service on the Internet for pasting . But there are obviously many limitations to doing so , Except not enough geek outside , There are many occasions , We need this sticker to work with other unix Class tools , Make up some more complex scripts .
So can we do a project based on curl The pasteboard tool , Temporary pasted content is also easier to handle , Just store it in the object store , Here we use the... On Tencent cloud cos Storage is a small tool 【cos The free quota of should be enough for us to use 】
Realization
First of all, this service is a http service , It needs to have the following functions :
- Support writing arbitrary binary data
- After writing the data, it returns a Clipboard id, adopt This id Data can be returned
- use curl Can use
- Support regular cleaning of old pasteboard data
- Store data to cos On
- other , such as size Limit ,qps Limit etc.
This is a very simple tool , The code implemented does not exceed 200 That's ok
var (
DefaultTTL = flag.Duration("default_ttl", time.Hour*24*7, "default ttl for object")
RateLimit = flag.Int("rate_limit", 1, "rate limit for api call")
SizeLimit = flag.Int64("size_limit", 1024*1024*10, "size limit for object in byte")
OSSecret = flag.String("os_secret", "::", "secret key for object storage, format: key:id:session")
BucketUrl = flag.String("bucket_url", "", "bucket_url for object storage")
NameLength = flag.Int("name_length", 4, "name length for object put")
)
func main() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
key, secret, token := "", "", ""
if os.Getenv("os_secret") != "" {
*OSSecret = os.Getenv("os_secret")
}
if os.Getenv("bucket_url") != "" {
*BucketUrl = os.Getenv("bucket_url")
}
secretList := strings.Split(*OSSecret, ":")
if len(secretList) >= 1 {
key = secretList[0]
}
if len(secretList) >= 2 {
secret = secretList[1]
}
if len(secretList) >= 3 {
token = secretList[2]
}
u, err := url.Parse(*BucketUrl)
if err != nil {
panic("bucket url not valid")
}
client := cos.NewClient(&cos.BaseURL{BucketURL: u}, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: key,
SecretKey: secret,
SessionToken: token,
},
})
go expireJob(client)
s := &http.Server{
Addr: ":80",
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
MaxHeaderBytes: 1 << 10,
}
limiter := rate.NewLimiter(rate.Limit(*RateLimit), *RateLimit)
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
_ = limiter.Wait(ctx)
if request.Method == "POST" {
log.Printf("[Handle] Method=Post Content-Length=%d", request.ContentLength)
if *SizeLimit > 0 && request.ContentLength > *SizeLimit {
writer.WriteHeader(400)
_, _ = writer.Write([]byte(fmt.Sprintf("content size out of limit: %d", *SizeLimit)))
return
}
name := fmt.Sprintf("%s/%s", time.Now().Format("20060102"), randName())
resp, err := client.Object.Put(ctx, name, request.Body, &cos.ObjectPutOptions{
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{},
})
if err != nil {
if resp != nil {
writer.WriteHeader(resp.StatusCode)
} else {
writer.WriteHeader(400)
}
log.Printf("[Handle] put file failed: %s", err)
_, _ = writer.Write([]byte("put file failed"))
return
}
writer.WriteHeader(resp.StatusCode)
_, _ = writer.Write([]byte(encodeName(name)))
} else if request.Method == "GET" {
name, err := decodeName(strings.Trim(request.URL.Path, "/"))
log.Printf("[Handle] Method=Get Name=%s", name)
if err != nil {
writer.WriteHeader(400)
log.Printf("[Handle] file not valid: %s", err)
_, _ = writer.Write([]byte("file name not valid"))
return
}
resp, err := client.Object.Get(ctx, name, nil)
if err != nil {
if resp != nil {
writer.WriteHeader(resp.StatusCode)
} else {
writer.WriteHeader(400)
}
log.Printf("[Handle] get file failed: %s", err)
_, _ = writer.Write([]byte("get file failed"))
return
}
data, _ := ioutil.ReadAll(resp.Body)
_, _ = writer.Write(data)
}
})
log.Println("starting server...")
log.Fatal(s.ListenAndServe())
}
func expireJob(client *cos.Client) {
for range time.Tick(time.Minute * 10) {
log.Println("do expire....")
expireTo := time.Now().Add(-1 * *DefaultTTL)
var cleaned []string
for day := 1; day <= 30; day++ {
toDelDay := expireTo.Add(time.Duration(day) * time.Hour * 24 * -1)
name := toDelDay.Format("20060102") + "/"
ctx := context.Background()
resp, err := client.Object.Head(ctx, name, nil)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
continue
}
log.Println("head object failed, ", err)
continue
}
if resp.StatusCode != 200 {
continue
}
resp, err = client.Object.Delete(ctx, name, nil)
if err != nil {
log.Println("delete object failed, ", err)
continue
}
if resp.StatusCode != 200 {
log.Println("delete object failed, ", resp.Status)
} else {
cleaned = append(cleaned, name)
log.Printf("expire old folder: %s success", name)
}
}
log.Println("do expire done:", cleaned)
}
}Use docker Deploy ,dockerfile as follows
FROM alpine ADD bin/clipboard /usr/local/bin ENTRYPOINT ["clipboard"]
demonstration
# Example 1
* curl 148.70.103.38:30744 -d "hello"
20210925Vd4h%
* curl 148.70.103.38:30744/20210925Vd4h
hello%
# Example 2
* curl 148.70.103.38:30744 -d '{"someJsonData"}'
20210925aHiw%
* curl 148.70.103.38:30744/20210925aHiw
{"someJsonData"}%
# Example 3: Upload a picture
* curl 148.70.103.38:30744 --data-binary @`pwd`/test.png
20210925fzul%
* curl 148.70.103.38:30744/20210925fzul > test1.png
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 70858 0 70858 0 0 162k 0 --:--:-- --:--:-- --:--:-- 162kThe complete project code is in here
边栏推荐
- Coding CD of Devops
- What is the difference between elasticity and scalability of cloud computing? What does elastic scaling of cloud computing mean?
- Coding Ci of Devops
- No monitoring information seen in kibana
- Principle of efficient animation Implementation-A preliminary exploration of jetpack compose
- Tencent cloud ASR product -php realizes the authentication request of the extremely fast version of recording file identification
- What port does the fortress machine use? What is the role of the fortress machine?
- Grp: how to automatically add requestid in GRP service?
- What is the principle of intelligent image recognition? What are the applications of intelligent image recognition?
- Go program lifecycle
猜你喜欢

Get to know MySQL database

Simple and beautiful weather code

Sorting out of key vulnerabilities identified by CMS in the peripheral management of red team (I)

Community pycharm installation visual database

元气森林推“有矿”,农夫山泉们跟着“卷”?

Ar 3D map technology

QT creator tips

On Sunday, I rolled up the uni app "uview excellent UI framework"
![[summary of interview questions] zj6 redis](/img/4b/eadf66ca8d834f049f3546d348fa32.jpg)
[summary of interview questions] zj6 redis
![[summary of interview questions] zj5](/img/d8/ece82f8b2479adb948ba706f6f5039.jpg)
[summary of interview questions] zj5
随机推荐
Community pycharm installation visual database
New Google brain research: how does reinforcement learning learn to observe with sound?
How to handle the uplink and downlink silence of TRTC
How to select a cloud game server? Which cloud game server recommends?
Hunan data security governance Summit Forum was held, and Tencent built the best practice of government enterprise data security
What is the GPU usage for cloud desktops and servers? What can cloud desktop do?
Record the creation process of a joke widget (II)
LeetCode 129. Find the sum of numbers from root node to leaf node
What does cloud desktop mean? What are the characteristics of cloud desktop?
Disaster recovery series (V) -- database disaster recovery construction
What is the difference between server leasing and hosting?
What is the role of the distributed configuration center? What are the advantages of a distributed configuration center?
How to register a trademark? What needs to be prepared?
Under what circumstances do you need a fortress machine? What are the functions of a fortress machine
take the crown! Tencent security won the 2021 national network security week outstanding innovation achievement award
Tencent cloud CIF engineering efficiency summit ends perfectly
Technical dry goods - how to use AI technology to accurately identify mining Trojans
What are the advantages of EIP? What is the relationship between EIP and fixed IP?
What does cloud computing elasticity mean? What are its functions?
Cloud development RMB 1 purchase activity is in progress