Skip to content

Commit

Permalink
👥 Add contributor(s).
Browse files Browse the repository at this point in the history
  • Loading branch information
Han-Ya-Jun committed May 24, 2019
1 parent f3db928 commit 5fbe48a
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# qiniuyun_upload_tools
七牛云上传工具
# config
```json
{
"access_key": "************", // ak
"secret_key": "***************", // sk
"zone": 0, //存储空间所在区域,0:华东, 1:华北, 2:华南, 3:北美
"Bucket": "hanyajun", //存储空间名
"use_https": false,//是否使用https
"bucket": "http://cdn.hanyajun.com/"
}


```
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"access_key": "*******",
"secret_key": "*******",
"zone": 0,
"Bucket": "hanyajun",
"use_https": false,
"bucket": "http://cdn.hanyajun.com/"
}
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"html/template"
"io/ioutil"
"os"
)

/*
* @Author:hanyajun
* @Date:2019/5/22 0:01
* @Name:cmd
* @Function:
*/

func main() {
fileList := readDirectory("need_upload_data")
printFileList(fileList)
t, _ := template.ParseFiles("templates/success.html")
fmt.Println(t.Name())
fileObj, err := os.OpenFile("success.html", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
fmt.Println("Failed to open the file", err.Error())
os.Exit(2)
}
_ = t.Execute(fileObj, fileList)
}

func readDirectory(dir string) (fl []string) {

files, _ := ioutil.ReadDir(dir)

var fileList []string
fileList = make([]string, len(files))

i := 0
for _, file := range files {
if file.IsDir() {
continue
} else {
fileList[i] = file.Name()
i++
}
}
return fileList
}

func printFileList(fl []string) {
for i := 0; i < len(fl); i++ {
fmt.Println(fl[i])
}
}
Binary file added need_upload_data/123.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added need_upload_data/travis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added need_upload_data/travis_build.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added need_upload_data/travis_token.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions pkg/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pkg

import (
"fmt"
"io/ioutil"
)

/*
* @Author:hanyajun
* @Date:2019/5/25 0:48
* @Name:pkg
* @Function:
*/

/*
* 读取目录内的文件
*/
func readDirectory(dir string) (fl []string) {

files, _ := ioutil.ReadDir(dir)

var fileList []string
fileList = make([]string, len(files))

i := 0
for _, file := range files {
if file.IsDir() {
continue
} else {
fileList[i] = file.Name()
i++
}
}
return fileList
}

func printFileList(fl []string) {
for i := 0; i < len(fl); i++ {
fmt.Println(fl[i])
}
}
69 changes: 69 additions & 0 deletions pkg/qiniu_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package pkg

import (
"context"
"fmt"
"github.com/qiniu/api.v7/auth/qbox"
"github.com/qiniu/api.v7/storage"
"time"
)

/*
* @Author:hanyajun
* @Date:2019/5/22 15:16
* @Name:pkg
* @Function:
*/

type QiNiuClient struct {
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Bucket string `json:"bucket"`
Zone int `json:"zone"` //0:华东, 1:华北, 2:华南, 3:北美
UseHTTPS bool `json:"use_https"`
UseCdnDomains bool `json:"use_cdn_domains"`
Domain string `json:"domain"`
}

func NewClient(accessKey, secretKey, bucket string, Zone int, useHttps, useCdnDomains bool, domain string) *QiNiuClient {
return &QiNiuClient{
AccessKey: accessKey,
SecretKey: secretKey,
Bucket: bucket,
Zone: Zone,
UseHTTPS: useHttps,
UseCdnDomains: useCdnDomains,
Domain: domain,
}
}

func (client *QiNiuClient) UploadFile(fileList []string) {
putPolicy := storage.PutPolicy{
Scope: client.Bucket,
}
mac := qbox.NewMac(client.AccessKey, client.SecretKey)
upToken := putPolicy.UploadToken(mac)
cfg := storage.Config{}
// 空间对应的机房
cfg.Zone = &storage.ZoneHuadong
// 是否使用https域名
cfg.UseHTTPS = client.UseHTTPS
// 上传是否使用CDN上传加速
cfg.UseCdnDomains = client.UseCdnDomains
// 构建表单上传的对象
formUploader := storage.NewFormUploader(&cfg)
ret := storage.PutRet{}
// 可选配置
putExtra := storage.PutExtra{
//Params: map[string]string{
// "x:name": "github logo",
//},
}
key := time.Now().Unix() + "_"
err := formUploader.PutFile(context.Background(), &ret, upToken, key, localFile, &putExtra)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ret.Key, ret.Hash)
}
19 changes: 19 additions & 0 deletions templates/success.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>上传内容</title>
</head>
<body>
<div>
{{ range . }}
<img src='{{ . }}' height='120' />&nbsp;&nbsp;<span id='my_url_0'>![]({{ . }})</span>&nbsp;&nbsp;<button class='btn' data-clipboard-target='#my_url_0' style='width:50px;height:30px;'>复制</button><br/>
{{ end }}

</div>
<script type='text/javascript' src='https://cdn.staticfile.org/clipboard.js/1.5.15/clipboard.min.js'></script>
<script type = 'text/javascript' >
var clipboard = new Clipboard('.btn');
</script>
</body>
</html>

0 comments on commit 5fbe48a

Please sign in to comment.