静态云存储操作

静态云存储操作

GoTool 支持阿里云、腾讯云、本地三种形式的静态云存储,通过配置可以灵活地对静态文件进行同步和删除操作。

工具加载

import (
	"github.com/cnlesscode/gotool/cloud/static"
)

本地静态云存储配置

staticCloudConfig := &static.Config{
	Type:    "Local",
	BaseUrl: "http://localhost/",
}

阿里云静态云存储配置

staticCloudConfig := &static.Config{
	Type:       "AliOSS",
	Endpoint:   "oss-cn-beijing.aliyuncs.com",
	KeyId:      "******",
	Secret:     "******",
	BucketName: "阿里云 BucketName",
	BaseUrl:    "https://hcoder.oss-cn-beijing.aliyuncs.com/",
}

腾讯云静态云存储配置

staticCloudConfig := &static.Config{
	Type:     "TencentCOS",
	Endpoint: "https://test-***.cos.ap-nanjing.myqcloud.com",
	KeyId:    "******",
	Secret:   "******",
	BaseUrl:  "https://test-***.cos.ap-nanjing.myqcloud.com",
}

UploadFile(fileUrl string) error

函数功能 : 将本地文件同步到云

返回格式 : error

package main

import (
	"fmt"

	"github.com/cnlesscode/gotool/cloud/static"
)

func main() {
	staticCloudConfig := &static.Config{
		Type:     "TencentCOS",
		Endpoint: "https://test-***.cos.ap-nanjing.myqcloud.com",
		KeyId:    "******",
		Secret:   "******",
		BaseUrl:  "https://test-***.cos.ap-nanjing.myqcloud.com",
	}
	err := static.UploadFile(staticCloudConfig, "./demo.txt")
	fmt.Printf("err: %v\n", err)
}

RemoveFile(fileUrl string, removeLocalFile bool) error

函数功能 : 删除云文件及本地文件

返回格式 : error

package main

import (
	"fmt"

	"github.com/cnlesscode/gotool/cloud/static"
)

func main() {
	staticCloudConfig := &static.Config{
		Type:     "TencentCOS",
		Endpoint: "https://test-***.cos.ap-nanjing.myqcloud.com",
		KeyId:    "***",
		Secret:   "***",
		BaseUrl:  "https://test-***.cos.ap-nanjing.myqcloud.com",
	}
	err := static.RemoveFile(staticCloudConfig, "demo.txt", false)
	fmt.Printf("err: %v\n", err)
}