mirror of
https://github.com/go-gitea/gitea
synced 2024-11-07 10:44:26 +00:00
62e6c9bc6c
* Add a storage layer for attachments * Fix some bug * fix test * Fix copyright head and lint * Fix bug * Add setting for minio and flags for migrate-storage * Add documents * fix lint * Add test for minio store type on attachments * fix test * fix test * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Add warning when storage migrated successfully * Fix drone * fix test * rebase * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * remove log on xorm * Fi download bug * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * Add URL function to serve attachments directly from S3/Minio * Add ability to enable/disable redirection in attachment configuration * Fix typo * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * don't change unrelated files * Fix lint * Fix build * update go.mod and go.sum * Use github.com/minio/minio-go/v6 * Remove unused function * Upgrade minio to v7 and some other improvements * fix lint * Fix go mod Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Tyler <tystuyfzand@gmail.com>
11 KiB
Vendored
11 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 (
"github.com/minio/minio-go/v7"
"log"
)
func main() {
endpoint := "play.min.io"
accessKeyID := "Q3AM3UQ867SPQQA43P2F"
secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
useSSL := true
// 初使化 minio client对象。
minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
if err != nil {
log.Fatalln(err)
}
log.Printf("%#v\n", minioClient) // minioClient初使化成功
}
示例-文件上传
本示例连接到一个对象存储服务,创建一个存储桶并上传一个文件到存储桶中。
我们在本示例中使用运行在 https://play.min.io 上的MinIO服务,你可以用这个服务来开发和测试。示例中的访问凭据是公开的。
FileUploader.go
package main
import (
"github.com/minio/minio-go/v7"
"log"
)
func main() {
endpoint := "play.min.io"
accessKeyID := "Q3AM3UQ867SPQQA43P2F"
secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
useSSL := true
// 初使化minio client对象。
minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
if err != nil {
log.Fatalln(err)
}
// 创建一个叫mymusic的存储桶。
bucketName := "mymusic"
location := "us-east-1"
err = minioClient.MakeBucket(bucketName, location)
if err != nil {
// 检查存储桶是否已经存在。
exists, err := minioClient.BucketExists(bucketName)
if err == nil && exists {
log.Printf("We already own %s\n", bucketName)
} else {
log.Fatalln(err)
}
}
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(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 Extension)
API文档 : 操作文件对象
API文档 : 操作对象
GetObject
PutObject
PutObjectStreaming
StatObject
CopyObject
RemoveObject
RemoveObjects
RemoveIncompleteUpload
API文档: 操作加密对象
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扩展)
完整示例 : 操作文件对象
完整示例 : 操作对象
- putobject.go
- getobject.go
- putobject-context.go
- getobject-context.go
- statobject.go
- copyobject.go
- removeobject.go
- removeincompleteupload.go
- removeobjects.go