阿里云oss多文件上传 go代码
时间: 2023-12-05 09:04:56 浏览: 27
好的,以下是一个使用 Go 语言实现的阿里云 OSS 多文件上传的示例代码:
```
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// 配置 OSS 客户端
endpoint := "yourEndpoint"
accessKeyID := "yourAccessKeyID"
accessKeySecret := "yourAccessKeySecret"
bucketName := "yourBucketName"
client, err := oss.New(endpoint, accessKeyID, accessKeySecret)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// 获取存储空间
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// 设置并发上传的协程数
oss.SetConcurrency(50)
// 上传文件列表
fileList := []string{
"/path/to/file1",
"/path/to/file2",
"/path/to/file3",
}
// 遍历文件列表进行上传
for _, filePath := range fileList {
// 打开文件
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error:", err)
continue
}
defer file.Close()
// 创建 ObjectMeta
meta := oss.ObjectMeta{}
// 上传文件
objectKey := "yourObjectKeyPrefix/" + filepath.Base(filePath)
err = bucket.PutObject(objectKey, file, meta)
if err != nil {
fmt.Println("Error:", err)
continue
}
fmt.Println("File uploaded successfully:", filePath)
}
}
```
在使用该代码前,您需要先将 `yourEndpoint`、`yourAccessKeyID`、`yourAccessKeySecret` 和 `yourBucketName` 替换成您自己的阿里云 OSS 相关信息。此外,您还需要修改 `fileList` 变量,将其中的文件路径替换成您想要上传的文件路径。