type Reader interface {
Read(p []byte) (n int, err error)
}
type MyIoReader struct{
UploadFileSize int
UploadeSize *int//这样用是因为Read不能用指针this,要不编译不过
Count int//test (this *MyIoReader) and (this MyIoReader)
}
//这样表示是类型MyIoReader实现了io.Reader接口, var instance io.Reader = MyIoReader
//这个p怎么就成了输入参数,应该是输出的参数,原理上是这个参数是指针型的既可做输入又可做输出
// 在minio的上传接口minio.PutObjectOptions中的Progress参数的接口中,p代表每次上传了的字节数据
func (this MyIoReader) Read(p []byte) (int, error) {
log.Info(" this is MyIoReader")
log.Infof("write len:", len(p))
log.Infof("upload_size1:", *this.UploadeSize)
*(this.UploadeSize) += len(p)
log.Infof("upload_size2:", *this.UploadeSize)
log.Info("Count:", this.Count)
return 0, nil
}
/*
这样表示是类型*MyIoReader实现了io.Reader接口, var instance io.Reader = new(MyIoReader)
func (this *MyIoReader) Read(p []byte) (int, error) {
log.Info(" this is MyIoReader")
log.Infof("write len:", len(p))
log.Infof("upload_size1:", *this.UploadeSize)
*(this.UploadeSize) += len(p)
log.Infof("upload_size2:", *this.UploadeSize)
return 0, nil
}
*/
//这个表示传的是MyIoReader类型对象的指针
func (this *MyIoReader) MyFun1(add int) {
log.Infof("count1:", this.Count)
this.Count += add
log.Infof("count2:", this.Count)
}
//这个表示传的是MyIoReader类型对象的一个拷贝对象(新生成的临时对象,当函数退出就会被垃圾回收)
func (this MyIoReader) MyFun2(add int) {
log.Infof("count1:", this.Count)
this.Count += add
log.Infof("count2:", this.Count)
}
type MyIoReader2 struct{
UploadFileSize int
UploadeSize int//这样用是因为Read不能用指针this,要不编译不过
}
//*MyIoReqder2才实现了io.Reader接口,而MyIoReader2是没实现io.Reader接口的
func (this *MyIoReader2) Read(p []byte) (int, error) {
log.Info(" this is MyIoReader")
log.Infof("write len:", len(p))
log.Infof("upload_size1:", this.UploadeSize)
this.UploadeSize += len(p)
log.Infof("upload_size2:", this.UploadeSize)
return 0, nil
}
测试使用示例:
var reader MyIoReader
reader.UploadeSize = new(int)
*(reader.UploadeSize) = 0
reader.Count = 101
url, err := PutLocalFileV2("/mnt/hgfs/share/CentOS-7-x86_64-Minimal-1810.iso", "dir_test/hello/file.iso", reader)
log.Infof("%#v, %#v", url, err)
/*
//*MyIoReqder2才实现了io.Reader接口,而MyIoReader2是没实现io.Reader接口的
var reader2 MyIoReader2
reader2.UploadeSize = 0
url, err := PutLocalFileV2("/mnt/hgfs/share/CentOS-7-x86_64-Minimal-1810.iso", "dir_test/hello/file.iso", &reader2)
log.Infof("%#v, %#v", url, err)
*/
对应封装函数
func PutLocalFileV2(local_file string, object_name string, progress io.Reader) (string, error) {
minio_client, err := minio.New(EndPoint, AccessKey, AccessSecret, UseSSL)
if err != nil {
log.Error(err)
return "", err
}
// 上传一个文件。
file_suffix := path.Ext(local_file)
if "" == file_suffix {
file_suffix = ".noSuffix"
}
content_type := "application/" + string([]byte(file_suffix)[1:])
bucket_name := BucketName
// 使用FPutObject上传一个文件。
n, err := minio_client.FPutObject(bucket_name, object_name, local_file, minio.PutObjectOptions{ContentType: content_type, Progress: progress})
if err != nil {
log.Error(err)
return "", err
}
log.Info("Successfully uploaded "+object_name+" of size:", n)
url := PutObjUrlHead + "/" + BucketName
if url[len(url)-1] != '/' {
url = url + "/"
}
url = url + object_name
return url, nil
}