代码拉取完成,页面将自动刷新
大家可能会遇到这种场景:
如何解决这个问题:
1. nohup python -m SimpleHTTPServer [port] & 快速搭建一个http服务
2. nc ...
由于可能面临复杂网络下(多层跳板机)的上传和下载问题, 通过点对点传(比如scp之类)的很难行得通,所有选择用中转站的方式,比如oss
oss在产品开发过程中也经常用到比如:
我们寻找有没有现有的客户端:
client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
if err != nil {
// HandleError(err)
}
bucket, err := client.Bucket("my-bucket")
if err != nil {
// HandleError(err)
}
err = bucket.PutObjectFromFile("my-object", "LocalFile")
if err != nil {
// HandleError(err)
}
准备好你的阿里云bucket资源
开始准备开发一个项目:
1. 初始化一个go mod工程
go mod init "gitee.com/infraboard/go-course/day8/simple"
2. 编写main.go 引入sdk, 将基础样例封装为一个函数
3. 下载项目依赖
go mod tidy
var (
endpint = ""
acessKey = ""
secretKey = ""
bucketName = ""
uploadFile = ""
)
func upload(filePaht string) error {
client, err := oss.New(endpint, acessKey, secretKey)
if err != nil {
return err
}
bucket, err := client.Bucket(bucketName)
if err != nil {
return err
}
err = bucket.PutObjectFromFile(filePaht, filePaht)
if err != nil {
return err
}
return nil
}
func main() {
if err := upload(uploadFile); err != nil {
fmt.Printf("upload file error, %s", err)
os.Exit(1)
}
}
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/aliyun/aliyun-oss-go-sdk/oss.(*urlMaker).Init(0xc0001ba300, 0x0, 0x0, 0x1914c280000, 0x60, 0xc000024060)
E:/Golang/pkg/mod/github.com/aliyun/aliyun-oss-go-sdk@v2.1.9+incompatible/oss/conn.go:722 +0x40c
github.com/aliyun/aliyun-oss-go-sdk/oss.New(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe1ff40, ...)
E:/Golang/pkg/mod/github.com/aliyun/aliyun-oss-go-sdk@v2.1.9+incompatible/oss/client.go:52 +0xf2
main.upload(0x0, 0x0, 0x0, 0xc00004bf78)
e:/Projects/Golang/go-course/day8/simple/simple.go:26 +0x93
main.main()
e:/Projects/Golang/go-course/day8/simple/simple.go:19 +0x45
exit status 2
func main() {
if err := validate(); err != nil {
fmt.Printf("validate paras error, %s\n", err)
os.Exit(1)
}
if err := upload(uploadFile); err != nil {
fmt.Printf("upload file error, %s\n", err)
os.Exit(1)
}
}
func validate() error {
if endpint == "" {
return fmt.Errorf("endpoint missed")
}
if acessKey == "" || secretKey == "" {
return fmt.Errorf("access key or secret key missed")
}
if bucketName == "" {
return fmt.Errorf("bucket name missed")
}
return nil
}
再次执行 验证参数检测逻辑是否生效
validate paras error, access key or secret key missed
exit status 1
var (
endpint = "oss-cn-chengdu.aliyuncs.com"
acessKey = "ak"
secretKey = "sk"
bucketName = "bucket"
uploadFile = ""
)
结果如下:
upload file error, open : The system cannot find the file specified.
exit status 1
var (
endpint = "oss-cn-chengdu.aliyuncs.com"
acessKey = "ak"
secretKey = "sk"
bucketName = "cloud-station"
uploadFile = "go.sum"
)
upload file error, oss: service returned error: StatusCode=403, ErrorCode=InvalidAccessKeyId, ErrorMessage="The OSS Access Key Id you provided does not exist in our records.", RequestId=60FB893504ACC03439856321
exit status 1
var (
endpint = "oss-cn-chengdu.aliyuncs.com"
acessKey = "LTAI5tMvG5NA51eiH3ENZDaa"
secretKey = "找老师要"
bucketName = "cloud-station"
uploadFile = "go.sum"
)
upload file error, oss: service returned error: StatusCode=403, ErrorCode=AccessDenied, ErrorMessage="You have no right to access this object because of bucket acl.", RequestId=60FB89ACD9A90238360BCC5E
exit status 1
阿里云上配置该key的授权
upload file go.sum success
到此我们的程序可以正常运行,但是用户无法指定自己上传的文件
使用flag从命令行接收用户输入的参数
flag.StringVar(&uploadFile, "f", "", "指定本地文件")
定义一个函数用于读取用户从命令行传入的参数
func loadParam() {
flag.Parse()
}
这样就正常使用了
$ go run simple.go -f go.mod
upload file go.mod success
flag.BoolVar(&help, "h", false, "this help")
编写使用说明
func usage() {
fmt.Fprintf(os.Stderr, `cloud-station version: 0.0.1
Usage: cloud-station [-h] -f <uplaod_file_path>
Options:
`)
flag.PrintDefaults()
}
最后解析参数的时候补充使用说明逻辑
func loadParam() {
flag.Parse()
if help {
usage()
os.Exit(0)
}
}
测试
$ go run simple.go -h
cloud-station version: 0.0.1
Usage: cloud-station [-h] -f <uplaod_file_path>
Options:
-h this help
-upload_file string
指定本地文件
signedURL, err := bucket.SignURL(filePaht, oss.HTTPGet, 60*60*24)
if err != nil {
return fmt.Errorf("SignURL error, %s", err)
}
fmt.Printf("下载链接: %s\n", signedURL)
fmt.Println("\n注意: 文件下载有效期为1天, 中转站保存时间为3天, 请及时下载")
调试
下载链接: http://cloud-station.oss-cn-chengdu.aliyuncs.com/go.mod?Expires=1627186396&OSSAccessKeyId=LTAI5tMvG5NA51eiH3ENZDaa&Signature=TdwgxJVCOsmf84fqix8FKPmSLJc%3D
注意: 文件下载有效期为1天, 中转站保存时间为3天, 请及时下载
upload file go.mod success
我们以写脚本的一个模式写了一个工具, 他能最快的解决我们问题, 但是其中还是有一些简单的技巧
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。