forked from mirrors/gotosocial
9d0df426da
* feat: vendor minio client * feat: introduce storage package with s3 support * feat: serve s3 files directly this saves a lot of bandwith as the files are fetched from the object store directly * fix: use explicit local storage in tests * feat: integrate s3 storage with the main server * fix: add s3 config to cli tests * docs: explicitly set values in example config also adds license header to the storage package * fix: use better http status code on s3 redirect HTTP 302 Found is the best fit, as it signifies that the resource requested was found but not under its presumed URL 307/TemporaryRedirect would mean that this resource is usually located here, not in this case 303/SeeOther indicates that the redirection does not link to the requested resource but to another page * refactor: use context in storage driver interface
12 KiB
12 KiB
适用于与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