mirror of
https://github.com/go-gitea/gitea
synced 2024-12-27 02:54:27 +00:00
c4deb97ed1
* github.com/alecthomas/chroma v0.8.1 -> v0.8.2 Changelog: https://github.com/alecthomas/chroma/releases/tag/v0.8.2 * github.com/blevesearch/bleve v1.0.12 -> v1.0.13 Changelog: https://github.com/blevesearch/bleve/releases/tag/v1.0.13 * github.com/editorconfig/editorconfig-core-go v2.3.8 -> v2.3.9 Changelog: https://github.com/editorconfig/editorconfig-core-go/releases/tag/v2.3.9 * github.com/klauspost/compress v1.11.2 -> v1.11.3 Changelog: https://github.com/klauspost/compress/releases/tag/v1.11.3 * github.com/minio/minio-go v7.0.5 -> v7.0.6 Changelog: https://github.com/minio/minio-go/releases/tag/v7.0.6 Co-authored-by: Lauris BH <lauris@nix.lv>
12 KiB
Vendored
12 KiB
Vendored
适用于与Amazon S3兼容云存储的MinIO Go SDK
MinIO Go Client SDK提供了简单的API来访问任何与Amazon S3兼容的对象存储服务。
支持的云存储:
-
AWS Signature Version 4
- Amazon S3
- MinIO
-
AWS Signature Version 2
- Google Cloud Storage (兼容模式)
- Openstack Swift + Swift3 middleware
- Ceph Object Gateway
- Riak CS
本文我们将学习如何安装MinIO client SDK,连接到MinIO,并提供一下文件上传的示例。对于完整的API以及示例,请参考Go Client API Reference。
本文假设你已经有 Go开发环境。
从Github下载
go get -u github.com/minio/minio-go
初始化MinIO Client
MinIO client需要以下4个参数来连接与Amazon S3兼容的对象存储。
参数 | 描述 |
---|---|
endpoint | 对象存储服务的URL |
accessKeyID | Access key是唯一标识你的账户的用户ID。 |
secretAccessKey | Secret key是你账户的密码。 |
secure | true代表使用HTTPS |
package main
import (
"log"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
endpoint := "play.min.io"
accessKeyID := "Q3AM3UQ867SPQQA43P2F"
secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
useSSL := true
// 初使化 minio client对象。
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
log.Printf("%#v\n", minioClient) // minioClient初使化成功
}
示例-文件上传
本示例连接到一个对象存储服务,创建一个存储桶并上传一个文件到存储桶中。
我们在本示例中使用运行在 https://play.min.io 上的MinIO服务,你可以用这个服务来开发和测试。示例中的访问凭据是公开的。
FileUploader.go
package main
import (
"context"
"log"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
ctx := context.Background()
endpoint := "play.min.io"
accessKeyID := "Q3AM3UQ867SPQQA43P2F"
secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
useSSL := true
// 初使化 minio client对象。
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
// 创建一个叫mymusic的存储桶。
bucketName := "mymusic"
location := "us-east-1"
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
if err != nil {
// 检查存储桶是否已经存在。
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
if errBucketExists == nil && exists {
log.Printf("We already own %s\n", bucketName)
} else {
log.Fatalln(err)
}
} else {
log.Printf("Successfully created %s\n", bucketName)
}
// 上传一个zip文件。
objectName := "golden-oldies.zip"
filePath := "/tmp/golden-oldies.zip"
contentType := "application/zip"
// 使用FPutObject上传一个zip文件。
n, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
log.Fatalln(err)
}
log.Printf("Successfully uploaded %s of size %d\n", objectName, n)
}
运行FileUploader
go run file-uploader.go
2016/08/13 17:03:28 Successfully created mymusic
2016/08/13 17:03:40 Successfully uploaded golden-oldies.zip of size 16253413
mc ls play/mymusic/
[2016-05-27 16:02:16 PDT] 17MiB golden-oldies.zip
API文档
完整的API文档在这里。
API文档 : 操作存储桶
API文档 : 存储桶策略
API文档 : 存储桶通知
SetBucketNotification
GetBucketNotification
RemoveAllBucketNotification
ListenBucketNotification
(MinIO 扩展)ListenNotification
(MinIO 扩展)
API文档 : 操作文件对象
API文档 : 操作对象
GetObject
PutObject
PutObjectStreaming
StatObject
CopyObject
RemoveObject
RemoveObjects
RemoveIncompleteUpload
SelectObjectContent
API文档 : Presigned操作
API文档 : 客户端自定义设置
完整示例
完整示例 : 操作存储桶
- makebucket.go
- listbuckets.go
- bucketexists.go
- removebucket.go
- listobjects.go
- listobjectsV2.go
- listincompleteuploads.go
完整示例 : 存储桶策略
完整示例 : 存储桶生命周期
完整示例 : 存储桶加密
完整示例 : 存储桶复制
完整示例 : 存储桶通知
- setbucketnotification.go
- getbucketnotification.go
- removeallbucketnotification.go
- listenbucketnotification.go (MinIO扩展)
- listennotification.go (MinIO 扩展)
完整示例 : 操作文件对象
完整示例 : 操作对象
- putobject.go
- getobject.go
- putobject-context.go
- getobject-context.go
- statobject.go
- copyobject.go
- removeobject.go
- removeincompleteupload.go
- removeobjects.go