跳转至

获取对象

函数定义

GetObject(ctx context.Context, bucketName, objectName string, opts GetObjectOptions) (*Object, error)
返回对象数据流。大多数常见错误发生在读取流时。

参数

参数 类型 描述
ctx context.Context 自定义上下文,用于超时或取消调用
bucketName string 存储桶的名称
objectName string 对象的名称
opts minio.GetObjectOptions GET请求的选项,指定额外的选项,如加密、If-Match。

minio.GetObjectOptions

参数 类型 描述
opts.ServerSideEncryption encrypt.ServerSide encrypt包提供的接口,用于指定服务器端加密。
opts.Internal minio.AdvancedGetOptions 此选项仅供MinIO服务器内部使用。除非应用程序知道预期的用途,否则不应设置此选项。

返回值

参数 类型 描述
object *minio.Object minio.Object表示对象读取器。它实现了io.Reader、io.Seeker、io.ReaderAt和io.Closer接口。

示例

object, err := minioClient.GetObject(context.Background(), "mybucket", "myobject", minio.GetObjectOptions{})
if err != nil {
    fmt.Println(err)
    return
}
defer object.Close()

localFile, err := os.Create("/tmp/local-file.jpg")
if err != nil {
    fmt.Println(err)
    return
}
defer localFile.Close()

if _, err = io.Copy(localFile, object); err != nil {
    fmt.Println(err)
    return
}